如何编辑VBA集合中的项目值?

时间:2016-02-11 22:52:51

标签: excel macos vba

我正在尝试编辑集合中包含的项的值。但是我不相信我的语法正确。代码不会出错,也不会更改项目的值。

myMonth是一个集合。

myDays是myMonth中包含的项目集合。

myFirstPriority是myDays

集合中的一个项目

thisDay是一个迭代变量,用于遍历myDays Collection的项目。

myMonth.myDays(thisDay).myFirstPriority = "task1"

我的目标是更改myDays Collection中包含的item myFirstPriority的值。 myDays本身就是myMonth中包含的一个项目。

我知道我可以使用字典对象,但是我正在寻找一种利用集合变通方法的解决方案。集合是我第一次从数组中进入类。数组的赋值不适用于集合。

编辑:约翰科尔曼知道他的东西,...错误恢复下一个= P

这是代码:

'Main Procedure

Public Sub PopulateBarData()

    Debug.Print "Begin Workflow"
    Debug.Print " "

Dim pbl As cProcessBarLog
Dim arrBli() As cBarLogItem

Dim i As Integer, j As Integer, endRow As Integer, thisRow As Integer, nextRow As Integer, prevRow As Integer
Dim ws As Worksheet, wsQS As Worksheet

Dim myChartIndex() As Integer
Dim myPrices() As Collection


'Do Something

Set wsQS = ThisWorkbook.Worksheets("Sheet1")

Set ws = ThisWorkbook.Worksheets("Data")
    endRow = ws.Range("A" & Rows.Count).End(xlUp).row

    Call createChartIndex(myChartIndex, ByVal endRow)

Set pbl = New cProcessBarLog
Set pbl.BarLogitems = loadmyPrices(ws, endRow, myChartIndex)

    Call calcPVmetrics(wsQS, pbl, pbl.BarLogitems, endRow) <--- in here

End Sub


'Builds myChartIndex from last row first,
Public Sub createChartIndex(ByRef myChartIndex, ByVal endRow)
ReDim myChartIndex(1 To endRow)
Dim x As Integer, lastrowfirst As Integer
lastrowfirst = endRow
 For x = 1 To endRow
    myChartIndex(x) = lastrowfirst
    lastrowfirst = lastrowfirst - 1
 Next x
End Sub


'Loads myPrice Collection with values from sheet "Data"
'
Public Function loadmyPrices(ByRef ws, ByVal endRow, ByRef myChartIndex) As Collection

Dim dayDate As Date
Dim dayOpen As Currency, dayClose As Currency, dayHigh As Currency, dayLow As Currency
Dim dayPDx As Integer, dayVDx As Integer
Dim DayVol As Long, DayChg As Currency, DayRange As Currency

Dim prevOpen As Currency, prevHigh As Currency, prevLow As Currency
Dim i As Integer, j As Integer, thisRow As Integer, nextRow As Integer, prevRow As Integer

Debug.Print "load myPrices Collection"

Dim clnBlis As Collection               'collectionBarLogItems
Dim bli As cBarLogItem                  
'Put some data in the collection'
Set clnBlis = New Collection

i = endRow

While i >= 2    'go until 2nd row

'Working up the sheet doing whatever

        thisRow = i             'the current row or date under consideration
        nextRow = i - 1      'the row above (next day) since we are working from EOF up to first record
        prevRow = i + 1      'the row below (previous day)

        dayDate = ws.Cells(i, 1).Value                  'Data Sheet Column A
        dayOpen = ws.Cells(i, 2).Value                  'Data Sheet Column B
        dayHigh = ws.Cells(i, 3).Value                  'Data Sheet Column C
        dayLow = ws.Cells(i, 4).Value                   'Data Sheet Column D
        dayClose = ws.Cells(i, 5).Value                 'Data Sheet Column E
        DayVol = ws.Cells(i, 6).Value                     'Data Sheet Column F

            DayChg = dayClose - dayOpen
            DayRange = dayHigh - dayLow

        'myPriceArray(1, i) = thisRow
        Set bli = New cBarLogItem
            bli.RowID = thisRow
            bli.BarID = myChartIndex(i)
            bli.BarDate = dayDate
            bli.BarOpen = dayOpen
            bli.BarClose = dayClose
            bli.DayChg = DayChg
            bli.BarLow = dayLow
            bli.BarHigh = dayHigh
            bli.DayVol = DayVol
            bli.DayRange = DayRange



        clnBlis.Add bli


