我已經將一個變量作爲參數傳遞給VBA函數,但只引用了函數中的參數,而不是原始變量。看來原始變量正在被函數修改,這意味着它共享相同的地址空間?這裏發生了什麼,我如何防止它發生?傳遞給函數的VBA變量被修改
下面是函數:(單位,數十,數百,數千的全局整型變量)
Function Components(qty As Integer, stringSize As Integer)
If qty < stringSize Then
units = qty
ElseIf qty >= stringSize * 100 Then
thousands = qty \ (stringSize * 100)
qty = qty - (thousands * stringSize * 100)
Components qty, stringSize
ElseIf qty >= stringSize * 10 Then
hundreds = qty \ (stringSize * 10)
qty = qty - (hundreds * stringSize * 10)
Components qty, stringSize
ElseIf qty >= stringSize Then
tens = qty \ (stringSize)
qty = qty - (tens * stringSize)
Components qty, stringSize
End If
End Function
我這樣稱呼它使用其他功能
Components charQty, 26
其中charQty = 565時,作爲參數傳遞給組件,並且在組件完成後charQty = 19。我通過在函數調用之前和之後立即打印值來確定這一點。
我對VBA相當陌生。任何幫助將不勝感激。
的參數隱式[通過由參考](VBA中的「ByRef」)(https://stackoverflow.com/documentation/vba/7363/passing-arguments-byref-or-byval/24427/byref#t=201706020439138540139)。考慮[按值傳遞](https://stackoverflow.com/documentation/vba/7363/passing-arguments-byref-or-byval/28511/byval#t=201706020440066200781)。 –