[英]Accessing an objects fields within another sub in VBA
我創建了一個對象,表示要從一個excel工作表中查找並獲取的三個值,然后將其復制並粘貼到工作簿的另一個工作表中的單元格中。 我在函數中創建對象后一直試圖訪問對象的字段。
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
這里的代碼示例(未經測試)。 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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.