问题
I have designed a VBA form to collect data from the user. However, I want to allow the user to fill the form multiple times and each time prints the filled out data into appropriate cells in the Excel sheet, so I want the VBA program to look for the next empty cell in the sheet without having the user to look manually and select it.
Cells(lastRow, 1) = emplName
Cells(lastRow, 2) = depTxt
Cells(lastRow, 3) = emplNo
Cells(lastRow, 4) = mngrName
Cells(lastRow, 5) = ComboBox1.Value
Here for instance, these five values should then print into the first row, if the form is used again, it should find the next empty row to print the values.
Thank you for your time.
回答1:
There are quite a few ways to select/detect last row in Excel with VBA. The best one I usually use:
Function LastRow(wsName As String, Optional columnToCheck As Long = 1) As Long
Dim ws As Worksheet
Set ws = Worksheets(wsName)
LastRow = ws.Cells(ws.Rows.Count, columnToCheck).End(xlUp).Row
End Function
This is how to use it for column "E" (5th column):
someLastRow = LastRow("WorksheetName", 5)
If you have ideas for improvement, feel free to make PR: https://github.com/Vitosh/VBA_personal/blob/master/LastThings.vb
来源:https://stackoverflow.com/questions/59078104/how-to-print-values-into-next-empty-row-cells