Code that works on one workbook but not another ??

gheyman

Well-known Member
Joined
Nov 14, 2005
Messages
2,341
Office Version
  1. 365
Platform
  1. Windows
I have these two codes that I run on Workbook open. The second runs without any errors. The first one does not.

Note: Sheets("1 Proposal Selection") is visible and not hidden. And the sheet names are different in each workbook.

First
Code:
Private Sub Workbook_Open()

'Gets Error message: Run-time error '1004':  Application-defined or object-define error
MsgBox "Please be patient while the workbook updates with the latest data from ProPricer"

Sheets("1 Proposal Selection").Activate
Range("B16").Select

    With ThisWorkbook
         Sheets("1 Proposal Selection").ListObjects(1).QueryTable.Refresh BackgroundQuery:=False
     End With

End Sub

Second
Code:
Private Sub Workbook_Open()
'code that does not error

Sheets("Step 2").Visible = xlVeryHidden
Sheets("Step 3").Visible = xlVeryHidden
Sheets("DM Parts").Visible = xlVeryHidden
Sheets("DM Assemblies").Visible = xlVeryHidden
Sheets("DM Assembly Elements").Visible = xlVeryHidden
Sheets("Parts").Visible = xlVeryHidden
Sheets("Assemblies").Visible = xlVeryHidden
Sheets("Assembly Elements").Visible = xlVeryHidden
Sheets("Product List").Visible = xlVeryHidden
Sheets("Proposal List").Visible = xlVeryHidden

MsgBox "Please be patient while workbook is updated with ProPricer data."

    With ThisWorkbook
        Sheets("Proposal List").ListObjects(1).QueryTable.Refresh BackgroundQuery:=False
    End With

End Sub

Don't understand why one works and the other does not.

Thanks!
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
Which line actually causes the error?

Note: Your With block in the first code is not actually serving any purpose at all.
 
Upvote 0
Which line actually causes the error?

Note: Your With block in the first code is not actually serving any purpose at all.
Sheets("1 Proposal Selection").ListObjects(1).QueryTable.Refresh BackgroundQuery:=False
 
Upvote 0
Can you manually refresh the query? (I assume the sheet is not protected?)
 
Upvote 0
Yes.

And if I run this code separately (not on Workbook Open) it works fine.

Code:
Sub RefreshProposal()
    
    Sheets("1 Proposal Selection").ListObjects(1).QueryTable.Refresh BackgroundQuery:=False

End Sub
 
Upvote 0
You could just set the connection properties to refresh when opening the file?
 
Upvote 0
Solution
can you give a little more information on how? Thanks
 
Upvote 0
On the Data tab, click Connections, select the connection, then the Properties button, and check the option to refresh when the workbook opens.
 
Upvote 0

Forum statistics

Threads
1,214,875
Messages
6,122,042
Members
449,063
Latest member
ak94

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top