2012-09-04 2 views
3

좋아, 내 목표는 약 90 가지의 다른 스테이션 이름을 가지고 있기 때문에 차트 만들기 매크로를 만드는 것입니다. 각 역마다 자신의 차트가 필요합니다.Excel 매크로 : 해당 열의 이름이 바뀔 때마다 특정 열을 통해 매크로를 만드는 차트를 반복하는 방법

내가 사용하고자하는 여러 시리즈는 예상 CFS, 시뮬레이션 된 CFS, 스트레스 기간 번호 (번호) 및 스테이션 이름입니다. 번호 값이 x 범위이고, est : CFS와 sim : CFS가 둘 다 오히려 간단한 두 줄을 만드는 내 y 범위가되는 간단한 xy 분산 선 그래프를 만들려고합니다.

내 문제는 간단합니다 : NBAbrara River Station 차트의 차트에서 계열 데이터 수신을 중지하고 Snake River 스테이션 차트에서 다음 데이터를 사용하기 시작하도록 VBA에서 코드를 어떻게 디자인해야합니까? .. 그리고 90 번 차트를 모두 반복 할 때까지.

루프 명과 그 스테이션 이름 열을 읽고 해당 스테이션에 해당하는 모든 행이 그 명령에 속한다는 사실을 이해하고 모든 데이터를 이해할 수 있도록 명령 구문을 작성하는 방법이 난처하지 않습니다. 그것은 하나의 차트를 필요로하면서 역이 바뀌면 다음으로 이동하고 싶습니다.

아래의 이미지가 워크 시트에 데이터를 배치하는 방법을 보여줍니다 :

worksheet

나는 그것이 내가 더 내 질문을하기 위해 제공 할 수있는 더 이상의 정보가있는 경우 이해하기 조금 어렵다면 사과 분명히 더 게시 할 수 있습니다.

+0

이 질문에 대한 답이 도움이 될 경우 녹색 눈금 표시를 클릭하여 대답이 수락되었음을 나타내야합니다. [이 봐] (http://meta.stackexchange.com/a/5235/182513) – psubsee2003

답변

3

아래 코드는 워크 시트에서 모든 고유 스테이션 이름을 검색하고 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 

코드는 다음 해당 행에 해당하는 데이터를 검색 할과 성 (행)의 값을 사용할 수있는 실제 차트를 만들 수 있습니다.

+0

내 의견을 결코 표시되지! 저는 지난 달에 당신이 대답 할 때 당신에게 감사 드리고 싶었던 것을 언급했습니다, 그것은 저를 위해 그 문제를 너무 분명히하는 데 도움이되었습니다! – WATERflowTech

+1

@WATERflowTech, 기꺼이 도와 드리겠습니다. 문제를 해결 한 경우 위/아래 투표 화살표 바로 아래에있는 체크 표시를 클릭하여이 대답을 수락 할 수 있습니다. 그렇게하면 다른 사람들이 즉시 도움이된다는 것을 알게 될 것입니다. – Stewbob

+0

아, 흥미 롭군요! 하아, 시도했지만 지금까지 투표까지도 충분히 높은 순위는 아니 었습니다. 다시 감사드립니다! – WATERflowTech

관련 문제