Refresh Worksheet prior to continuing with rest of VBA

jamobe

New Member
Joined
Dec 23, 2014
Messages
36
Office Version
  1. 365
Platform
  1. Windows
Hi

So I have this VBA code below which I use to export a query I have as a csv file. I am trying to get it so the query refreshes before carrying on with the export. It does the job I need, however it only refreshes after it has exported. So theoretically I have to do it twice for it to work. The 'Call ThisWorkbook.RefreshAll' I have tried in various places. I hope this makes sense. Thanks


VBA Code:
Private Sub ExportMinMaxRepricer_Click()



Dim proceed As Integer
proceed = MsgBox("Proceed With Exporting Min Max Repricer File?", vbQuestion + vbYesNo)

  If proceed = vbYes Then

Dim wbkExport As Workbook
Dim shtToExport As Worksheet

Call ThisWorkbook.RefreshAll

Set shtToExport = ThisWorkbook.Worksheets("Min_Max_New")     'Sheet to export as CSV
Set wbkExport = Application.Workbooks.Add
shtToExport.Copy Before:=wbkExport.Worksheets(wbkExport.Worksheets.Count)
Application.DisplayAlerts = False                       'Possibly overwrite without asking
wbkExport.SaveAs Filename:="C:\Users\xxxja\Dropbox\Repricer\Min_Max.csv", FileFormat:=xlCSV
Application.DisplayAlerts = True
wbkExport.Close SaveChanges:=False
MsgBox "Repricer Min Max Export Successful", vbInformation, "Exporting"
Else

  End If


End Sub
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
See if this makes a difference.

Right Click on the query in the query navigation pane in Excel > Select properties
Uncheck > "Enable Background Refresh"

1665146672061.png
 
Upvote 0
Thanks very much, both of these have helped and made it work on 1st export :) Did not realise that with call function, guess it was never refreshing at all. Am a complete novice with VBA and just splice bits of code I find until it works, but this works like a charm so thanks again!
 
Upvote 0
You're welcome. Glad we could help.

PS: You can use a the Call function to run the refresh in the way you have done. It is however unusual to use the Call statement when executing standard VBA functions. eg you can also use Call Msgbox ("Hello") but again very few people would do it that way, standard practice is just to have Msgbox "Hello"
It is similar to assigning a value to a variable, technically it is "Let a = 10 + 20" but almost nobody uses the optional "Let" when doing so.
 
Upvote 0
Thanks very much, both of these have helped and made it work on 1st export :) Did not realise that with call function, guess it was never refreshing at all. Am a complete novice with VBA and just splice bits of code I find until it works, but this works like a charm so thanks again!
Glad to help you
 
Upvote 0

Forum statistics

Threads
1,215,022
Messages
6,122,716
Members
449,093
Latest member
Mnur

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