VBA插入SUMIF函数报错:Expected End Of Statement 求助
Hey there! Let's break down why your code is hitting that "Expected End Of Statement" error and fix it up.
First, the main issue is how you're handling the double quotes in your SUMIF formula. In VBA, when you want to include a double quote inside a string literal, you have to use two consecutive double quotes to escape it. Your original code uses a single quote around <0, which breaks the string syntax and confuses the compiler.
On top of that, you had an extra Cells keyword before your cell reference — that's another small syntax mistake that would cause issues once you fixed the quote problem.
Here's the corrected code:
Dim ACHRowCount As Integer ACHRowCount = Range("Q" & Rows.Count).End(xlUp).Row 'ACH CREDIT TOTALS Cells(ACHRowCount, 18).Offset(2, 0).Formula = "=SUMIF(S3:S" & ACHRowCount & ", ""<0"")"
Let me walk through the fixes:
- Changed
<0from being wrapped in single quotes to""<0""— this tells VBA to include a single double quote around<0in the final formula. - Removed the extra
Cellsat the start of the formula assignment line to properly reference the target cell.
A quick reminder for future VBA formula work: anytime you need a double quote in the formula that gets inserted into a cell, you have to write two double quotes in your VBA code. That's the standard way to escape quotes in VBA string literals.
内容的提问来源于stack exchange,提问作者alewis9