Debug.Print bli.BarID & "  " & bli.RowID & " " & bli.BarDate & "  " & bli.BarOpen & "  " & bli.BarClose & "  " & bli.DayRange & "  " & bli.PDx

        Set loadmyPrices = clnBlis

    i = i - 1

  Wend
End Function



Public Sub calcPVmetrics(ByRef wsQS, ByRef pbl, ByRef BarLogitems, ByVal endRow)

On Error Resume Next    'stackoverflow divide by zero for first bar looking for prev bar


Dim dayDate As Date
Dim dayOpen As Currency, dayClose As Currency, dayHigh As Currency, dayLow As Currency
Dim DayChg As Currency, DayChgPct As Currency
Dim ldayVol As Long, DayVolChg As Long, DayVolChgPct  As Currency, Vol65DayAvg As Long
Dim prevOpen As Currency, prevClose As Currency, prevHigh As Currency, prevLow As Currency,     prevVol As Long
Dim i As Integer, j As Integer, thisRow As Integer, nextRow As Integer, prevRow As Integer, prevDate As Date



i = endRow - 1        'begin comparisons at 2nd bar

While i >= 2            'go until 2nd row


        thisRow = i             'the current row or date under consideration
        nextRow = i + 1      'the row above (next day) since we are working from EOF up to first record
        prevRow = i - 1      'the row below (previous day)

        'myPriceArray(1, i) = thisRow

        Dim iBarID As Integer, iRowID
        'load variables
        iBarID = pbl.BarLogitems(thisRow).BarID
        iRowID = pbl.BarLogitems(thisRow).RowID
        dayDate = pbl.BarLogitems(thisRow).BarDate
        dayOpen = pbl.BarLogitems(thisRow).BarOpen
        dayHigh = pbl.BarLogitems(thisRow).BarHigh
        dayLow = pbl.BarLogitems(thisRow).BarLow
        dayClose = pbl.BarLogitems(thisRow).BarClose
        ldayVol = pbl.BarLogitems(thisRow).DayVol


        prevDate = pbl.BarLogitems(prevRow).BarDate
        prevOpen = pbl.BarLogitems(prevRow).BarOpen
        prevHigh = pbl.BarLogitems(prevRow).BarHigh
        prevLow = pbl.BarLogitems(prevRow).BarLow
        prevClose = pbl.BarLogitems(prevRow).BarClose
        prevVol = pbl.BarLogitems(prevRow).DayVol



            'determine price direction
            If dayClose > dayOpen Then
            Dim temp As Integer
                'temp = pbl.BarLogitems(thisRow).Item("PDx")
                'pbl.BarLogitems(thisRow).Remove "PDx"
                'pbl.BarLogitems(thisRow).Add Item:=1, Key:="PDx"

                'pbl.BarLogitems(thisRow).Remove "PDx"
                'pbl.BarLogitems(thisRow).Add 1, "PDx"

                pbl.BarLogitems(thisRow).PDx = 1    <-----------does not change value of property PDx. 

                Debug.Print pbl.BarLogitems(thisRow).PDx

                'code needed to assign a value to an item of a collection
                 'within a collection. None of the following assignments
                  'changes the value

            End If
             If dayClose < dayOpen Then
                pbl.BarLogitems(thisRow).PDx = 0

            End If

            'determine volume direction
            If ldayVol > prevVol Then
                pbl.BarLogitems(thisRow).VDx = 1

            Else
            'End If
             'If DayVol < preveVol Then
                pbl.BarLogitems(thisRow).VDx = 0

            End If


        'Open of Bar
        If dayOpen > prevOpen Then

            pbl.BarLogitems(thisRow).HO = 1    ' open to open
            pbl.BarLogitems(thisRow).LO = 0

        End If
        If dayOpen < prevOpen Then

            pbl.BarLogitems(thisRow).LO = 1
            pbl.BarLogitems(thisRow).HO = 0

        End If


        'High of Bar
        If dayHigh > prevHigh Then

            pbl.BarLogitems(thisRow).HH = 1    ' high to high
            pbl.BarLogitems(thisRow).LH = 0

        End If
        If dayHigh < prevHigh Then

            pbl.BarLogitems(thisRow).LH = 1
            pbl.BarLogitems(thisRow).HH = 0

        End If


        'Low of Bar
        If dayLow > prevLow Then

            pbl.BarLogitems(thisRow).HL = 1    ' Low to low
            pbl.BarLogitems(thisRow).LL = 0

        End If
        If dayLow < prevLow Then

            pbl.BarLogitems(thisRow).LL = 1
            pbl.BarLogitems(thisRow).HL = 0

        End If



        'Close of bar
        If dayClose > prevClose Then

                pbl.BarLogitems(thisRow).HC = 1      'close to close
                pbl.BarLogitems(thisRow).LC = 0

        End If
        If dayClose < prevClose Then

                pbl.BarLogitems(thisRow).LC = 1
                pbl.BarLogitems(thisRow).HC = 0

        End If


        'determine day Change percent
        DayChg = dayClose - prevClose
        DayChgPct = (DayChg / prevClose) * 100

         pbl.BarLogitems(thisRow).DayChgPct = DayChgPct


        'determine day Volume Change percent
        DayVolChg = ldayVol - prevVol
        DayVolChgPct = (DayVolChg / prevVol) * 100

            pbl.BarLogitems(thisRow).DayVolChgPct = DayVolChgPct


        'determine AD = ((Close -Low)-(High-Close))/(High-Low) * PeriodVolume
        Dim AccDist As Currency
        Dim MFM As Currency
        Dim ADx As Integer

            MFM = ((dayClose - dayLow) - (dayHigh - dayClose)) / (dayHigh - dayLow)
            AccDist = MFM * ldayVol

            pbl.BarLogitems(thisRow).MFM = MFM

        If MFM > 0 Then
                pbl.BarLogitems(thisRow).ADx = 1
            Else
                pbl.BarLogitems(thisRow).ADx = 0
        End If




        'Determine if day Volume is above or below 65day Vol avg
        Vol65DayAvg = wsQS.Cells(15, 2).Value

        pbl.BarLogitems(thisRow).Vol65DayAvg = Vol65DayAvg

        If ldayVol > Vol65DayAvg Then

                pbl.BarLogitems(thisRow).V65Dx = 1

        End If
        If ldayVol < Vol65DayAvg Then
                pbl.BarLogitems(thisRow).V65Dx = 0
        End If




    i = i - 1

  Wend


