我如何修复 application.counta 公式

How do i fix application.counta formula

我正在编写一个用户表单以将数据输入 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```



函数 COUNTA() returns 给定范围内非空白单元格的数量。

我假设您想将数据粘贴到 sheet 中的第一个空行,因此您需要最后一个非空行的编号 (+1) 来粘贴数据。试试这个:

emptyRow = Cells(Rows.Count, "B").End(xlUp).Row + 1

这里有更多关于如何在 sheet 中查找最后一行/列的示例。 https://www.thespreadsheetguru.com/blog/2014/7/7/5-different-ways-to-find-the-last-row-or-last-column-using-vba