2013-09-22 23 views
1

我目前正在寫一子找到最大的正或負的價值模式,但我不清楚如何來解決這個問題編程如何從一個給定的角度來看

基本上我有數據值的列的表從「A1」開始

如何編寫一個將通過列檢查以查找從第一個單元格和模式方向開始的最大模式的子?

即。如果A1是2,則A2是5,A3是-2 ...如果A1是-2,A2 -1,A3是-5,A4是-2,則該子應該返回2(連續正2天) 。 A5 -1,A6 2 ...子應該返回-5(連續負5天)

我想要的是以某種方式收集這個數字,但在過程中也保存模式中的最後一行,所以我可以計算平均值,std變化等以存儲到集合

這是檢查模式的代碼.... j是列計數器...我需要弄清楚如何使循環返回直到for循環之前,而不是迭代j變量,然後回落...

但無論如何這裏是檢查圖案子

<i> Sub pattern_recogADR() 

'add back in as parameters 
x As Long 
pat_days As Long 
sht_start As Long 


x = 1 
pat_days = 5 
sht_start = 13 


Dim st As Long 
Dim st_num As Long 
Dim st_end As Long 
Dim count As Long 
Dim patrn As Long 

count = sht_start 

Dim i As Long 

Set pat = New pattern 

For j = 8 To 12 
    st_num = 0 
     If IsNumeric(Cells(count, j).value) Then 
      st_num = count 'sets default pattern to beginning cell value 

       If Cells(st_num, j).value < 0 Then 
        For i = count + 1 To count + 1 + pat_days 
         If IsNumeric(Cells(i, j).value) And Cells(i, j).value < 0 Then 
          st_end = i 
         Else 
          Exit For 
         End If 
        Next i 

         patrn = st_end - st_num 

         tix.arbPnl = patrn 
         '**CONFUSION HERE WANT TO SAVE PATTERN TO AN EXISTING COLLECTION STARTING `  
         'AT THE FIRST ITEM ** 


       ElseIf Cells(st_num, j).value > 0 Then 
        For i = count + 1 To count + 1 + pat_days 
         If IsNumeric(Cells(i, j).value) And Cells(i, j).value < 0 Then 
          st_end = i 
         Else 
          Exit For 
         End If 
        Next i 

         patrn = st_end - st_num 

         TIX.arbPnl = patrn 
         'save to separate class for patterns 


     Else 
      count = count + 1 
     End If 
Next j 




End Sub 

這裏是我以前定義的對象。基本上我想得到這個模式,然後將它添加到這個集合中的相應屬性(?我不知道編碼vocab),這個集合已經被定義了,所以模式與集合中的相應項目相匹配。

Option Explicit 

Public TixCollection As New Collection 

Sub DefineTixCollection() 

Application.ScreenUpdating = False 

Sheets("Input").Activate 
Set TixCollection = Nothing 


Dim tix As clsTix 
Dim i As Long 
Dim last_row As Long 
last_row = Range("A" & Rows.count).End(xlUp).Row 

    'Add tix properties 
    For i = 3 To last_row 
     Set tix = New clsTix 

     'only adds active tickers to collection 
     If Range("A" & i).value = "x" Then 
      'Random data 

      tix.ORD = Range("B" & i).value 
      tix.ADR = Range("C" & i).value 
      tix.ratio = Range("D" & i).value 
      tix.crrncy = Range("E" & i).value 
      tix.hedge_index = Range("F" & i).value 
      tix.hedge_ord = Range("G" & i).value 
      tix.hedge_ratio = Range("H" & i).value 

      ' ADR is the id key 
      TixCollection.Add tix, tix.ADR 
     End If 

    Next i 

' Error Check 
    ' For i = 1 To 5 
    '  'retrieve by collection index 
    '  Debug.Print TixCollection(i).ORD 
    '  Debug.Print TixCollection(5).ADR 
    '  Debug.Print TixCollection(5).ratio 
    '  Debug.Print TixCollection(i).crrncy 
    '  Debug.Print TixCollection(i).hedge_index 
    '  Debug.Print TixCollection(i).hedge_ord 
    '  Debug.Print TixCollection(i).hedge_ratio 
    ' Next i 

End Sub 

任何幫助將非常感謝現在越來越沮喪...啊

+0

只是保持正數或負數的計數的循環,直到它變化。 –

+0

我真的很困惑如何做到這一點......看到我最大的問題不是所有的數據點都是值,所以我想從數字的數字開始計算。此外,我想循環列以及..也是你的建議沒有幫助BC,如果數字是積極或消極的說什麼關於以前的數字。 – googlekid

+0

您可以使用isnumber工作表函數來判斷單元格是否包含數字,如果是,您可以檢查該數值是負數還是負數,並設置布爾值以跟蹤此數值。如果下一個單元格包含一個數字,則檢查它是否與布爾值具有相同的符號(正數或負數),如果它是將數字加1就可以了。如果您顯示迄今爲止已開發的代碼,則可能會有所幫助。 –

回答

0
Sub Button1_Click() 
    Dim patrn() As Long 
    ReDim patrn(0 To 4) 

    Dim count As Long 
    Dim posCount As Integer 
    Dim negCount As Integer 
    Dim sign As Boolean 

    posCount = 0 
    negCount = 0 
    count = 0 

    Dim i As Long 
    Dim j As Integer 
    Dim lastRow As Long 

    For j = 8 To 12 
     lastRow = ActiveSheet.Cells(ActiveSheet.Rows.count, j).End(xlUp).Row 
     For i = 1 To lastRow 
      If IsNumeric(Cells(i, j).Value) Then 
       If count = 0 Then 
        If Cells(i, j).Value > 0 Then 
         sign = True 
         posCount = posCount + 1 
        ElseIf Cells(i, j).Value < 0 Then 
         sign = False 
         negCount = negCount + 1 
        End If 
       ElseIf count > 0 And count <= 4 Then 
        If Cells(i, j).Value > 0 And sign = True Then 
         sign = True 
         posCount = posCount + 1 
        ElseIf Cells(i, j).Value > 0 And sign = False Then 
         sign = True 
         posCount = 1 
        ElseIf Cells(i, j).Value < 0 And sign = True Then 
         sign = False 
         negCount = 1 
        ElseIf Cells(i, j).Value < 0 And sign = False Then 
         sign = False 
         negCount = negCount + 1 
        End If 
       ElseIf count = 5 Then 
        Exit For 
       End If 
       count = count + 1 
      End If 
     Next i 
     If posCount > negCount Then 
      patrn(j - 8) = posCount 
     Else 
      patrn(j - 8) = negCount - (negCount * 2) 
     End If 
     negCount = 0 
     posCount = 0 
     count = 0 
    Next j 

    'Do your other calculations here. 
    For i = LBound(patrn) To UBound(patrn) 
     Debug.Print patrn(i) 
    Next 
End Sub 
相關問題