我有一个VBA代码,不断出错,给我一个“对象范围” _worksheet失败的错误。该代码引用了两个不同的工作表,一旦引用了未选中的工作表,就会出错,但是我无法弄清楚在其他工作表中引用范围的正确方法,而不会出现此错误。
我尝试使用With Worksheet,在Range和Cells对象之前引用了工作表,但我不断收到错误消息。
Sub Working_Macro()
'Row and Cloumn Selection
Dim Data As Worksheet
Set Data = Sheets("sheet3")
Dim Main As Worksheet
Set Main = Sheets("Sheet4")
Dim DataLastRW ' This is the last row for The Data set
Dim DataLastCOL ' This is the last Column for The Data set
Dim MainLastRG ' This is the last row to filter by for Filter Criteria
DataLastRW = Data.Cells(Rows.Count, 1).End(xlUp).Row
DataLastCOL = Data.Cells(11, Columns.Count).End(xlToLeft).Column 'The row for the cell need to be updated to represent the row in Data set.
MainLastRG = Main.Cells(Rows.Count, 1).End(xlUp).Row
Dim DataRN As Range
Dim CriteriaRN As Range
Set CriteriaRN = Main.Range(Cells(2, 1), Cells(MainLastRG, 1))
Set DataRN = Data.Range(Cells(11, 1), Cells(DataLastRW, DataLastCOL))
Range(DataRN).AdvancedFilter Action:=xlFilterInPlace, _
CriteriaRange:=Range(CriteriaRN), _
Unique:=False
我正在尝试创建一个宏,该宏基于Sheet4
上的输入对sheet3中的数据进行排序。但是我不断收到'Range of Object'_worksheet failed
错误