2016-02-24 166 views
0

我在使用VB6構建報告時遇到了一些問題。基本上,我正在構建一個動態報告,其中標題和2列(客戶端,學生)從記錄集中填充。正如您在圖片中看到的,在我的標題末尾,我添加了一個TOTAL標題,下面有客戶和學生。我試圖讓每個專欄中的所有客戶總數達到TOTAL,與學生一樣。列數(UCLA,SDU,SCCU)可能會有所不同,所以我試圖讓它變成動態的。基本上從A開始,然後是B,然後是C,D和NONE。有任何想法嗎? enter image description here 編輯:我請從SQL SERVER的SHORT LABEL和填充直到g_RS3是空Excel報告 - 公式VBA

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和填充數據在客戶端和STUDENT列。我擁有的循環相當長。但用戶只會查看摘錄。一旦它的產生取決於他們,他們用它做什麼。該應用程序爲他們提供了添加SHORTLABEL的選項,一旦它們生成,SHORTLABEL需要在提取中顯示。

+0

哪一個VBA或VB6? – 0m3r

+0

[SUMIFS函數](https://support.office.com/en-us/article/SUMIFS-function-C9E748F5-7EA7-455D-9406-611CEBCE642B)將執行此操作;例如'= sumifs($ b4:$ g4,$ b $ 3:$ g $ 3,$ h $ 3)''。如果必須在VBA中執行計算或[Range.Formula屬性](https:/ /),請使用[WorksheetFunction對象](https://msdn.microsoft.com/en-us/library/office/ff834434.aspx) /msdn.microsoft.com/en-us/library/office/ff838835.aspx)如果你想保留公式。 – Jeeped

+1

順便說一句,你將不得不決定是否使用'CLIENT'或'CLIENTS'。不應該有一個標準的多個版本。 – Jeeped

回答

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列(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方法。這將擴展返回的列,並允許您使用字段計數來確定範圍。

+0

我必須從3列中取得客戶數,並將其置於TOTAL的客戶端,與學生相同。讓我這樣做。此外,我需要使這種動態,基本上如果用戶添加另一個HEADING,這個Excel提取將自動添加它,所以我不能指定哪一列開始。我只能確定TOTAL中的ROW總是與報告格式相同。 COLUMN將取決於有多少HEADINGS。 – FatBoySlim7

+0

回覆:*'讓我做臨時工*' - 你甚至在問之前試過嗎?至於動態擴展a)它將取決於用戶如何放置新列,b)您應該更擔心它結束的位置,而不是它開始的位置。在我看來,它始終始於B列。c)我認爲數據來自記錄集,而不是來自用戶隨意添加新列的任何地方。 – Jeeped

+0

我一直在玩它。用戶將其輸入到應用程序中。它被保存在SQL SERVER中。我從那裏選擇數據並在Excel中創建提取。摘錄僅用於查看目的(供用戶使用)。它始終在同一地方開始,結尾欄是問題,因爲它可能會有所不同。我編輯了這個問題,告訴你我是如何做到的。 – FatBoySlim7