我遇到了运行时错误91,我不明白为什么。我将此代码用于不同的工作簿并且它完美地工作,列中的信息与我正在提取的不同但我将所有列和范围更改为正确的,但现在我在这里得到此错误并且唯一差异是范围,请帮助!
Range(Cells(20, 1), Cells(LastRow, LastCol)).Select
Selection.AutoFilter
Range("C2").Select
这是开始,但这里是错误发生的地方:
ActiveWorkbook.Worksheets(msheet).AutoFilter.Sort.SortFields. _
Add Key:=Range("A20:A" & LastRow), SortOn:=xlSortOnValues, Order:=xlAscending, _
DataOption:=xlSortNormal
请帮助,我不知道我错过了什么。谢谢!
以下是代码的开头:
Sub getdata()
Dim mastername As String
Dim count As Long
Dim match As Long
Dim repeat As Long
Dim path As String
Dim status As String
Dim name As String
Dim mpath As String
Dim cpath As String
Dim LastRow As Long
Dim LastCol As Integer
Dim mbank As String
Dim mname As String
mpath = Sheets("Master log").Cells(14, "W").Value
mname = Sheets("Master log").Cells(15, "W").Value
msheet = Sheets("Master log").Cells(16, "W").Value
Sheets("MGPR1").Range("A1:AA50000").ClearContents
name = Application.ActiveWorkbook.name
cpath = Application.ActiveWorkbook.path & "\"
Windows(name).Activate
'--open Management report workbook if not already open
If CheckFileIsOpen(mname) = False Then
Workbooks.Open mpath & mname
End If
'-------------------------------------------
Windows(mname).Activate
Sheets(msheet).Select
'select full data
With ActiveSheet
LastRow = .Cells(.Rows.count, "A").End(xlUp).Row
End With
With ActiveSheet
' LastCol = .Cells(1, .Columns.count).End(xlToLeft).Column
LastCol = 20
End With
答案 0 :(得分:1)
您必须引用range("A20.....")
的表格或其在活动表格中的表格。
类似sh.range("...")
之类的内容,或使用此示例中的with
部分:
with ActiveWorkbook.Worksheets(msheet)
.AutoFilter.Sort.SortFields. _
Add Key:= .Range("A20:A" & LastRow), SortOn:=xlSortOnValues, Order:=xlAscending, _
DataOption:=xlSortNormal
end with
也可以帮助引用,并停止使用activate / select:
Dim Wb as Workbook
Dim Sh as Worksheet
'other code, i just write the needed code now
set wb = Workbooks.Open mpath & mname
set Sh = wb.Sheets(msheet)
with Sh
LastRow = .Cells(.Rows.count, "A").End(xlUp).Row
'lots of stuff to do (....) please wait , computing .... error / no really i try to be serious here!
end with
答案 1 :(得分:0)
错误的含义是:
91 - 未设置对象变量
涉及的对象链是钝的。这是一个录制的宏,可以完成您要尝试的类型。如果有疑问,请将复杂的陈述解释为更简单的陈述。
也总是做Msgbox Vartype(whatever):Msgbox IsEmpty(whatever):msgbox IsNull(whatever)
。这允许您检查关于什么是什么的假设。
Range("A1:G19").Select
Selection.AutoFilter
Selection.AutoFilter Field:=3, Criteria1:="<>sub", Operator:=xlAnd
Selection.Sort Key1:=Range("C1"), Order1:=xlAscending, Header:=xlGuess, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal
Range("H34").Select