End Sub

课程模块(BarLogitem)

Option Explicit

Private pRowID As Long
Private pBarID As Long
Private pSymbol As String
Private pBarDate As Date

Private pBarOpen As Currency, pBarClose As Currency, pBarLow As Currency,       pBarHigh As Currency
Private pDayChg As Currency, pDayChgPct As Currency, pDayRange As Currency
Private pDayVol As Long, pDayVolChg As Currency, pDayVolChgPct As Currency, pMFM  As Currency 'MoneyFlowMultiplier

Private pPDx As Integer, pVDx As Integer, pADx As Integer, pPTrend As Integer, pVTrend As Integer, pVPeriod As Integer
Private pV65Dx As Integer, pVxDx As Integer
Private pPrPeriod As Integer, pPeriod As Integer, pPeriodTape As Integer, pPeriodTraverse As Integer, pPeriodChannel As Integer
Private pHH As Integer, pLH As Integer, pHL As Integer, pLL As Integer
Private pHC As Integer, pHO As Integer, pLC As Integer, pLO As Integer
Private pVolHH As Integer, pVolLH As Integer, pVolAbovePeriodAvg As Integer, pVolBelowPeriodAvg As Integer
Private pVolPaceShort As Integer, pVolPaceLong As Integer, pVolPacePeriodAvg As Integer, pVol65DayAvg As Long
Private pPT1 As Integer, pPT2 As Integer, pPT3 As Integer

'Index & Identifying
'RowID
Public Property Get RowID() As Long
    RowID = pRowID
End Property
Public Property Let RowID(lRowID As Long)
    pRowID = lRowID
End Property

'BarID
Public Property Get BarID() As Long
    BarID = pBarID
End Property
Public Property Let BarID(lBarID As Long)
   pBarID = lBarID
End Property

'Symbol
Public Property Get Symbol() As String
    Symbol = pSymbol
End Property
Public Property Let Symbol(strSymbol As String)
    pSymbol = strSymbol
End Property

'BarDate
Public Property Get BarDate() As Date
    BarDate = pBarDate
End Property
Public Property Let BarDate(dBarDate As Date)
    pBarDate = dBarDate
End Property

'Data Detail
'BarOpen
Public Property Get BarOpen() As Currency
    BarOpen = pBarOpen
End Property
Public Property Let BarOpen(cBarOpen As Currency)
    pBarOpen = cBarOpen
End Property

'BarClose
Public Property Get BarClose() As Currency
    BarClose = pBarClose
End Property
Public Property Let BarClose(cBarClose As Currency)
    pBarClose = cBarClose
End Property

