Question

I did a search before posting this question. The results that I found are only finding the last row of 1 column, I would like to find the last row of 2 columns then enter the data accordingly. Hope you guys could help. Thanks! :)

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

This is the output now

enter image description here

This is how the userform looks like

This is the output that I want

Was it helpful?

Solution

You were pretty close to the solution. ;) Try this:

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top