You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

VBA插入SUMIF函数报错:Expected End Of Statement 求助

解决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 <0 from being wrapped in single quotes to ""<0"" — this tells VBA to include a single double quote around <0 in the final formula.
  • Removed the extra Cells at 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 07:29:15