我有以下利用Excel Interop调用和使用Excel的方法:
Dim ExcelTestSetOutput, Sheet1TestSetOutput
ExcelTestSetOutput = CreateObject(“Excel.Application”) ' Open Excel
ExcelTestSetOutput.WorkBooks.Add() ' Add a new workbook
'Get the first worksheet.
Sheet1TestSetOutput = ExcelTestSetOutput.ActiveSheet
Sheet1TestSetOutput.Cells(RowTestSetOutput, 1).Value = "ID" ' A
Sheet1TestSetOutput.Cells(RowTestSetOutput, 2).Value = "Parent Path" ' B
Sheet1TestSetOutput.Cells(RowTestSetOutput, 3).Value = "Name" ' C
Sheet1TestSetOutput.Cells(RowTestSetOutput, 4).Value = "Last Modified" ' D
Sheet1TestSetOutput.Cells(RowTestSetOutput, 5).Value = "Script Size" ' E
Dim n
For n = 1 To RecSetTestSet.RecordCount
Sheet1TestSetOutput.Cells(RowTestSetOutput + 1, 1).Value = RecSetTestSet.FieldValue(0)
Sheet1TestSetOutput.Cells(RowTestSetOutput + 1, 2).Value = RecSetTestSet.FieldValue(1)
Sheet1TestSetOutput.Cells(RowTestSetOutput + 1, 3).Value = RecSetTestSet.FieldValue(2)
Sheet1TestSetOutput.Cells(RowTestSetOutput + 1, 4).Value = RecSetTestSet.FieldValue(3)
Sheet1TestSetOutput.Cells(RowTestSetOutput + 1, 5).Value = RecSetTestSet.FieldValue(4)
RowTestSetOutput = RowTestSetOutput + 1
RecSetTestSet.Next()
Next
Dim eTotal As Long
' eTotal = Application.WorksheetFunction.Sum(columns((rReportData.column))
' eTotal = SUM(Sheet1TestSetOutput.Range("E2:E"))
' eTotal = ExcelTestSetOutput.ActiveSheet.Sum("E2:E")
' eTotal = Sheet1TestSetOutput.Range("E:E").FormulaR1C1("sum(R2E1:R100E1)")
除了我无法获得eTotal的值之外,其他所有方法都在工作。
eTotal需要对E列中的所有值求和。E列的最后一行号可能会有所不同。
我已注释掉4行代码,在这里我尝试对E列求和以将值转换为eTotal变量,但是错误消息指出该功能在Worksheeet下不受支持。希望得到您的反馈。
答案 0 :(得分:1)
eTotal = ExcelTestSetOutput.Evaluate("=SUM(E:E)")