VBA 用公式填充多个工作表上的单元格

标签 vba excel loops for-loop excel-formula

我正在尝试为工作簿的每个工作表中的每个单元格设置公式。我需要将 2 个公式分布在 2 个不同的单元格范围内。我在 Variant 中定义了 strFormula 1 和 2。

对于公式1我希望分发到A列到AJ列下的单元格

对于公式 2 我希望分发到 AK 到 AR 列下的单元格

我在运行循环后收到以下错误,在定义 strFormulas(2) 时收到语法错误。需要一些帮助来解决这些问题。

谢谢!

Run time error 1004, Application-defined or object-defined error

 Dim w As Long
 Dim strFormulas(1 To 2) As Variant

 For w = 2 To ActiveWorkbook.Worksheets.Count
    With ActiveWorkbook.Worksheets(w)

        strFormulas(1) = "=IF(ISBLANK('Sheet 1'!A4),"",'Sheet 1'!A4)"
        'strFormulas(2) has a syntax error.
        strFormulas(2) = "=IF('Sheet 2'!N1838="S","S","")"

        .Range("A2:AJ2").Formula = strFormulas(1)
        .Range("A2:AJ2000").FillDown
        .Range("AK2:AR2").Formula = strFormulas(2)
        .Range("AK2:AR2000").FillDown

    End With
Next w

最佳答案

带引号的字符串中的引号需要加倍。 "" 的替代项是 text(,),因此您不需要 """"S 是 ascii 83。

strFormulas(1) = "=IF(ISBLANK('Sheet 1'!A4),"""",'Sheet 1'!B4)"
strFormulas(1) = "=IF(ISBLANK('Sheet 1'!A4), text(,), 'Sheet 1'!B4)"

strFormulas(2) = "=IF('Sheet 2'!N1838=""S"",""S"","""")"
strFormulas(2) = "=IF('Sheet 2'!N1838=char(83), char(83), text(,))"

关于VBA 用公式填充多个工作表上的单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49839313/

相关文章:

vba - 为 MS Access 制作 QRCode ActiveX 控件 : Control Source Property

excel - 根据单元格中的十六进制值设置自动颜色背景?

python - Pandas 到 Excel(合并标题列)

javascript - 创建循环 SetTimeOut Jquery 数组

vba - Excel - 复制条件格式、删除规则、保留格式

python - 使用 Python (pywin32) 或 VBA 更改 Excel 中的 Microsoft Query

php - Excel php图表生成

javascript - 为什么我的函数调用应该由 setTimeout 调度立即执行?

python - 在python中每次只比较多个列表一次

excel - VBA "Application defined or object defined error"