'BarLow
Public Property Get BarLow() As Currency
    BarLow = pBarLow
End Property
Public Property Let BarLow(cBarLow As Currency)
    pBarLow = cBarLow
End Property

'BarHigh
Public Property Get BarHigh() As Currency
    BarHigh = pBarHigh
End Property
Public Property Let BarHigh(cBarHigh As Currency)
    pBarHigh = cBarHigh
End Property

'DayChg
Public Property Get DayChg() As Currency
    DayChg = pDayChg
End Property
Public Property Let DayChg(cDayChg As Currency)
    pDayChg = cDayChg
End Property

'DayChgPct
Public Property Get DayChgPct() As Currency
    DayChgPct = pDayChgPct
End Property
Public Property Let DayChgPct(cDayChgPct As Currency)
    pDayChgPct = cDayChgPct
End Property

'DayRange
Public Property Get DayRange() As Currency
    DayRange = pDayRange
End Property
Public Property Let DayRange(cDayRange As Currency)
    pDayRange = cDayRange
End Property

'DayVol
Public Property Get DayVol() As Long
    DayVol = pDayVol
End Property
Public Property Let DayVol(ldayVol As Long)
    pDayVol = ldayVol
End Property

'DayVolChg
Public Property Get DayVolChg() As Long
    DayVolChg = pDayVolChg
End Property
Public Property Let DayVolChg(lDayVolChg As Long)
    pDayVolChg = lDayVolChg
End Property

'DayVolChgPct
Public Property Get DayVolChgPct() As Currency
    DayVolChgPct = pDayVolChgPct
End Property
Public Property Let DayVolChgPct(cDayVolChgPct As Currency)
    pDayVolChgPct = cDayVolChgPct
End Property

'MFM - Money Flow Multiplier
Public Property Get MFM() As Currency
    MFM = pMFM
End Property
Public Property Let MFM(cMFM As Currency)
    pMFM = cMFM
End Property


'PDx
Public Property Get PDx() As Integer
     PDx = pPDx
End Property
Public Property Let PDx(iPDx As Integer)
     pPDx = iPDx
End Property

'VDx
Public Property Get VDx() As Integer
    VDx = pVDx
End Property
Public Property Let VDx(iVDx As Integer)
    pVDx = iVDx
End Property

'ADx
Public Property Get ADx() As Integer
     ADx = pADx
End Property
Public Property Let ADx(iADx As Integer)
     pADx = iADx
End Property

'V65Dx
Public Property Get V65Dx() As Integer
    V65Dx = pV65Dx
End Property
Public Property Let V65Dx(iV65Dx As Integer)
    pV65Dx = iV65Dx
End Property

'VxDx
Public Property Get VxDx() As Integer
    VxDx = pVxDx
End Property
Public Property Let VxDx(iVxDx As Integer)
    pVxDx = iVxDx
End Property


'PTrend
Public Property Get PTrend() As Integer
    PTrend = pPTrend
End Property
Public Property Let PTrend(iPTrend As Integer)
    pPTrend = iPTrend
End Property

'VTrend
Public Property Get VTrend() As Integer
     VTrend = pVTrend
End Property
Public Property Let VTrend(iVTrend As Integer)
    pVTrend = iVTrend
End Property

'Period
Public Property Get Period() As Integer
    Period = pPeriod
End Property
Public Property Let Period(iPeriod As Integer)
    pPeriod = iPeriod
End Property

'PrPeriod
Public Property Get PrPeriod() As Integer
    PrPeriod = pPrPeriod
End Property
Public Property Let PrPeriod(iPrPeriod As Integer)
    pPrPeriod = iPrPeriod
End Property

'VPeriod
Public Property Get VPeriod() As Integer
    VPeriod = pVPeriod
End Property
Public Property Let VPeriod(iVPeriod As Integer)
    pVPeriod = iVPeriod
End Property

'PeriodTape
Public Property Get PeriodTape() As Integer
    PeriodTape = pPeriodTape
End Property
Public Property Let PeriodTape(iPeriodTape As Integer)
    pPeriodTape = iPeriodTape
End Property

'PeriodTraverse
Public Property Get PeriodTraverse() As Integer
    PeriodTraverse = pPeriodTraverse
End Property
Public Property Let PeriodTraverse(iPeriodTraverse As Integer)
    pPeriodTraverse = iPeriodTraverse
End Property

'PeriodChannel
Public Property Get PeriodChannel() As Integer
    PeriodChannel = pPeriodChannel
