2016-01-26 23 views
-2

我一直在尋找無處不在的如何做到這一點,我已經嘗試vlookup,我可以手動合併數據,但是我想要做的是大量在多張紙上垂直排列的風味名稱,在最後一張紙上抓住所有這些風味,合併它們,並給我所需的調味品總量。我也試圖讓這個自動更新,因爲我更新了我需要的數量來製作任何數量的果汁。在excel中列出風味名稱並給出風味的總和

我會嘗試做一個例子,因爲我無法在此處上傳工作表。

一張看起來是這樣的

French Vanilla 20 
Pineapple  16 
Sweet Cream  10 
Mango   8 
Coconut   4 
       0 
Strawberry  30 
NY Cheesecake 20 
       0 
       0 
       0 
       0 

第二板可以像這樣

Passion Fruit 20 
Mango   10 
Coconut   10 
Koolada   2 
       0 
       0 

Blueberry Candy 10 
Banana Split  6 
Strawberry  12 
Mango   12 
       0 
       0 

我想要做的就是最後一張紙拉所有這些名字和數字將匹配的名稱放在一起,然後將匹配名稱中的數字加起來。我不需要將它按字母順序排列,我只需要它自動更新,因爲我在第一張紙上輸入了不同的數量。

回答

0

我已經添加了這個作爲一個單獨的答案,因爲它是從我的第一個答案完全不同。

工作簿中的某處創建一個表名稱列表。例如在A1中放入'Norse Temple 30ML',在A2中放入'Norse Temple 15ML'等。命名這個範圍 - 我在我的例子中命名爲SheetList。

創建這樣的公式: =SUMPRODUCT(SUMIF(INDIRECT("'" & SheetList & "'!B:B"),"Nicotine 100mg",INDIRECT("'" & SheetList & "'!C:C")))

「100毫克尼古丁」可以通過單元格引用來代替,因此您可以拖動。

此方法將要求您在報告頁面上手動創建成分列表,但將爲您完成加法。在更新數字時它也會更新--VBA方法需要手動執行代碼。

如果你想要一個VBA解決方案,我可以創建一個 - 只需要找到時間。

鏈接的地方,我找到了解決方法:http://www.mrexcel.com/forum/excel-questions/119020-sumif-multiple-sheets.html

+0

真棒,感謝您花時間。我很少使用excel來處理簡單的電子表格。我知道它非常強大,現在我在學校學習C++,我認爲我應該讓excel爲我做好工作。 – liftedplane

0

您還沒有說過是否有其他表沒有數據,或者數據出現在哪些列中。我已經假設數據總是在列A:B中,並且工作簿中沒有其他任何內容除了那些牀單。
這段代碼每次都會創建一個新的報告表 - 運行兩次它會崩潰,因爲它會嘗試創建名爲「報告」的第二個表(因此每次刪除報告表)。 有可能有更好的方法來做到這一點,這個代碼可以絕對改善,以滿足您的需求。

Public Sub Report() 

    Dim wrkSht As Worksheet 
    Dim wrkSht_rpt As Worksheet 
    Dim lLastRow As Long 
    Dim lLastRow_rpt As Long 

    'Create a new sheet to put the report on. 
    Set wrkSht_rpt = Worksheets.Add 
    wrkSht_rpt.Name = "Report" 

    'Get the data from each sheet and paste into columns A:B on the report sheet. 
    For Each wrkSht In ThisWorkbook.Worksheets 
     lLastRow = 0 
     With wrkSht 
      If .Name <> wrkSht_rpt.Name Then 

       'If there's no data in column A this will throw an error. 
       On Error Resume Next 
       lLastRow_rpt = wrkSht_rpt.Columns(1).Find("*", , , , xlByColumns, xlPrevious).Row 
       lLastRow = .Columns(1).Find("*", , , , xlByColumns, xlPrevious).Row 
       On Error GoTo 0 

       If lLastRow > 0 Then 
        'There's data on the sheet, so copy it to the report sheet. 
        .Range(.Cells(1, 1), .Cells(lLastRow, 2)).Copy _ 
         Destination:=wrkSht_rpt.Cells(lLastRow_rpt + 1, 1) 
       End If 
      End If 
     End With 
    Next wrkSht 

    lLastRow = 0 
    lLastRow_rpt = 0 

    With wrkSht_rpt 
     'Find last row in column A on report. 
     lLastRow_rpt = .Columns(1).Find("*", , , , xlByColumns, xlPrevious).Row 

     'Copy all flavours to column D. 
     .Range(.Cells(1, 1), .Cells(lLastRow_rpt, 1)).Copy _ 
      Destination:=.Cells(1, 4) 

     'Sort the column, this will place all blanks at the bottom. 
     .Sort.SortFields.Clear 
     .Sort.SortFields.Add Key:=.Range(.Cells(1, 4), .Cells(lLastRow_rpt, 4)), Order:=xlAscending 
     .Sort.SetRange .Range(.Cells(1, 4), .Cells(lLastRow_rpt, 4)) 
     .Sort.Header = xlNo 
     .Sort.SortMethod = xlPinYin 
     .Sort.Apply 

     'Remove duplicates and find new last row in column D. 
     'The new last row is stored in a separate variable as we need to original to use 
     'within the SUMIF formula. 
     .Range(.Cells(1, 4), .Cells(lLastRow_rpt, 4)).RemoveDuplicates 1, xlNo 
     lLastRow = .Columns(4).Find("*", , , , xlByColumns, xlPrevious).Row 

     'Add a formula to add everything up, then copy paste values. 
     With .Range(.Cells(1, 5), .Cells(lLastRow, 5)) 
      .FormulaR1C1 = _ 
       "=SUMIF(R1C1:R" & lLastRow_rpt & "C1,RC4,R1C2:R" & lLastRow_rpt & "C2)" 
      .Copy 
      .PasteSpecial xlPasteValues 
     End With 

     .Range("A1:C1").EntireColumn.Delete 
     .Columns(1).AutoFit 

    End With 

End Sub 
+0

我有E欄風味的名字和列FI金額也有B列數據,我會看看我是否能找出代碼做這個工作。非常感謝你。只要我知道它是否能滿足我需要或不需要,我會盡快回復您。 – liftedplane

+0

好吧,我一直在努力讓你的代碼工作,這是我第一次使用excel VBA,我知道一些C++和XML,它似乎並沒有幫助這裏 這裏是一個鏈接到我的工作手冊https://www.dropbox.com/s/kv9w1k117nvtsr9/juice%20order%20calculation.xlsm?dl = 0 我想要做的是從每一頁的B列中取出所有VG和Nic,並將它們放入最後一頁中的一個位置,然後將E列中的所有口味與其旁邊的數字放在一起F列將排除現有總數,因此我可以輕鬆進行QOH檢查。 目前我正在用手 – liftedplane