excel - 命令将 0 添加到相关列和单元格 - Excel vba
问题描述
我有 2 个命令将 0 添加到数字(8 位,需要 9 位)和 0 到手机号码(以 5 位开头的 9 位需要为 10 位):
1.添加0到8位数字:
Sub Add_Zeros()
Selection.NumberFormat = "@"
For Each CL In Selection.Cells
If CL <> "" Then CL.Value = Application.Rept("0", (9 - Len(CL))) & CL
Next
End Sub
2.添加0到9位数字:
Sub Add_Zeros()
Selection.NumberFormat = "@"
For Each CL In Selection.Cells
If CL <> "" Then CL.Value = Application.Rept("0", (10 - Len(CL))) & CL
Next
End Sub
我可以升级这些代码并将它们组合如下吗?:
A.第一个条件:如果只有8位就在开头加0(最后应该是9位)
B.第二个条件:如果有9个数字,左边第一个数字是5你在开头加0(最后应该有10个数字)
C.还有什么我如何将命令返回(即更改之前)以取消?你有办法把它插入另一个代码吗?
提前致谢 ,
解决方案
不确定我是否完全理解,但您可以使用if
else
和elseif
Function sort_out_phone(strPhoneIN As String) As String
If Len(strPhoneIN) = 8 Then
sort_out_phone = "0" & strPhoneIN
ElseIf Len(strPhoneIN) = 9 And Left(strPhoneIN, 1) = "5" Then
sort_out_phone = "0" & strPhoneIN
Else
sort_out_phone = "What ever you need to do here"
End If
End Function
sort_out_phone("12345678")=012345678
sort_out_phone("123456789")=你需要在这里做什么
sort_out_phone("523456789")=0523456789
推荐阅读
- visual-studio-code - 卸载了我能找到的所有东西,但扩展主机仍然意外终止
- xslt - 将 XSLT 中的变量字符串与 IF 条件进行比较
- python - 在 Python 中的 2 列上合并两个不同大小的数据框
- android - Android - 在片段上按下后退按钮只会重新打开相同的片段
- schema - 最好为事件发生的每个日期编写一个 JSON-LD 脚本,还是在一个脚本中定义所有日期?
- android - Android CardView 没有上边框
- python - Django 无法识别模块导入/应用文件夹
- php - 无法使用 nginx 在 linux 18.04 中运行 codeigniter
- python - 从网站收集数据时同时发送多个 http 请求的最佳方式
- .net-core - 如何正确引用 NETStandard 2.1?