如何修复此编译错误-应为语句结尾?使用VBA将公式输入到单元格中,VBA会将VBA变量传递到该公式中



我有一个变量,它将用户在用户窗体上输入的数字加3。然后,我在另一张表上创建了一个有那么多行的表。然后,我希望该工作表上的一个单元格对表中的单元格进行计数并告诉用户。我想在VBA中执行此操作,因为此Sub每次运行时都会生成一个新的工作表/表格,所以我不能只在单元格中输入公式并将其保留在那里。

Dim Qnum as Integer 'Defined the integer
Dim ws as Worksheet
Set ws = Sheets("Test")
Qnum = 3 + GenTest.txtNum 'adding three to the number of rows the user has requested
with ws
.Range("C1").Formula = "=Counta(D4:D" & Qnum")" 'This gives a compile error - expected end of statement

这只是代码中返回错误的部分。代码的其余部分(未显示(从a:D列创建一个表,其中包含Qnum变量的行数。

我在这里错过了什么?

您需要一个&Qnum 之后

.Range("C1").Formula = "=Counta(D4:D" & Qnum & ")"

最新更新