Excel VBA-存储值以便稍后显示

时间:2013-05-31 17:10:50

标签: excel vba excel-vba

我正在寻找的是存储图表的值。我们有两个相关的数据列:A有名字,B有数量。我需要做的是将具有相同名称的金额相加,拉出这些金额中的前十名,并将它们显示在图表中。这是可能的,还是我必须将前十名存储在某种表格中?

谢谢!

1 个答案:

答案 0 :(得分:0)

您需要将所有值放在一个表中,以便图表来源。

如果您不希望基本用户看到或更改第二组数据,您可以添加另一个工作表,只复制所需的10行,并将图表源设置为该工作表。在新工作表中,您可以在VBA编辑器中将其标记为“Visible = xlSheetVeryHidden”,这基本上使其仅在VBA中可见。

是否可以,从脚本是的,您可以在VBA脚本中自己完成这些并将它们写入目标表。

以下是一些关于在Excel中循环播放工作表的示例代码(来自我举办的培训课程)

Sub LoopingThroughSheetContents()

    ' This is a comment in Visual Basic, any text after a single-quote character is ignored.
    ' Explain PURPOSE and OBJECTIVE with comments DO NOT explain the obvious it's not useful later on.

    ' DIM statements define variables that are typically used inside a sub-routine or function (both called a method)
    ' Variables can be passed to different methods to break up code into re-usable chunks and to simplify or generalize operation.

    Dim Sh As Worksheet ' used to hold reference to the sheet that is being worked on.
    Set Sh = Application.ActiveSheet 'complex objects are assigned with the "Set" statement.
        ' basic data types such as Long, Date, String, Integer, Boolean are simply assigned and read with "="
        ' complex ones can have a default property that returns a value instead of the object itself.
        ' SET indicates to assign the object and not the default property value.

    Dim iR As Long ' Variable used to hold the CURRENT row during the loop
    Dim iC As Long ' Variable used to hold the CURRENT Column during the loop

    Dim Buffer As String ' used to buffer the cell values for test display.

    ' Excel provides Range objects to work with depending on the scenario.
    ' A Range is equivalent to a users selection in the Excel GUI (Graphical User Interface)
    ' a built in Range Property of all WorkSheets is the "UsedRange"
    ' UsedRange refers to the smallest rectangluar selection that incudes all data on a sheet normally starting from 1,1 (A1)

    Dim TempCellValue As String

    For iR = 2 To Sh.UsedRange.Rows.Count    ' causes the code inside the "For Next" to run once
        ' for each number from 1 to the last row on the sheet based on excels sheet data range that is considered USED.
        ' each loop iR will increment by 1. Eg. 1, 2, 3, 4 ... exit
        ' iR in the first loop is 1

        For iC = 1 To Sh.UsedRange.Columns.Count  ' another loop inside the ROW loop.
            ' used to access each column in this case.
            ' The column loop will start, Loop multiple times, and End once for each row because it is INSIDE the ROW loop!
            ' If there are 3 rows and 5 columns the block of code will execute 15 times.

             ' get the value property of the range of cells requested basically Row iR and Column iC
             ' assigning it to the temporary variable TempCellValue
            TempCellValue = Sh.Cells(iR, iC)

            Buffer = Buffer & TempCellValue & vbTab ' append the value to a buffer with a trailing TAB character.

        Next iC 'the end of the column loop.

        Buffer = Buffer & vbCrLf ' after each column is processed add an ENTER character to the buffer to End the Rows line output

        ' the end marker for the loop for each ROW ...
    Next iR ' the end of the loop, code will return to the corresponding "FOR" line.
        'with iR incremented to the next number until it is past the desired end point.

    MsgBox Buffer

End Sub