未find方法或数据成员vba

我有一个用三个列表框devise的用户表单。 列表框从工作表List_Dev_Red,List_Man_Red,List_SQM_Red中填充。 在我的用户表单激活,我得到一个错误“方法或数据成员”没有在下面的行中find

对于xRow = 2 To Last(1,List_Dev_Red.Range(“A:A”))

任何人都可以打电话给我,可能是这个错误的原因。

Private Sub UserForm_Activate() Dim xRow As Integer Dim yRow As Integer Dim zrows As Integer For xRow = 2 To Last(1, List_Dev_Red.Range("A:A")) With LB1 .AddItem List_Dev_Red.Cells(xRow, 3).Value If List_Dev_Red.Cells(xRow, 2) = True Then .Selected(xRow - 2) = True Else .Selected(xRow - 2) = False End If End With Next xRow LB1.Height = (xRow - 1) * 15 For yRow = 2 To Last(1, List_Man_Red.Range("A:A")) With LB2 .AddItem List_Man_Red.Cells(yRow, 3).Value If List_Man_Red.Cells(yRow, 2) = True Then .Selected(yRow - 2) = True Else .Selected(yRow - 2) = False End If End With Next yRow LB2.Height = (yRow - 1) * 15 For zrows = 2 To Last(1, List_SQM_Red.Range("A:A")) With LB3 .AddItem List_SQM_Red.Cells(zrows, 3).Value If List_SQM_Red.Cells(zrows, 2) = True Then .Selected(zrows - 2) = True Else .Selected(zrows - 2) = False End If End With Next zrows LB3.Height = (zrows - 1) * 15 End Sub 

function最后( 来自RondeBruin ):

 Function Last(choice As Long, rng As Range) ' 1 = last row ' 2 = last column ' 3 = last cell Dim lrw As Long Dim lcol As Long Select Case choice Case 1: On Error Resume Next Last = rng.Find(What:="*", _ After:=rng.Cells(1), _ LookAt:=xlWhole, _ LookIn:=xlFormulas, _ SearchOrder:=xlByRows, _ SearchDirection:=xlPrevious, _ MatchCase:=False).Row On Error GoTo 0 Case 2: On Error Resume Next Last = rng.Find(What:="*", _ After:=rng.Cells(1), _ LookAt:=xlWhole, _ LookIn:=xlFormulas, _ SearchOrder:=xlByColumns, _ SearchDirection:=xlPrevious, _ MatchCase:=False).Column On Error GoTo 0 Case 3: On Error Resume Next lrw = rng.Find(What:="*", _ After:=rng.Cells(1), _ LookAt:=xlPart, _ LookIn:=xlFormulas, _ SearchOrder:=xlByRows, _ SearchDirection:=xlPrevious, _ MatchCase:=False).Row On Error GoTo 0 On Error Resume Next lcol = rng.Find(What:="*", _ After:=rng.Cells(1), _ LookAt:=xlPart, _ LookIn:=xlFormulas, _ SearchOrder:=xlByColumns, _ SearchDirection:=xlPrevious, _ MatchCase:=False).Column On Error GoTo 0 On Error Resume Next Last = rng.Parent.Cells(lrw, lcol).Address(False, False) If Err.Number > 0 Then Last = rng.Cells(1).Address(False, False) Err.Clear End If On Error GoTo 0 End Select End Function 

你需要改变一下你的语法(在你的代码的上半部分):

 Dim xRow As Long Dim yRow As Long Dim zrows As Long Dim LastRow As Long ' <-- change all variables to Long (be on the safe side) ' get the last row by calling your Last function LastRow = Last(1, List_Dev_Red.Range("A:A")) ' <-- you are passing a Range, and want to get a Long, representing the row number ' loop through your rows For xRow = 2 To LastRow ' rest of your code Next xRow 

并改变你的Function返回一个Long (行号):

 Function Last(choice As Long, rng As Range) As Long 

一般来说,关于你知道你正在寻找列或行,你可以使用这样的简化函数:

 Option Explicit Function LastRow(sh As Worksheet) As Long On Error Resume Next LastRow = sh.Cells.Find(What:="*", _ After:=sh.Range("A1"), _ Lookat:=xlPart, _ LookIn:=xlFormulas, _ SearchOrder:=xlByRows, _ SearchDirection:=xlPrevious, _ MatchCase:=False).Row On Error GoTo 0 End Function Function LastCol(sh As Worksheet) As Long On Error Resume Next LastCol = sh.Cells.Find(What:="*", _ After:=sh.Range("A1"), _ Lookat:=xlPart, _ LookIn:=xlFormulas, _ SearchOrder:=xlByColumns, _ SearchDirection:=xlPrevious, _ MatchCase:=False).Column On Error GoTo 0 End Function 

它们来自Variant函数的同一站点。