How do I fix this compile error - Expected end of statement? Inputting a formula into a cell with VBA that passes a VBA variable into that formula

ATCEO

I've got a variable that adds 3 to a number that a user inputs on a userform. Then, I create a table with that many rows on another sheet. Then, I want a cell on that sheet to count the cells that are in the table and tell the user. I want to do this in VBA because this Sub generates a new sheet/table each time it's run so I can't just enter the formula in the cell and keep it there.

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

This is just the part of the code that's returning the error. The rest of the code (not shown) creates a table from the A:D column with the amount of rows that the Qnum variable has.

What am I missing here?

Stuart

You need an & after Qnum

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

VBA how to copy the formula and formatting of a cell quickly?

How can I insert variable into formula in VBA

Formula with quotes into cell using VBA throws error

How to enter a formula in a cell using vba

Display formula in cell vba

How do I write VBA code such that my formula returns the value in the selected cell?

Excel VBA Formula1 compile error

VBA Compile Error: "Expected: end of statement" - (Working between different sheets)

Excel VBA Error 1004 in cell formula replacement

How do I add / pull data from a cell using a variable and into a formula in VBA?

How do I fix this formula?

Excel 2010 formula, How do I write this formula? (VBA)

How to disable the "compile error expected end of statement" from showing in vba

VBA input formula into cell error

How to put a formula into a cell by Excel VBA?

Excel VBA error: "Compile error: Expected: end of statement"

VBA VLOOKUP Formula Compile error

Expected: end of statement error -- Coding a Volatile Formula

Concatenate variable at end of formula as opposed to within VBA

VBA : How do I insert VLookup formula (instead of the values) in a cell?

How to fix the error in my Browse for file & Copy Paste Formula for VBA

How do I replace a formula to stay on a specific cell in vba when the sheet it references updates

assign IF(AND formula to the cell in VBA

VBA - Show Formula in cell when variable value

How can i fix VBA Copy and Past Formula Error

How to concat the formula in all reference cell into one formula by VBA?

Expected End of Statement Error When Inserting Formula into Excel with VBA

How do i fix Outlook VBA Compile Error Type Mismatch

Syntax Error trying to write a formula into a cell with VBA