使用 VBA 计算单元格中的位数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21713094/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 01:52:25  来源:igfitidea点击:

Count a the number of digits in a cell with VBA

excelvbaexcel-vba

提问by user3271518

I get a spreadsheet but sometimes they dont give me all the digits I need when running my macro. So i am trying to find a way to count how many digits are in a cell and then use msgbox to tell me to add a digit to the cell.

我得到一个电子表格,但有时他们不会给我运行我的宏时需要的所有数字。所以我试图找到一种方法来计算单元格中有多少位数字,然后使用 msgbox 告诉我向单元格添加一个数字。

If Len(A2) <> 7 Then
MsgBox ("Add # to the end ")
Exit Sub
End If

I was also wondering if there is a way to use an input box to add the number to the end of the value in excel. Like if the numbers are 123456 i can put 7 into the input box and it changes the cell value to 1234567

我还想知道是否有办法使用输入框将数字添加到 excel 中的值的末尾。就像数字是 123456 我可以在输入框中输入 7 并将单元格值更改为 1234567

采纳答案by user2140261

This should work for Characters, But if other Characters are in the cell besides just numbers it counts everything:

这应该适用于字符,但是如果单元格中除了数字之外还有其他字符,它会计算所有内容:

If Len([A2]) <> 7 Then
    [A2] = [A2].Value & InputBox("Add # to the end ")
End If