我的任务是在我们公司的系统中输入每种产品的重量(超过 65,000 种),所以我使用 Excel 和对 vba 的知识有限的知识来尽可能自动化。
计划是,我输入产品名称的一部分,然后输入要输入的数字,然后在工作表上的所有相关行中输入该数字。
问题; 我输入的数字很少超过小数点后 2 位,但是在工作表上输入的数字并不相同 - 总是非常接近,但不完全一样。例如,当我刚刚尝试在相关单元格中输入 0.88 时,它输入了 0.87999995。
代码(简化):
Sub EnterWeight()
Dim Filter As String
Dim Weight As Single
Filter = InputBox("Add text filter", "Add Filter")
w = InputBox("Insert weight in Kg", "Enter Weight", 1)
'(yes, I know it should be 'mass in Kg', but ... ¯\_(ツ)_/¯ )
Weight = CDec(w)
Debug.Print Weight 'To test that it's the correct number, always seems to be ok.
For b = 1 To Activesheet.UsedRange.Rows.Count
If Cells(b, ) Like "*" & Filter & "*" Then 'Find the filter in any part of the cell
If Cells(b, 2) <> "" And Cells(b, 2).Value <> SG Then 'Cells already populated with a different value
y = MsgBox("Product """ & Cells(b, 1).Value & _
""" already has a weight assigned of " & _
Cells(b, 2).Value & Chr(13) & _
"OverWrite?", vbYesNo + vbExclamation, _
"Weight already assigned")
If y = vbYes Then Cells(b, 2).Value = Weight
Else
Cells(b, 2).Value = Weight
End If
End If
Next
End sub
谁能告诉我为什么不能Weight
在相关单元格中正确输入变量?通过谷歌搜索似乎没有产生答案,尽管也许我只是问错了问题。
提前谢谢了