找不到vba的方法或数据成员

时间:2017-09-11 10:09:07

标签: excel vba excel-vba

我有一个用三个列表框设计的用户表单。列表框由工作表填充,List_Dev_Red,List_Man_Red,List_SQM_Red。在我的用户窗体激活中,我收到错误"方法或数据成员"在下面的行中找不到

  

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

功能最后(from 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

3 个答案:

答案 0 :(得分:1)

您需要稍微更改语法(在代码的上半部分):

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

答案 1 :(得分:1)

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

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

他们来自同一网站as the Variant function

答案 2 :(得分:0)

Option Explicit
'***** VARIABLES *****
Public wbI As Workbook, wsChess As Worksheet
Public blackPlayer As cPlayer, whitePlayer As cPlayer
Public txtB_bPlayerName As TextBox, txtB_wPlayerName As TextBox

Public Sub initVars()

    Set wbI = ThisWorkbook
    Set wsChess = wbI.Sheets(1)

    'Works
    ThisWorkbook.Sheets(1).txtB_bPlayerName.Value = ""

    'Don't work
    wsChess.txtB_wPlayerName.Value = ""

End Sub