[英]Excel VBA - Code is converting a decimal to integer when read from cell
我完全不知道為什么要將 long 轉換為 Int; 未定義的變體應保持其內容的格式。
在從工作表中提取 val 后,您能否單步執行代碼並查看 CurrentValue 中的值是多少? 就像...在您問題中的那一行之后, debug.Print CurrentValues 以查看它包含的內容。 我最好的猜測是,無論您最終將 CurrentValues 粘貼到何處,都將被格式化為 Int 或四舍五入到最接近的整數。
您可以在早期綁定中將 CurrentValue 聲明為 Double 或使用 CDec 函數將單元格值轉換為十進制。 在下面查看我的代碼的結果:
Public Function TestingThis()
Dim ws As Worksheet
Dim dblVal As Double
Set ws = Application.ThisWorkbook.Worksheets(1)
For i = 2 To 8
currentValue = ws.Cells(i, 1) 'How you have it in yor example code
ws.Cells(i, 2) = currentValue
dblVal = ws.Cells(i, 1) 'Using a early binding Double Var to retain decimal format
ws.Cells(i, 3) = dblVal
ws.Cells(i, 4) = CDec(ws.Cells(i, 1)) 'Using the CDec Function to retain decimal format
Next i
End Function
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.