Good day everyone.
I've got a tiny issue I'm hoping someone can help me solve.
I've got a bit of code that takes approximately 20-30 seconds to complete.
While the code is running, a shape is unhidden which displays the text "Please Wait...", and then is hidden again towards the end of the script.
The issue I'm having is, when the code runs, the following line doesn't work:
If I manually unhide it and run the code, the Visible = False at the end works as intended.
Weirdly though, when I 'F8' through the code manually in the VBA editor, the "= True" at the beginning works; it just seems to be when you run the entire code - it fails.
I've tried putting the code to hide and unhide the shape in their own subs, and have my main code call on those subs, but the exact same thing happens.
Any help would be greatly appreciated.
Thank you.
My full code is as follows:
I've got a tiny issue I'm hoping someone can help me solve.
I've got a bit of code that takes approximately 20-30 seconds to complete.
While the code is running, a shape is unhidden which displays the text "Please Wait...", and then is hidden again towards the end of the script.
The issue I'm having is, when the code runs, the following line doesn't work:
VBA Code:
ActiveSheet.Shapes("PleaseWait").Visible = True
If I manually unhide it and run the code, the Visible = False at the end works as intended.
Weirdly though, when I 'F8' through the code manually in the VBA editor, the "= True" at the beginning works; it just seems to be when you run the entire code - it fails.
I've tried putting the code to hide and unhide the shape in their own subs, and have my main code call on those subs, but the exact same thing happens.
Any help would be greatly appreciated.
Thank you.
My full code is as follows:
VBA Code:
Sub RefreshReport()
'Declare Variables
Dim rngData As Range, tblData As ListObject, boolHasZero As Boolean, temp As String, Sheet As Worksheet, Pivot As PivotTable, Answer As VbMsgBoxResult, iWindowState As Integer
Dim CutOffDate As Long
'Notify user of task and ask if they want to continue.
Answer = MsgBox("The report will now be updated." & vbNewLine & "You will be notified once complete." & vbNewLine & "Do you wish to continue?", vbYesNo + vbQuestion + vbDefaultButton2, "Refresh Table")
If Answer = vbYes Then
Else
ActiveSheet.Shapes("PleaseWait").Visible = False
Exit Sub
End If
'Set the cut off date
temp = Application.InputBox("Please enter the cut-off date" & vbNewLine & "'DD/MM/YY'")
CutOffDate = CLng(DateValue(temp))
'Display 'Please Wait' box
[B][U]ActiveSheet.Shapes("PleaseWait").Visible = True[/U][/B]
'Disable Screen Updating
Application.Calculation = xlManual
Application.ScreenUpdating = False
Application.EnableEvents = False
'Unhide and go to 'Data' tab
Data.Visible = xlSheetVisible
Data.Select
'Set Variables for the active sheet
Set tblData = ActiveSheet.ListObjects("Data")
'Clear and refresh query table
Range("Data").Select
Selection.ClearContents
Selection.ListObject.QueryTable.Refresh BackgroundQuery:=False
'Populate "Total" column with formula
ActiveSheet.ListObjects("Data").DataBodyRange.Columns(18).FormulaR1C1 = _
"=IFERROR(SUM([@MOrderQty]*[@MPrice]/[@MConvFactPrcUm]),""0"")"
'Filter table by dates prior to the cut off, clear contents and unfilter the table
tblData.Range.AutoFilter Field:=tblData.ListColumns("OrderEntryDate").Index, Criteria1:="<=" & CutOffDate
tblData.DataBodyRange.ClearContents
tblData.Range.AutoFilter Field:=tblData.ListColumns("OrderEntryDate").Index
'Check if there are any zeros in the 'Total' column
For Each rngData In tblData.DataBodyRange.Columns(tblData.ListColumns("Total").Index).Cells
If rngData.Value = 0 Then
boolHasZero = True
Exit For
End If
Next rngData
'Filter Table if has zero
If boolHasZero = True Then
tblData.Range.AutoFilter Field:=tblData.ListColumns("Total").Index, Criteria1:="0"
tblData.DataBodyRange.ClearContents
tblData.Range.AutoFilter Field:=tblData.ListColumns("Total").Index
End If
'Sort by newest order entry date
ActiveWorkbook.Worksheets("Data").ListObjects("Data").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Data").ListObjects("Data").Sort.SortFields.Add2 Key _
:=Range("Data[[#All],[OrderEntryDate]]"), SortOn:=xlSortOnValues, Order:= _
xlDescending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Data").ListObjects("Data").Sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
'Delete all blank rows in the table.
tblData.Range.AutoFilter Field:=tblData.ListColumns("Total").Index, Criteria1:=""
tblData.DataBodyRange.EntireRow.Delete
tblData.Range.AutoFilter Field:=tblData.ListColumns("Total").Index
'Repopulate columns with formula.
ActiveSheet.ListObjects("Data").DataBodyRange.Columns(2).FormulaR1C1 = _
"=IF(XLOOKUP([@Supplier],SupplierNames[Supplier],SupplierNames[Supplier Name])="""","""",XLOOKUP([@Supplier],SupplierNames[Supplier],SupplierNames[Supplier Name]))"
ActiveSheet.ListObjects("Data").DataBodyRange.Columns(16).FormulaR1C1 = _
"=IF(XLOOKUP([@Supplier],SupplierNames[Supplier],SupplierNames[Master Category])="""","""",XLOOKUP([@Supplier],SupplierNames[Supplier],SupplierNames[Master Category]))"
ActiveSheet.ListObjects("Data").DataBodyRange.Columns(17).FormulaR1C1 = _
"=IF(XLOOKUP([@Supplier],SupplierNames[Supplier],SupplierNames[Buyer])="""","""",XLOOKUP([@Supplier],SupplierNames[Supplier],SupplierNames[Buyer]))"
Range("A1").Select
'Hide 'Data' tab
Data.Visible = xlSheetVeryHidden
'Unhide and go to 'Data2' tab
Data2.Visible = xlSheetVisible
Data2.Select
'Clear and refresh query table
Range("Data2").Select
Selection.ClearContents
Selection.ListObject.QueryTable.Refresh BackgroundQuery:=False
Range("A1").Select
'Hide 'Data2' tab
Data2.Visible = xlSheetVeryHidden
'Go to 'Home' page.
Home.Select
'Refresh all pivot tables.
For Each Sheet In ThisWorkbook.Worksheets
For Each Pivot In Sheet.PivotTables
Pivot.RefreshTable
Pivot.Update
Next
Next
'Enable Screen Updating.
Application.ScreenUpdating = True
Application.Calculation = xlAutomatic
Application.EnableEvents = True
'Remove 'Please Wait' box
ActiveSheet.Shapes("PleaseWait").Visible = False
'Notify user the task has been completed.
MsgBox ("Update Complete.")
End Sub
Last edited: