将公式粘贴到 Excel 时,VBS 中出现错误代码 800A03EC

问题描述 投票:0回答:1

我有一个 XLSX 模板,我将其拖到 VBS 脚本上。 VBS 脚本从一个选项卡(“审阅者列表”)获取审阅者姓名,为每个审阅者姓名创建新选项卡,然后将合并公式(引用这些新选项卡中的单元格)写入另一选项卡(“审阅模板”)的单元格中。编写这些公式时(带有

objTemplate.Range("XX").Formula
的行),我收到错误代码 800A03EC。

令我困惑的是,如果我在公式开头插入单引号并粘贴,则不会出现错误,然后进入输出文件并删除引号,公式就可以正常工作。

Option Explicit

Dim objFileSystemObject, strFile
Set objFileSystemObject = CreateObject("Scripting.FileSystemObject")
strFile = objFileSystemObject.GetAbsolutePathName(Wscript.Arguments.Item(0))

Dim objExcel, objWorkbook
Set objExcel = CreateObject("Excel.Application")
Set objWorkbook = objExcel.Workbooks.Open(strFile)
objExcel.Visible = True

Dim objReviewers, objTemplate, objExport
Set objReviewers = objWorkbook.Sheets("Reviewers List")
Set objTemplate = objWorkbook.Sheets("Review Template")

' #########################################################################
' Get array of reviewer names so we can create their individual sheets and 
' write the consolidation formulas later.
Dim intIndex, intLastIndex, intRow
Dim arrNames(16)
intIndex = 0
For intRow = 2 To 17
    If objReviewers.Cells(intRow, 2).Value <> "" Then
        arrNames(intIndex) = objReviewers.Cells(intRow, 2).Value
        intIndex = intIndex + 1
    End If
Next
intLastIndex = intIndex - 1

' #########################################################################
' Copy the template into new sheets named for each reviewer, and while 
' we're at it, generate the consolidated results formula
Dim objNewSheet, strFormula1, strFormula2, strFormula3, n
strFormula1 = ""
strFormula2 = ""
strFormula3 = ""
n = ""
For intIndex = 0 To intLastIndex
    Call objTemplate.Copy(, objTemplate)
    Set objNewSheet = objWorkbook.Sheets("Review Template (2)")
    objNewSheet.Name = arrNames(intIndex)
    n = "'" & arrNames(intIndex) & "'"
    strFormula1 = strFormula1 & n & "!D2=""no"";"
    strFormula2 = strFormula2 & n & "!D2=""yes"";"
    strFormula3 = strFormula3 & "IF(" & n & "!Q2="""";"""";""" & arrNames(intIndex) & ": "" & " & n & "!Q2);"
    strFormula3 = strFormula3 & "IF(" & n & "!Q2="""";"""";CHAR(10));"
Next

objExcel.DisplayAlerts = False
objExcel.ActiveWorkbook.SaveAs strFile & "___output.xlsx"

strFormula1 = Left(strFormula1, Len(strFormula1) - 1)
strFormula2 = Left(strFormula2, Len(strFormula2) - 1)
strFormula3 = Left(strFormula3, Len(strFormula3) - 1)
objTemplate.Range("D2").Formula = "=IF(OR(" & strFormula1 & ");""no"";IF(OR(" & strFormula2 & ");""yes"";""abs""))"
objTemplate.Range("Q2").Formula = "=CONCAT(" & strFormula3 & ")"

' #########################################################################
' Clean up
objExcel.ActiveWorkbook.Save
objExcel.ActiveWorkbook.Close False
objExcel.DisplayAlerts = True
objExcel.Quit
excel vbscript
1个回答
0
投票

设置公式时,将代码中的 ; 替换为 ,

© www.soinside.com 2019 - 2024. All rights reserved.