首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何修复application.counta公式

如何修复application.counta公式
EN

Stack Overflow用户
提问于 2020-04-20 05:15:31
回答 1查看 43关注 0票数 0

我正在编写一个用户表单,以便将数据输入到excel电子表格中,并且我在使用counta命令注册空行时遇到了问题。

我是VBA的新手,并尝试在网上寻找替代方案,但没有太多成功,老实说,当我阅读一些代码时,我不知道我在看什么。

请参阅下面的代码,对于某些上下文,它用于房间管理,跟踪客人的到达和离开日期,房间价格,以及是否有两个房间相邻使用。由于某些原因,数据输入从第25行开始,我终生都不明白为什么要注册的单元格上方有25个空白行:

代码语言:javascript
运行
复制
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```
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61311719

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档