Thanks stnkynts. I figured out the problem, but I don't understand why. Let me give you another example. Here is my code:
Sub Test()
Dim Macro1, Macro2 As Worksheet
Set Macro1 = Worksheets("Macro1")
Set Macro2 = Worksheets("Macro2")
Macro1.Range(Cells(1, 1), Cells(16, 6)).Sort _
Key1:=Columns(4), _
Order1:=xlAscending
End Sub
I notice that when I am active in the Macro1 worksheet, it works fine. But when I move to the Macro2 worksheet and run the same code, I get a Run-time error 1004. I don't understand why I cannot work across several worksheets when I defined both worksheets.
The same thing happened with the AdvancedFilter application I posted earlier. There, I set a command button in Sheet 2. When I run the code while having Sheet 1 active, it runs fine. But when I click on the command button in Sheet 2 or when I am active in Sheet 2, I get the Run-time error 1004 again.
Also, I have the codes written inside a module of the workbook, not in any specific worksheet object.
Bookmarks