我正在编写一个用户表单,以便将数据输入到excel电子表格中,并且我在使用counta命令注册空行时遇到了问题。
我是VBA的新手,并尝试在网上寻找替代方案,但没有太多成功,老实说,当我阅读一些代码时,我不知道我在看什么。
请参阅下面的代码,对于某些上下文,它用于房间管理,跟踪客人的到达和离开日期,房间价格,以及是否有两个房间相邻使用。由于某些原因,数据输入从第25行开始,我终生都不明白为什么要注册的单元格上方有25个空白行:
Dim emptyRow As Long
emptyRow = Application.WorksheetFunction.CountA(Range("B:B")) + 1
Cells(emptyRow, 2).Value = txtRoomNum.Value
Cells(emptyRow, 3).Value = txtGuestName.Value
Cells(emptyRow, 4).Value = DTPicker1.Value
Cells(emptyRow, 5).Value = DTPicker2.Value
If opInter.Value = True Then
Cells(emptyRow, 7).Value = (Val(txtRev1.Text) + Val(txtRev2.Text) + Val(txtRev3.Text)) * 0.5
Cells(emptyRow, 12).Value = Val(txtChildren.Text) * 0.5
Cells(emptyRow, 13).Value = Val(txtAdults.Text) * 0.5
Else
Cells(emptyRow, 7).Value = (Val(txtRev1.Text) + Val(txtRev2.Text) + Val(txtRev3.Text))
Cells(emptyRow, 12).Value = txtChildren.Value
Cells(emptyRow, 13).Value = txtAdults.Value
End If
Cells(emptyRow, 8).Value = UCase(txtRateCode.Value)
If opRoomOnly.Value = True Then
Cells(emptyRow, 10).Value = "Yes"
Else
Cells(emptyRow, 10).Value = "No"
End If
If opInter = True Then
Cells(emptyRow, 11).Value = "Yes"
Else
Cells(emptyRow, 11).Value = "No"
End If
Unload Me
End Sub```发布于 2020-04-20 06:21:28
函数COUNTA()返回给定范围内不为空的单元格数量。
我假设您想要在工作表的第一个空行上粘贴数据,因此您需要最后一个非空行(+1)的编号来粘贴数据。试试这个:
emptyRow = Cells(Rows.Count, "B").End(xlUp).Row + 1
这里有一些关于如何在工作表中查找最后一行/最后一列的示例。https://www.thespreadsheetguru.com/blog/2014/7/7/5-different-ways-to-find-the-last-row-or-last-column-using-vba
https://stackoverflow.com/questions/61311719
复制相似问题