Gasman
Enthusiastic Amateur
- Local time
- Today, 23:24
- Joined
- Sep 21, 2011
- Messages
- 16,534
I have an Excel workbook, that reads data from an Access query and produces a few charts.
Up until now I have been opening it manually, but I decided to add the opening of it to my Blood Pressures form.
It opens fine, but I either get a message that this will Cancel the Refresh (which I want) or does nothing. A manual RefreshAll then does not run the code which copies formulae for me. Code is below.
Is there a way to just run the WorkBook_Open() code from Access, without having to create a class, which is all ChatGPT can offer.
TIA
Code to open it.
Up until now I have been opening it manually, but I decided to add the opening of it to my Blood Pressures form.
It opens fine, but I either get a message that this will Cancel the Refresh (which I want) or does nothing. A manual RefreshAll then does not run the code which copies formulae for me. Code is below.
Is there a way to just run the WorkBook_Open() code from Access, without having to create a class, which is all ChatGPT can offer.
TIA
Code:
Private Sub Workbook_Open()
Dim lngLastRow As Long, lngLastRowCopy As Long
Dim strColName1 As String, strColName2 As String
Dim iCol As Integer
'Refresh sheet to get latest data.
ActiveWorkbook.RefreshAll
'Get last row in A in sheet qryPressure
Sheets("qryPressure").Select
'Find colname for 'With Time'
iCol = Application.WorksheetFunction.Match("With Time", ActiveSheet.Rows(1), 0)
strColName1 = ActiveSheet.Cells(1, iCol).Address(False, False)
strColName2 = ActiveSheet.Cells(1, iCol + 3).Address(False, False)
'Allow for wide sheets
If Len(strColName1) > 2 Then
strColName1 = Left(strColName1, 2)
strColName2 = Left(strColName2, 2)
Else
strColName1 = Left(strColName1, 1)
strColName2 = Left(strColName2, 1)
End If
lngLastRow = getlastrow(ActiveSheet.Name) '.Cells(ActiveSheet.Rows.Count, "A").End(xlUp).Row
lngLastRowCopy = getlastrow(ActiveSheet.Name, strColName1) 'Just copy from last row available instead of row 2 as before
'Copy formula in F2 down.
Range(strColName1 & "2:" & strColName2 & "2").Copy
Range(strColName1 & lngLastRow - 10 & ":" & strColName2 & lngLastRow).PasteSpecial xlPasteFormulasAndNumberFormats
Range(strColName1 & lngLastRow).Select
Sheets("Weekly Chart").Select ' Change as required
ActiveWorkbook.Save
End Sub
Code to open it.
Code:
Private Sub cmdPressure_Click()
Dim xlApp As Object
Dim xlWorkbook As Object
Dim excelFilePath As String
' Path to your Excel file
excelFilePath = Environ("HomePath") & "\Documents\Blood_Pressures.xlsm"
' Create a new instance of Excel
Set xlApp = CreateObject("Excel.Application")
'Getting refresh error when opening workbook from here
xlApp.DisplayAlerts = False
xlApp.Visible = True
' Open the workbook
Set xlWorkbook = xlApp.Workbooks.Open(excelFilePath)
' Make Excel visible
'xlApp.Visible = True
xlApp.DisplayAlerts = True
'xlWorkbook.RefreshAll
'Optional: Clean up if you don't need the Excel objects later
Set xlWorkbook = Nothing
Set xlApp = Nothing
End Sub
Last edited: