SzajnIn
SzajnIn

Reputation: 81

Write values from textboxes in a single row

I have some textboxes and a button, which when clicked writes the values in the textboxes in a row, here's a screenshot:

enter image description here

And here's the code:

Function theLastRow() As Long
    Dim lastRow As Long

    lastRow = Sheet2.Cells(Rows.Count, 1).End(xlUp).Row
    theLastRow = lastRow
End Function

Private Sub button1_Click()

    Sheet2.Cells(theLastRow + 1, 5).Value = Comment.Value

    'cant be left empty
    If (name1.Value <> "" And name2.Value <> "" And szsz.Value <> "" And Sum.Value <> "") Then
        Sheet2.Cells(theLastRow + 1, 1).Value = name1.Value
        Sheet2.Cells(theLastRow + 1, 2).Value = name2.Value
        Sheet2.Cells(theLastRow + 1, 3).Value = szsz.Value
        Sheet2.Cells(theLastRow + 1, 4).Value = Sum.Value

    End If 
End Sub

It almost works how it's supposed to, but not exactly:

enter image description here

Name2, szsz and sum always start one row lower, what's the problem?

Upvotes: 3

Views: 93

Answers (1)

findwindow
findwindow

Reputation: 3153

Per my comment above, try this.

Private Sub button1_Click()

Dim LastRow As Long
LastRow = Sheet2.Cells(Rows.Count, 1).End(xlUp).Row

Sheet2.Cells(LastRow + 1, 5).Value = Comment.Value

'cant be left empty
If (name1.Value <> "" And name2.Value <> "" And szsz.Value <> "" And Sum.Value <> "") Then
    Sheet2.Cells(LastRow + 1, 1).Value = name1.Value
    Sheet2.Cells(LastRow + 1, 2).Value = name2.Value
    Sheet2.Cells(LastRow + 1, 3).Value = szsz.Value
    Sheet2.Cells(LastRow + 1, 4).Value = Sum.Value

End If


End Sub

Upvotes: 3

Related Questions