Excel 从第2列中获取最后一行,然后在第1列中输入数据

Excel 从第2列中获取最后一行,然后在第1列中输入数据,excel,vba,userform,Excel,Vba,Userform,在发布此问题之前,我进行了搜索。我找到的结果只是查找1列中的最后一行,我想查找2列中的最后一行,然后相应地输入数据。希望你们能帮忙。谢谢!:) 这是现在的输出 这就是userform的外观 这是我想要的输出 您非常接近解决方案。;)试试这个: Private Sub CommandButton1_Click() Dim rA As Long, rB As Long Dim lastRow As Long rA = Cells(Rows.Count, 1).End(xl

在发布此问题之前,我进行了搜索。我找到的结果只是查找1列中的最后一行,我想查找2列中的最后一行,然后相应地输入数据。希望你们能帮忙。谢谢!:)

这是现在的输出

这就是userform的外观

这是我想要的输出


您非常接近解决方案。;)试试这个:

Private Sub CommandButton1_Click()
    Dim rA As Long, rB As Long
    Dim lastRow As Long
    rA = Cells(Rows.Count, 1).End(xlUp).Row ' returns last row of first column
    rB = Cells(Rows.Count, 2).End(xlUp).Row ' returns last row of second column
    lastRow = IIf(rA > rB, rA + 1, rB + 1) ' returns maximum of rA and rA plus one
    If optMemberName.Value = True Then
        Cells(lastRow, 1) = txtMemberName.Text
    ElseIf optMemberID.Value = True Then
        Cells(lastRow, 2) = txtMemberID.Text
    End If
End Sub

嗨,谢谢你回答我的问题,我试过你的密码。但是我在这一行有一个运行时错误“424”
需要对象
,即rA=Cells(Row.Count,1)。End(xlUp)。Row然后我尝试添加
rA=Sheet1.Cells(Row.Count,1)。End(xlUp)。Row
但我仍然有相同的错误。
rA=Cells(Row.Count,1)。End(xlUp)。Row
是错误的。它必须是
rA=Cells(Rows.Count,1)。End(xlUp)。Row
。你忘了s.Oh。哈哈,是的,它已经起作用了:)非常感谢!:)但是还有其他的方法吗?不使用
单元格
,而是使用
范围
Private Sub CommandButton1_Click()
    Dim rA As Long, rB As Long
    Dim lastRow As Long
    rA = Cells(Rows.Count, 1).End(xlUp).Row ' returns last row of first column
    rB = Cells(Rows.Count, 2).End(xlUp).Row ' returns last row of second column
    lastRow = IIf(rA > rB, rA + 1, rB + 1) ' returns maximum of rA and rA plus one
    If optMemberName.Value = True Then
        Cells(lastRow, 1) = txtMemberName.Text
    ElseIf optMemberID.Value = True Then
        Cells(lastRow, 2) = txtMemberID.Text
    End If
End Sub