2016-02-11 74 views
1

我創建了一個對象來表示要從一個Excel工作表中找到並取得的三個值,然後將它們複製並粘貼到工作簿中另一個工作表中的單元格中。我在試圖在函數中創建對象後嘗試訪問對象的字段。在VBA中訪問另一個子對象中的對象字段

Public CProduct 

Private pEmea As Double 
Private pRenewal As Double 
Private pTotal As Double 

'Emea Property 
Public Property Get emea() As Double 
    emea = pEmea 
End Property 
Public Property Let emea(Value As Double) 
    pEmea = Value 
End Property 

'Renewal Property 
Public Property Get Renewal() As Double 
    Renewal = pRenewal 
End Property 
Public Property Let Renewal(Value As Double) 
    pRenewal = Value 
End Property 

'Total Property 
Public Property Get Total() As Double 
    Total = pTotal 
End Property 
Public Property Let Total(Value As Double) 
    pTotal = Value 
End Property 

Sub pipeWrapper() 
Dim prod As CProduct 
findVals "Testing", "A2:A55" 
updatePipe findVals("Testing", "A2:A55"), "G6" 

End Sub 

Sub updatePipe(prod As CProduct, myCell As String) 
Sheets("Weekly Pipeline Metrics").Activate 

Sheets("Weekly Pipeline Metrics").range(myCell).Value = prod.emea 
Sheets("Weekly Pipeline Metrics").range(myCell).Offset(1).Value = prod.Renewal 
Sheets("Weekly Pipeline Metrics").range(myCell).Offset(2).Value = prod.Total 


End Sub 


Function findVals(pdct As String, rg As String) As CProduct 
Dim prod As CProduct 
Set prod = New CProduct 


With Worksheets("Pipeline Raw Data").range(rg) 
For Each ProductCell In Sheets("Pipeline Raw Data").range(rg) 
    If ProductCell.Value = pdct Then 
     For Each TypeCell In range(ProductCell.Offset(, 1), ProductCell.Offset(4, 1)) 
      If TypeCell.Value = "Renewal" And TypeCell.Offset(1).Value = "Subtotal" Then 
       prod.emea = TypeCell.Offset(1, 2).Value 
       prod.Total = TypeCell.Offset(1, 4).Value 
       prod.Renewal = TypeCell.Offset(, 4).Value 
     Exit For 
    ElseIf TypeCell.Value = "New" And TypeCell.Offset(1).Value = "Subtotal" Then 
     prod.Renewal = 0 
     prod.Total = TypeCell.Offset(1, 4).Value 
     prod.emea = TypeCell.Offset(1, 2).Value 

    End If 
    Next TypeCell 
End If 
Next ProductCell 
End With 
End Function 
+2

CProduct.emea應該是prod.emea等。也許你混淆類的概念與對象的概念?你的類是CProduct,它的一個實例是變量prod引用的對象。 – dee

+0

我能夠將updatepipe findVals作爲函數對象傳遞嗎?或者我可以在pipeWrapper sub中設置一個變量作爲調用findVals的結果CProduct? –

回答

0

這裏的代碼示例(未經測試)。 HTH

Sub pipeWrapper() 
    Dim prod As CProduct 

    ' - Could I set a variable in the pipeWrapper sub to be the CProduct that results from calling findVals? 
    ' Yes you can: 
    set prod = findVals("Testing", "A2:A55") 

    ' - Am i able to pass updatepipe findVals as a function object? 
    ' Yes you can: 
    updatePipe prod, "G6" 
End Sub 

Sub updatePipe(prod As CProduct, myCell As String) 
    Sheets("Weekly Pipeline Metrics").Activate 
    Sheets("Weekly Pipeline Metrics").range(myCell).Value = prod.emea ' This was wrong: CProduct.emea 
    Sheets("Weekly Pipeline Metrics").range(myCell).Offset(1).Value = prod.Renewal ' This was wrong: CProduct.Renewal 
    Sheets("Weekly Pipeline Metrics").range(myCell).Offset(2).Value = prod.Total ' This was wrong: CProduct.Total 
End Sub 
+0

好吧,我試過,但它仍然給我錯誤「對象變量或塊變量未設置」。當我將鼠標懸停在set語句上時,它說「prod = Nothing」 –

+0

將findVals = prod'''添加到函數findVals'''中。現在函數結果沒有設置爲該函數創建的新CPorduct對象,因此它返回Nothing。 – dee

+0

順便說一句。 「TypeCell」在哪裏申報?你是否使用'''Option Explicit'''? – dee

相關問題