Excel报告 - 公式VBA

时间:2016-02-24 02:40:31

标签: excel vba excel-vba

我在报告中遇到一些困难,我在VB6中构建。基本上我正在构建一个动态报告,其中标题集和2列(客户,学生)从记录集中填充。正如你在图片中看到的那样,在我的标题末尾,我在下面的客户和学生中添加了一个TOTAL标题。我试图在每个栏目中总共占用所有客户,并将其总结为TOTAL,与学生一样。列数(UCLA,SDU,SCCU)可能会有所不同,所以我试图使其动态化。基本上从A开始,然后是B,然后是C,D和NONE。有任何想法吗? enter image description here 编辑:我从SQL SERVER中选择SHORT LABEL并填充,直到g_RS3为空

Do While Not g_RS3.EOF
    With xlSheet.Cells(xlRow, xlCol)
        .Value = g_RS3("ShortLabel")
            .Offset(1, 0).Value = " Clients "
            .Offset(1, 1).Value = " Students"
                With .Offset(1, 0)
                    .Font.Bold = True
                .Borders.Weight = xlThin
            End With
            With .Offset(1, 1)
                .Font.Bold = True
                .Borders.Weight = xlThin
            End With
            With .Resize(1, 2)
                .Font.Bold = True
                .WrapText = True
                .VerticalAlignment = xlCenter
                .Merge
                .HorizontalAlignment = xlCenter
                .Borders.Weight = xlThin
            End With
    End With
    xlCol = xlCol + 2
    g_RS3.MoveNext
Loop
With xlSheet.Cells(xlRow, xlCol)
    .Value = "TOTAL"
        .Offset(1, 0).Value = "Clients"
        .Offset(1, 1).Value = "Students"
            With .Offset(1, 0)
                .Font.Bold = True
            .Borders.Weight = xlThin
        End With
        With .Offset(1, 1)
            .Font.Bold = True
            .Borders.Weight = xlThin
        End With
        With .Resize(1, 2)
            .Font.Bold = True
            .WrapText = True
            .VerticalAlignment = xlCenter
            .Merge
            .HorizontalAlignment = xlCenter
            .Borders.Weight = xlThin
        End With
End With

然后我从xlrow = 4 xlcol = 2开始,用数据填充CLIENT AND STUDENT列。我拥有的循环很长。但用户只能查看摘录。一旦产生它们,它们对它的作用取决于它们。应用程序为他们提供了添加SHORTLABEL的选项,SHORTLABEL需要在生成后显示在提取中。

1 个答案:

答案 0 :(得分:2)

SUMIF functionSUMIFS function可以轻松执行此操作。

在H4中作为标准公式,

=sumifs($b4:$g4, $b$3:$g$3, h$3)

向右和向下填充。

在VBA中,

with worksheets("Sheet1")
    .range("H4:I8").formula = "=sumifs($b4:$g4, $b$3:$g$3, h$3)"
    'optional revert to values only
    '.range("H4:I8") = .range("H4:I8").values
end with

您必须确定客户/学生范围的范围,但只有一半知道公式的位置(例如H4)。

<强> VBA

我删除了原始代码使用的大量冗余。鉴于您还没有将数据填充到客户端/学生列中,我使用了一种方法,其中Total column(s)总是写在右边,并带有公式。如果设置了另一行,则将覆盖该总计并在右侧创建一个新总计。

Dim xlStartCol As Long
xlStartCol = xlCol
Do While Not g_RS3.EOF
    With xlSheet.Cells(xlRow, xlCol)
        .Resize(1, 2).Merge
        .Value = "TEST" 'g_RS3("ShortLabel")
        .Offset(1, 0).Resize(1, 2) = Array("Clients", "Students")
        .Offset(2, 0).Resize(1, 2).ClearContents
        With .Offset(0, 1)
            .Resize(1, 2).Merge
            .Value = "Total"   'keep writing Total to the right; it will be overwritten if there is another ShortLabel
            .Offset(1, 0).Resize(1, 2) = Array("Clients", "Students")
            .Offset(2, 0).Resize(1, 2).Formula = _
                "=SUMIFS(" & Range(.Parent.Cells(xlRow + 2, xlStartCol), .Parent.Cells(xlRow + 2, xlCol + 1)).Address(0, 1) & Chr(44) & _
                             Range(.Parent.Cells(xlRow + 1, xlStartCol), .Parent.Cells(xlRow + 1, xlCol + 1)).Address(1, 1) & Chr(44) & _
                             .Parent.Cells(xlRow + 1, xlCol - 1).Address(1, 0) & Chr(41)
        End With
        With .Resize(2, 4)
            .Font.Bold = True
            .VerticalAlignment = xlCenter
            .HorizontalAlignment = xlCenter
            .Borders.Weight = xlThin
        End With
    End With
    xlCol = xlCol + 2
    g_RS3.MoveNext
Loop

实际将数据填充到每对列中并知道范围后,只需使用Range.FillDown method填充剩余的公式。

xlCol

我建议删除不相关的部分录制代码。录制的代码非常冗长,妨碍了可读性。您可能还想查看在T-SQL中创建查询的For XML方法。这将扩展返回的列,并允许您使用字段计数来确定范围。