1

我目前正在写一个子,但我不清楚如何以编程方式解决问题

本质上,我有一张表,其中有一列数据值从“A1”开始

如何编写一个 sub 来检查列以找到从第一个单元格开始的最大模式和模式的方向?

IE。如果 A1 为 2,A2 为 5,A3 为 -2 ... 如果 A1 为 -2,A2 -1,A3 为 -5,A4 为 -2,A5,则子程序应返回 2(连续 2 天为正) -1, A6 2 ... sub 应该返回 -5(连续 5 天负)

我想要的是以某种方式收集这个数字,但在此过程中还要保存模式中的最后一行,以便我可以计算平均值、标准变化等以存储到集合中

这是检查模式的代码.... 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

这是我之前定义对象的地方。基本上我想得到这个模式,然后将它添加到这个集合中已经定义的相应属性(?我不知道编码词汇),所以模式与集合中的相应项目匹配。

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

任何帮助将不胜感激现在感到沮丧......呃

4

1 回答 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
于 2013-09-24T20:41:42.093 回答