End Property
Public Property Let PeriodChannel(iPeriodChannel As Integer)
    pPeriodChannel = iPeriodChannel
End Property


'Price Matrix 1 = yes, 0 = no
'HH
Public Property Get HH() As Integer
    HH = pHH
End Property
Public Property Let HH(iHH As Integer)
    pHH = iHH
End Property

'LH
Public Property Get LH() As Integer
    LH = pLH
End Property
Public Property Let LH(iLH As Integer)
    pLH = iLH
End Property

'HL
Public Property Get HL() As Integer
    HL = pHL
End Property
Public Property Let HL(iHL As Integer)
    pHL = iHL
End Property

'LL
Public Property Get LL() As Integer
    LL = pLL
End Property
Public Property Let LL(iLL As Integer)
    pLL = iLL
End Property


'HO
Public Property Get HO() As Integer
    HO = pHO
End Property
Public Property Let HO(iHO As Integer)
    pHO = iHO
End Property

'LO
Public Property Get LO() As Integer
    LO = pLO
End Property
Public Property Let LO(iLO As Integer)
    pLO = iLO
End Property

'HC
Public Property Get HC() As Integer
    HC = pHC
End Property
Public Property Let HC(iHC As Integer)
    pHC = iHC
End Property

'LC
Public Property Get LC() As Integer
    LC = pLC
End Property
Public Property Let LC(iLC As Integer)
    pLC = iLC
End Property


'Volume Data

'This Bar´s Volume compared to prior Bar
'VolHH
Public Property Get VolHH() As Integer
    VolHH = pVolHH
End Property
Public Property Let VolHH(iVolHH As Integer)
    pVolHH = iVolHH
End Property

'VolLH
Public Property Get VolLH() As Integer
     VolLH = pVolLH
End Property
Public Property Let VolLH(iVolLH As Integer)
     pVolLH = iVolLH
End Property

'VolAbovePeriodAvg
Public Property Get VolAbovePeriodAvg() As Integer
    VolAbovePeriodAvg = pVolAbovePeriodAvg
End Property
Public Property Let VolAbovePeriodAvg(iVolAbovePeriodAvg As Integer)
    pVolAbovePeriodAvg = iVolAbovePeriodAvg
End Property

'VolBelowPeriodAvg
Public Property Get VolBelowPeriodAvg() As Integer
    VolBelowPeriodAvg = pVolBelowPeriodAvg
End Property
Public Property Let VolBelowPeriodAvg(iVolBelowPeriodAvg As Integer)
    pVolBelowPeriodAvg = iVolBelowPeriodAvg
End Property

'VolPaceShort
Public Property Get VolPaceShort() As Integer
    VolPaceShort = pVolPaceShort
End Property
Public Property Let VolPaceShort(iVolPaceShort As Integer)
    pVolPaceShort = iVolPaceShort
End Property

'VolPaceLong
Public Property Get VolPaceLong() As Integer
    VolPaceLong = pVolPaceLong
End Property
Public Property Let VolPaceLong(iVolPaceLong As Integer)
    pVolPaceLong = iVolPaceLong
End Property

'VolPacePeriodAvg
Public Property Get VolPacePeriodAvg() As Long
    VolPacePeriodAvg = pVolPacePeriodAvg
End Property
Public Property Let VolPacePeriodAvg(iVolPacePeriodAvg As Long)
    pVolPacePeriodAvg = iVolPacePeriodAvg
End Property

'Vol65DayAvg
Public Property Get Vol65DayAvg() As Long
    Vol65DayAvg = pVol65DayAvg
End Property
Public Property Let Vol65DayAvg(iVol65DayAvg As Long)
    pVol65DayAvg = iVol65DayAvg
End Property



'Point1, Point2, Point3, Long or Short

'PT1
Public Property Get PT1() As Integer
    PT1 = pPT1
End Property
Public Property Let PT1(iPT1 As Integer)
    pPT1 = iPT1
End Property

'PT2
Public Property Get PT2() As Integer
    PT2 = pPT2
End Property
Public Property Let PT2(iPT2 As Integer)
    pPT2 = iPT2
End Property

'PT3
Public Property Get PT3() As Integer
    PT3 = pPT3
End Property
Public Property Let PT3(iPT3 As Integer)
    pPT3 = iPT3
End Property

感谢您一看!

好吧,我试图在没有错误恢复的情况下运行它。结果让我感到困惑。运行时的变量和对象在中间窗口中打印结果。但是,在我的监视窗口中,barlogitems集合中的项目值不会更改。

enter image description here

0 个答案:

没有答案