2012-10-29 21 views
0

因此,我需要找到每年每個月的最高價格和最低價格。按月和年篩選excel電子表格

Sub Anaylze() 
Dim datemonthcount As Long 
Dim dateyearcount As Long 
Dim yearcount As Long 
Dim month As Long 
Dim yearstart As Long 
Dim maxprice As Long 
Dim minprice As Long 
Dim rowprice As Long 
Dim percentchange 

Dim counterlong As Integer 

rowprice = 1 
yearstart = 2002 
counterlong = 0 

    Range("A1").Select 
    Do Until IsEmpty(ActiveCell) Or ActiveCell.Value = 0 Or IsNumeric(ActiveCell) = False 
     counterlong = counterlong + 1 'Increments the counter 
     If year(ActiveCell.Text) <> year((ActiveCell.Offset(-1, 0).Text)) Then 
     dateyearcount = dateyearcount + 1 
     End If 
     ActiveCell.Offset(1, 0).Select ' Step down 1 row from present location. 
    Loop 

    For yearcount = 0 To dateyearcount 
    For month = 1 To 12 
    'Range("A1", "B" & counterlong).AutoFilter Field:=1, Criteria1:=">=" & month & "/01/" & yearstart, Operator:=xlAnd, Criteria2:="<=" & month & "/31/" & yearstart 
    maxprice = WorksheetFunction.Max(Range("A1", "B" & counterlong).AutoFilter(Field:=1, Criteria1:=">=" & month & "/01/" & yearstart, Operator:=xlAnd, Criteria2:="<=" & month & "/31/" & yearstart)) 
    minprice = WorksheetFunction.Min(Range("A1", "B" & counterlong).AutoFilter(Field:=1, Criteria1:=">=" & month & "/01/" & yearstart, Operator:=xlAnd, Criteria2:="<=" & month & "/31/" & yearstart)) 
    Cells(rowprice, "g") = maxprice 
    Cells(rowprice, "h") = minprice 
    rowprice = rowprice + 1 
    Next 
    yearstart = yearstart + yearcount 
    Next 

End Sub 

我最大的問題是試圖讓過濾器的工作,我的數據以這種方式被格式化

10/26/2012 61.66 
10/25/2012 61.6
+4

爲什麼不創建一個支點? – nutsch

+0

nutch是現貨。你正在反思這一點。 – ApplePie

回答

0

我想Stepan1010的答案是在正確的方向 但如果你想VBA代碼進行定製,這裏是代碼,供大家參考

Sub testing() 
    Dim dataArray As Variant ' contains DATE,VALUE 
    Dim intArray As Variant ' contains uniqute identifer MM-YYYY,has operation or not (BOOLEAN) 
    Dim resultArray As Variant ' contains the min/max value, and min/max of the previous month 
    Dim min As Double 
    Dim max As Double 
    With ActiveSheet 
     Height = .Cells(.Rows.Count, 1).End(xlUp).Row 
     If Height < 2 Then 
      MsgBox "Are you sure there's only 1 line or 0 line of data and still want to process?" 
      Exit Sub 
     End If 

     'FIRST SORT THE DATA BY date ascending order 
     Application.CutCopyMode = False 
     .Sort.SortFields.Clear 
     .Sort.SortFields.Add Key:=Range("A1:A" & Height), _ 
      SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal 
     With .Sort 
      .SetRange Range("A1:B" & Height) 
      .Header = xlGuess 
      .MatchCase = False 
      .Orientation = xlTopToBottom 
      .SortMethod = xlPinYin 
      .Apply 
     End With 
     ReDim dataArray(1 To Height, 1 To 2) 
     ReDim intArray(1 To Height, 1 To 2) 
     ReDim resultArray(1 To Height, 1 To 3) 
     dataArray = .Range(.Cells(1, 1), .Cells(Height, 2)).Value 
     For i = LBound(intArray, 1) To UBound(intArray, 1) 
      intArray(i, 1) = Month(dataArray(i, 1)) & "-" & Year(dataArray(i, 1)) 
      intArray(i, 2) = False 
     Next i 


     lastMax = 1 
     For i = LBound(dataArray, 1) To UBound(dataArray, 1) 
      If Not intArray(i, 2) Then ' not yet found 
        min = dataArray(i, 2) 
        max = dataArray(i, 2) 
       For j = i To UBound(dataArray, 1) ' loop later elements 
        If intArray(j, 1) = intArray(i, 1) Then ' if same MM-YYYY 
         If dataArray(j, 2) < min Then 
          min = dataArray(j, 2) 
         End If 

         If dataArray(j, 2) > max Then 
          max = dataArray(j, 2) 
         End If 

         intArray(j, 2) = True 'mark as found(operated) 
        End If 
       Next j 

       resultArray(i, 1) = min 
       resultArray(i, 2) = max 


       If i = 1 Then 
        resultArray(i, 3) = 0 
       Else 
        resultArray(i, 3) = (min/lastMax) * 100 
       End If 
       If resultArray(i, 2) > 0 Then 
        lastMax = resultArray(i, 2) 
       End If 
      End If 
     Next i 

     ' YOU CAN CHANGE THE VALUE 3 ,5 to the column you prefer 
     .Range(.Cells(1, 3), .Cells(Height, 5)).Value = resultArray 




    End With 
End Sub 
2

是啊,這樣Nutsch指出的 - 要做到這一點最簡單的方法可能是用數據透視表:

首先您可以將日期分成月,日,年: eg1

然後,你可以調整你的支點TABL e值爲字段設置: eg2

eg3

編輯/添加

如此以來,你改變了你的問題 - 這裏是我會怎麼做你的評論在說什麼:

=MAX(IF($B$2:$B$22=(B2-1),$E$2:$E$22)) 

這是一個可以使用control-alt-delete輸入的數組公式。

eg4

eg5

eg6

然後你就可以總結出這些數據不過你想要的。

+0

我要試試這個,但我希望能夠在vb中做到這一點,以便我可以通過在一個月的最高價格和下個月的最低價格之間進行百分比變化來操縱數據。 –

+0

我建議不要使用VBA來完成這樣的任務。這確實是excel閃耀的地方。 – Stepan1010

+0

@Pradeep Bhat - 看我的編輯。 – Stepan1010