从2列获取最后一行,然后将数据输入到列的1中

时间:2013-12-10 08:11:24

标签: excel vba userform

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

If optMemberName.Value = True Then
    With Sheets("Sheet1")
        Range("A" & .Cells(.Rows.Count, "A").End(xlUp).Row) = txtMemberName.Text
    End With
ElseIf optMemberID.Value = True Then
    With Sheets("Sheet1")
        Range("B" & .Cells(.Rows.Count, "B").End(xlUp).Row) = txtMemberID.Text
    End With
End If

这是现在的输出

enter image description here

这就是用户形式的样子

这是我想要的输出

1 个答案:

答案 0 :(得分:0)

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

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