2013-05-31 59 views
-1

我在尋找的是存儲圖形的值。我們有兩個相關的數據列:A有名稱,B有金額。我需要做的是總結具有相同名稱的金額,並將這些金額的前十名並顯示在圖表中。這是可能的,還是我必須將前十名存儲在某種表格中?Excel VBA-將值存儲爲稍後顯示

謝謝!

+0

這是很容易與數據透視表/透視榜前10選項來執行,如果您有Excel 2013您可以創建圖表而不需要數據範圍。 –

回答

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 
+0

所以,即使我的原始數據可見,我們仍然需要在某個地方有一個包含計算值的表,即使它是隱藏的? –

+0

是的,圖表會預期具體的來源範圍。這個範圍可以通過計算,VBA代碼或者數據透視表等東西來獲得。我最喜歡自己使用VBA代碼。 – DarrenMB

相關問題