Excel VBA 4位数字运行时错误91

这是我的代码:

Sub MaxNumberRow() Dim max As Double Dim rowNum As Long With Sheet1 max = WorksheetFunction.max(.Columns(1)) If max > 0 Then rowNum = .Columns(1).Find(What:=max, After:=.Cells(1, 1), LookIn:=xlValues, LookAt:= xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False).Row Range(Cells(1, 1), Cells(rowNum, 1)).Select End If End With End Sub 

但是我得到一个运行时错误“91”:对象variables或块variables未设置。

当我find最多4位数字时出错。

请帮忙….

提前致谢

一些单元格是相对于当前表单。 试试这个:

 Sub MaxNumberRow() Dim max As Double Dim rowNum As Long With Sheet1 max = WorksheetFunction.max(.Columns(1)) If max > 0 Then rowNum = WorksheetFunction.Match(max, .Columns(1), 0) .Columns(1).Resize(rowNum).Select End If End With End Sub 

为了使它工作,我不得不 – 在当前工作簿中直接引用“Sheet1” – 在with域中定位RangeCells (带有前缀) .

你用下面的代码得到什么?

 Sub MaxNumberRow() On Error GoTo ErrorTrap Dim max As Double Dim rowNum As Long With ThisWorkbook.Sheets("Sheet1") .Range(.Cells(1, 1), .Cells(2, 1)).Select max = WorksheetFunction.max(.Columns(1)) If max > 0 Then rowNum = .Columns(1).Find(What:=max, After:=.Cells(1, 1), LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False).Row .Range(.Cells(1, 1), .Cells(rowNum, 1)).Select End If End With Exit Sub ErrorTrap: Beep MsgBox "FAILED" & Chr(13) & _ "arg: " & rowNum & Chr(13) & _ "Error number: " & Err & Chr(13) & _ Error(Err) End Sub