无法增加Cmd接下来

时间:2011-10-31 09:25:20

标签: excel vba

当在输入表单中单击按钮时,下面的代码应该是下一条记录。

当我点击它时,我的按钮在sheet3上被命名为CurrRecNew,它执行下面的代码,但它似乎没有增加。关于我做错了什么的建议?

数据表第1页的单元格从第A3行开始向下,例如

A3 1 B3 a
A4空白B4 b
A5 Blank B5 c
A6 2 B6 d
A7空白B7 f
A8空白B8 g
A9空白B9 h
A8 3 B10 ......

Sub ViewLogDown()
    Dim historyWks As Worksheet
    Dim InputWks As Worksheet

    Dim lRec As Long
    Dim lRecRow As Long
    Dim lLastRec As Long
    Dim LastRow As Long
    Dim Rlen As Long
    Dim lCurrentRow As Long

    lCurrentRow = lCurrentRow + 1

    Application.EnableEvents = False

    Set InputWks = Worksheets("Sheet3")
    Set historyWks = Worksheets("Sheet1")

    With historyWks
        LastRow = .Cells(.Rows.Count, "A").End(xlUp).Offset(1, 0).Row - 1
        lLastRec = LastRow - 1
    End With

    With InputWks
        lCurrentRow = lCurrentRow + 1
        lRec = .Range("CurrRecNew").Value

        Do While Len(Cells(lCurrentRow, 1).Value) = 0
            lCurrentRow = lCurrentRow + 1
        Loop
        lCurrentRow = lCurrentRow - 1

        .OLEObjects("tbRiskID").Object.Value = historyWks.Cells(lCurrentRow, 1)
        .OLEObjects("tbRiskContext").Object.Value = historyWks.Cells(lCurrentRow, 2)
        .OLEObjects("TextBox34").Object.Value = historyWks.Cells(lCurrentRow, 3)
        .OLEObjects("tbRiskEx").Object.Value = historyWks.Cells(lCurrentRow, 4)
        .OLEObjects("tbRiskCat").Object.Value = historyWks.Cells(lCurrentRow, 5)
    End With
    Application.EnableEvents = True
End Sub

1 个答案:

答案 0 :(得分:1)

您的代码非常混乱,您在InputWks工作表上找到lCurrentRow但是然后将文本框对象设置为Historywks工作表上的lcurrentrow ???您需要清楚地解释每个工作表的作用,您希望在哪个工作表上找到下一行等。

我假设您使用命名范围CurrRecNew来存储当前行。你想要获得historywrks表上的当前行。因此,只要查找下一行是您的实际问题,您的代码应如下所示:

    Dim rFound As Range

    '// History sheet
    With historyWks  
        '// Get current row, you need to correctly define the sheet name which contains the CurrRecNew Range.
        lCurrentRow = InputWks.Range("CurrRecNew").Value

        Set rFound = .Columns(1).Find(What:="*", After:=.Cells(lCurrentRow, 1))

        If Not rFound Is Nothing Then
            If rFound.Row > lCurrentRow Then
                lCurrentRow = rFound.Row
                txtName.Text = Cells(lCurrentRow, 1).Value
                txtPhone.Text = Cells(lCurrentRow, 2).Value
            End If
        End If

        '// Once again correct the sheet name here I guessed CurrRecNew was on the InputWks sheet
        InputWks.Range("CurrRecNew").Value = lCurrentRow

    End with