How to print values into next empty row cells?

笑着哭i 提交于 2021-02-11 13:37:57

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!