从单个行中的文本框中写入值

时间:2015-10-15 18:30:22

标签: excel vba excel-vba

我有一些文本框和一个按钮,点击它时会将文本框中的值写成一行,这是一个截图:

enter image description here

这是代码:

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

它几乎可以运作,但不完全正确:

enter image description here

Name2szszsum总是低一行,问题是什么?

1 个答案:

答案 0 :(得分:3)

根据我上面的评论,试试这个。

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