아래 코드는 워크 시트에서 모든 고유 스테이션 이름을 검색하고 Stations()
문자열 배열에 넣을 수있게합니다.
Dim TopRowOfData As Integer
Dim StationNameColumn As Integer
Dim Stations() As String
Sub GetUniqueChartNames()
Dim rngTmp As Range
Dim outRange As Range
'Select the first data cell of the Station name column.
Cells(TopRowOfData, StationNameColumn).Select
'Select the rest of the data in the column.
Range(Selection, Selection.End(xlDown)).Select
'Assign this data to a range variable.
Set rngTmp = Selection
'Find a row that occurs below the area of the range data. This will be used
'to paste the filtered values into temporarily.
outRow = rngTmp.Row + rngTmp.Rows.Count + 10 '10 is arbitrary, could be any number.
Set outRange = Cells(outRow, 1)
'Filter the data values by unique values and paste the results into the outRange area.
rngTmp.AdvancedFilter Action:=xlFilterCopy, CopyToRange:=outRange, Unique:=True
'Get the output results of the filter operation and store them in a range object.
'outRow contains the heading of the filtered column. outRow + 1 is where the data starts.
Cells(outRow + 1, 1).Select
Range(Selection, Selection.End(xlDown)).Select
Dim rngFinal As Range
Set rngFinal = Selection
'Add the output results into the Stations array.
ReDim Stations(rngFinal.Rows.Count - 1)
Dim i As Integer
For i = 0 To rngFinal.Rows.Count - 1
Stations(i) = Cells(rngFinal.Row + i, rngFinal.Column).Value
Next
'Delete the temporary range.
rngFinal.Clear
End Sub
TopRowOfData
변수가 단지 즉, 데이터가 시작되는 맨 위의 행이 무엇인지 코드를 이야기하는 정수입니다. StationNameColumn
은 스테이션 이름 (A = 1, B = 2 등)을 포함하는 열의 수입니다.
일단 스테이션 이름의 배열이 있으면 스테이션 이름 열을 내려 가서 관련된 모든 데이터 값을 검색 할 수 있습니다 배열의 각 항목과 함께 그런 다음 해당 데이터를 기반으로 개별 차트를 만듭니다.
또는 스테이션 이름 열의 값을 단계별로 검색하고 현재 스테이션 이름과 관련된 첫 번째 및 마지막 행의 주소를 검색 한 다음 해당 데이터를 기반으로 차트를 만들 수 있습니다. 아래의 코드는 데이터가 스테이션 이름으로 정렬되었다고 가정하여 모든 동일한 스테이션 이름이 함께 그룹화되도록합니다.
Sub FindRowsAssociatedWithStationName()
Dim i As Integer
Dim j As Integer
Dim rng As Range
'Temp variables to store the first and last row numbers.
Dim first As Integer
Dim last As Integer
'Loop through all of the station names.
For i = 0 To UBound(Stations)
'Select the first data cell of the station names column.
Cells(TopRowOfData, StationNameColumn).Select
'Select the rest of the data in the column.
Range(Selection, Selection.End(xlDown)).Select
'Assign this data to a range variable.
Set rng = Selection
'Initialize both of the row number variables to 0.
first = 0
last = 0
'Loop through all the data rows.
For j = 0 To rng.Rows.Count - 1
If Cells(rng.Row + j, StationNameColumn).Value = Stations(i) Then
'Set the first variable.
If first = 0 Then
first = rng.Row + j
End If
'Set the last variable.
last = rng.Row + j
End If
Next
'Call a method to create the actual charts, passing in the first and last row associated with the current Station name.
Call CreateChart(first, last)
Next
End Sub
코드는 다음 해당 행에 해당하는 데이터를 검색 할과 성 (행)의 값을 사용할 수있는 실제 차트를 만들 수 있습니다.
이 질문에 대한 답이 도움이 될 경우 녹색 눈금 표시를 클릭하여 대답이 수락되었음을 나타내야합니다. [이 봐] (http://meta.stackexchange.com/a/5235/182513) – psubsee2003