通过VBA

时间:2016-09-12 20:04:39

标签: excel-vba sum formula listobject vba

我试图通过VBA在第一张工作表的单元格中的第二张工作表上总结一个表的列。我试图使用.Formula属性推入公式,但是单元格留空。

对于这个问题,假设该表位于工作表(" 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的更多代码...无法显示所有代码,因为在导出到Excel之前,Access首先完成的工作太多了:

        '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

1 个答案:

答案 0 :(得分:-1)

斯科特是天才,注意到我留下了一个额外的(在我的所有公式中。一旦删除,他们的工作完全没有问题。所以下面的代码部分现在有效。感谢斯科特!

With .Range("B7")
                        .Select
                        .FormulaR1C1 = rpt
                        .Offset(1, 0).FormulaR1C1 = "APAC"
                        .Offset(1, 1).FormulaR1C1 = "=Sum(Table1[APAC])"
                        .Offset(1, 1).FormulaR1C1 = .Worksheet("Data").ListObject("Table1").ListColumns("APAC")
                        .Offset(2, 0).FormulaR1C1 = "EMEA"
                        .Offset(2, 1).Formula = "=Sum(Table1[EMEA])"
                        .Offset(3, 0).FormulaR1C1 = "LATAM"
                        .Offset(3, 1).Formula = "=Sum(Table1[LATAM])"
                        .Offset(4, 0).FormulaR1C1 = "NAM"
                        .Offset(4, 1).Formula = "=Sum(Table1[NAM])"
                        .Offset(5, 0).FormulaR1C1 = "Global"
                        .Offset(5, 1).Formula = "=Sum(Table1[GLOBAL])"
                        .Offset(6, 0).FormulaR1C1 = "Total"
                        .Offset(6, 1).Formula = "=Sum(C8:C12)"
                    End With