Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Write values from textboxes in a single row

Tags:

excel

vba

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?

like image 451
SzajnIn Avatar asked Feb 18 '26 18:02

SzajnIn


1 Answers

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
like image 124
findwindow Avatar answered Feb 21 '26 10:02

findwindow



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!