我正在尝试通过 VBA 在第一个工作表的单元格中总结第二个工作表上的表格列。我尝试使用 .Formula 属性推入公式,但单元格留空。
对于这个问题,假设该表位于 Worksheet("Data") 上并命名为“Table1”。有问题的专栏是 [APAC]。xlsheet 在代码的前面定义,并确认按预期工作。考虑到这一点,用于填充我使用的公式的 VBA 如下:
xlsheet.Range("C8").Formula = "=Sum(Table1[APAC])"
手动输入单元格时,上面的公式本身就像一个魅力,但由于某种原因,通过 VBA 这样做是不被接受的。
我什至尝试使用 .FormulaR1C1 属性,但收到了相同的结果。
我希望通过使用表格,我可以轻松地引用整个表格列进行计算,以通过 VBA 汇总存储在其中的数据,但它似乎并不喜欢它。是否有其他人遇到此错误或有任何解决方法/解决方案?提前致谢!
有一件事我忘了提,它可能是一件大事。我正在从 MS Access 2010 生成我的数据并使用 MS Access VBA 来格式化 Excel 输出。在大多数情况下,Excel 中使用的 VBA 将是相同的,除了后期绑定 2010 Excel 应用程序。
更多来自 MS Access 到 MS Excel 的代码......无法全部显示,因为 Access 在导出到 Excel 之前先做了很多工作,这太过分了:
'Filename is the string with the link to the file
Set xlbook = GetObject(filename)
'Disable Screen Updating until all editing is complete
'xlApp.ScreenUpdating = False
'Make sure excel is visibe on the screen
xlApp.Visible = True
xlbook.Windows(1).Visible = True
'Rename the existing worksheet to Data
xlbook.Worksheets(1).Name = "Data"
'Set populated data on Data worksheet as a Table
With xlbook.Worksheets("Data")
'Get the Last Row and Column to determine the final populated cell to be included in the Table
lastrow = .Cells(.Rows.Count, "A").End(xlUp).Row
lastcol = .Cells(1, .Columns.Count).End(xlToLeft).Column
'Set the populated data as Table1
.ListObjects.Add(xlSrcRange, .Range(.Cells(1, 1), .Cells(lastrow, lastcol)), , xlYes).Name = "Table1"
End With
'Add a new worksheet
With xlbook
.Worksheets.Add .Worksheets(.Worksheets.Count), , 1
.Worksheets(1).Name = "Reports"
End With
Set xlsheet = xlbook.Worksheets("Reports")
With xlsheet
With .Range("A1:A5")
.Font.Bold = True
.Font.Size = 14
.HorizontalAlignment = xlRight
End With
.Range("B1:B5").Font.Size = 14
.Range("A1").FormulaR1C1 = "Report:"
.Range("B1").FormulaR1C1 = rpt
.Range("A2").FormulaR1C1 = "Region:"
.Range("B2").FormulaR1C1 = region
.Range("A3").FormulaR1C1 = "System:"
.Range("B3").FormulaR1C1 = Sys
.Range("A4").FormulaR1C1 = "Program/Funding Source:"
.Range("B4").FormulaR1C1 = prog
If rpt = "Request Delivered" Or rpt = "Requests Received" Then
.Range("A5").FormulaR1C1 = "Year:"
.Range("B5").FormulaR1C1 = Yr
Else
.Rows(5).EntireRow.Delete 'Not needed for the other reports
End If
.Columns(1).AutoFit
End With
'Define the current sheet in the workbook as xlSheet
Set xlsheet = xlbook.Worksheets(1)
'**********************************************************************************************************************************
'Use Select case to Go to appropriate formatting code
Select Case rpt
Case "Approved for Target Staging", "In Process"
With xlsheet
.Range("B:B").ColumnWidth = 30
With .Range("B7:C7")
.Font.Size = 12
.Font.Bold = True
.HorizontalAlignment = xlCenter
.Interior.ColorIndex = RGB(191, 191, 191)
End With
With .Range("B7")
.Select
.FormulaR1C1 = rpt
.Offset(1, 0).FormulaR1C1 = "APAC"
.Offset(1, 1).Formula = "=Sum(Table1([APAC])" '<--- HERE IS ONE OF THEM
.Offset(2, 0).FormulaR1C1 = "EMEA"
.Offset(2, 1).Formula = "=Sum(Table1([EMEA])" '<-- ANOTHER
.Offset(3, 0).FormulaR1C1 = "LATAM"
.Offset(3, 1).Formula = "=Sum(Table1([LATAM])" '<-- ETC.
.Offset(4, 0).FormulaR1C1 = "NAM"
.Offset(4, 1).Formula = "=Sum(Table1([NAM])" '<-- ETC.
.Offset(5, 0).FormulaR1C1 = "Global"
.Offset(5, 1).Formula = "=Sum(Table1([GLOBAL])" '<-- ETC.
.Offset(6, 0).FormulaR1C1 = "Total"
.Offset(6, 1).Formula = "=Sum(C8:C12)"
End With
End With
Case "Request Delivered", "Requests Received"
End Select