Используйте lRow
в качестве ссылки на строку параметра .Cells()
. Попробуйте ниже подпункт.
Private Sub cmdAdd_Click()
'Copy input values to sheet.
Dim rw As Integer
Dim ws As Worksheet
Set ws = Worksheets("Sheet1")
lRow = ws.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
With ws
.Cells(lRow, 3) = Me.txtDate
.Cells(lRow, 4) = Me.cboType
.Cells(lRow, 5) = Me.cboDesciption
.Cells(lRow, 6) = Me.txtIncomeAmount
.Cells(lRow, 7) = Me.txtExpensesAmount
.Cells(lRow, 8) = Me.txtComment
End With
'Clear Input Controls.
Me.txtDate = ""
Me.cboType = ""
Me.cboDesciption = ""
Me.txtIncomeAmount = ""
Me.txtExpensesAmount = ""
Me.txtComment = ""
End Sub
# EIDT
В случае таблицы вам нужно найти последнюю пустую ячейку конкретного столбца. В соответствии с вложением вашего файла вам нужно найти последнюю пустую ячейку B Column
означает Date
столбец. Поэтому используйте приведенные ниже коды для работы с вашим листом.
Private Sub cmdAdd_Click()
'Copy input values to sheet.
Dim rw As Integer
Dim ws As Worksheet
Dim lrow As Long
Dim LO As ListObject
Dim LEO As Range
Set ws = Worksheets("2020_Data")
Set LO = ws.ListObjects("Table2")
'lrow = ws.Cells(Rows.Count, "B").End(xlUp).Offset(1, 0).Row
With LO.Range.Columns(2)
Set LEO = .Find(what:="", after:=.Cells(1), LookIn:=xlValues, _
searchorder:=xlByRows, searchdirection:=xlNext)
If Not LEO Is Nothing Then
lrow = LEO.Row
' MsgBox LEO.Row 'First empty row at column B
End If
End With
'rw = ws.Cells.Find(What:="*", SearchOrder:=xlRows, SearchDirection:=xlPrevious, LookIn:=xlValues).Row + 1
With ws
.Cells(lrow, "B") = Me.txtDate
.Cells(lrow, "E") = Me.cboType
.Cells(lrow, "F") = Me.cboDesciption
.Cells(lrow, "G") = Me.txtIncomeAmount
.Cells(lrow, "H") = Me.txtExpensesAmount
.Cells(lrow, "I") = Me.txtComment
End With
'Clear Input Controls.
Me.txtDate = ""
Me.cboType = ""
Me.cboDesciption = ""
Me.txtIncomeAmount = ""
Me.txtExpensesAmount = ""
Me.txtComment = ""
End Sub