I have a spread sheet that has multiple query tables across multiple sheets. I'v written a worksheet change sub that is is supposed to refresh all the data in the different tables, and then copy a certain range of data from one of the query tables and paste it on another sheet.
But every time I run the code, I keep getting an run-time error when the macro tries to select the data in the query table after the refresh.
Anyone know how I can fix this problem?
Error:
Run-time Error '1004'
Select method of Range Class Failed
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$A$2" Then
Application.ScreenUpdating = False
'Refresh All Query Tables
ActiveWorkbook.RefreshAll
'Clear data old data from main sheet
Sheets("beamlift").Select
Range("A5").Select
Range(Selection, Selection.End(xlDown)).Select
Application.CutCopyMode = False
Selection.ClearContents
'Select and copy the new data from queried table
Sheets("Mo Prod").Select
'This next line is where the code always fails
Range("B7").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
'Past new data on main sheet
Sheets("BeamLift").Select
Range("A5").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Columns("B:B").EntireColumn.AutoFit
Application.ScreenUpdating = True
End If
End Sub
Bookmarks