-1

私が探しているのは、グラフの値を保存することです。関連するデータ列が 2 つあります。A には Name、B には Amount があります。私がする必要があるのは、同じ名前を持つ金額を合計し、それらの合計の上位 10 を引き出して、それらをグラフに表示することです。これは可能ですか、それともトップ 10 を何らかのテーブルに保存する必要がありますか?

ありがとう!

4

1 に答える 1

0

チャートのソースとなる 1 つのテーブルにすべての値を入力する必要があります。

この 2 番目のデータ セットを基本ユーザーが表示したり変更したりしたくない場合は、必要な 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
于 2013-05-31T17:46:49.737 に答える