我正在寻找的是存储图表的值。我们有两个相关的数据列:A 有名称,B 有金额。我需要做的是将具有相同名称的金额相加,拉出这些总和中的前十名,并将它们显示在图表中。这是可能的,还是我必须将前十名存储在某种表格中?
谢谢!
您需要将所有值放在一个表格中,以便图表来源。
如果您不希望基本用户看到或更改第二组数据,您可以添加另一个工作表,您只需将所需的 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