我創建了一個對象來表示要從一個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
CProduct.emea應該是prod.emea等。也許你混淆類的概念與對象的概念?你的類是CProduct,它的一個實例是變量prod引用的對象。 – dee
我能夠將updatepipe findVals作爲函數對象傳遞嗎?或者我可以在pipeWrapper sub中設置一個變量作爲調用findVals的結果CProduct? –