Excel报告的行列表不断增加

时间:2018-08-15 18:46:20

标签: excel excel-formula excel-2010

我需要构建一个报告,该报告在A列中列出产品,并在接下来的3列中列出已售数量/销售收入/单位。像这样:

Products  Qty Sold  Revenue  Units on Hand
--------  --------  -------  -------------
A         1         10       3
B         2         20       4
C         1         20       5
D         3         30       6

理想情况下,我想以可以将数据转储到“数据”标签中并在此处填充的方式来设置此报告。我可以使用sumifs公式轻松设置它。

我面临的问题是:如何处理A列中不断扩大的产品列表?

例如,下周有一个新产品'E'有了一定的销量,我需要能够动态添加它。对于我来说,很难像在真实报告中那样手动添加它,将列出数百种产品。我可以设置数据透视表,但我不愿意与观众共享报告。

让我知道这是否有意义,是否需要提供任何其他信息来澄清此问题。

感谢您的帮助,谢谢!

1 个答案:

答案 0 :(得分:0)

如果您不想使用数据透视表,那么我最好的选择是编写一个简短的宏,以确保数据标签中的每个产品都显示在报告标签中。例如:

Option Explicit
Sub CheckDataAndAddToReport()
Dim wsReport As Worksheet, wsData As Worksheet
Dim startRow As Long, endRow As Long, currRow As Long, addRow As Long
Dim missingCt As Long

Set wsReport = ActiveWorkbook.Sheets("Report") 'put the name of your Report sheet here
Set wsData = ActiveWorkbook.Sheets("Data") 'put the name of your Data sheet here

'startRow will be the first row with data in the data worksheet
startRow = 2

'set endRow to the last row with data in the data worksheet
endRow = wsData.Cells(wsData.Rows.Count, 1).End(xlUp).Row

'loop through all the rows of the data worksheet with data
For currRow = startRow To endRow

    'check to see if the item on the current row is found in the reports page
    If WorksheetFunction.CountIf(wsReport.Columns(1), wsData.Cells(currRow, 1).Value) = 0 Then

        'if not, add it and the relevant formulas
        addRow = wsReport.Cells(wsReport.Rows.Count, 1).End(xlUp).Row + 1
        wsReport.Cells(addRow, 1).Formula = wsData.Cells(currRow, 1).Value
        wsReport.Cells(addRow, 2).Formula = "" 'put whatever formulas you need to here
        wsReport.Cells(addRow, 3).Formula = "" 'put whatever formulas you need to here
        wsReport.Cells(addRow, 4).Formula = "" 'put whatever formulas you need to here

        'keep a count of how many were added for reporting purposes
        missingCt = missingCt + 1

    End If

Next currRow

'show a message box with the results
If missingCt > 0 Then
    MsgBox "A total of " & missingCt & " items were found in " & wsData.Name & " that were missing from " & wsReport.Name & " and were added.", _
    vbInformation, _
    "Results"
Else
    MsgBox "No items were found to be missing from " & wsReport.Name & ".", _
    vbInformation, _
    "Results"
End If

End Sub