我被要求在excel列中修改一些电话号码值,我想借此机会第一次制作UDF。
目前,我有一个大小不一的电话号码列表。有些数字是完整的,有些数字是不完整的(即长度范围从7位数到11位数)。每个数字的目标长度是10位数字。如果数字大于10位,我会修剪掉第一个数字,如果小于10,我会在字符串的开头附加“ 0”,直到长度为10位。
我不太确定自己在做什么错,所以我希望有人可以指出正确的方向。目前,我的函数单元格中出现“ #VALUE”。我的目标是一个格式为“文本”单元格的单元格。任何建议表示赞赏!这是我的功能...
Public Function phone_number_trim(phone_number As String)
Dim phone_number_length As Integer
Dim s_phone_number As String
phone_number_length = phone_number.Len
s_phone_number = phone_number
If phone_number_length > 10 Then s_phone_number = Mid(s_phone_number, 2, 10)
If phone_number_length < 10 Then
For i = 0 To phone_number_length
s_phone_number = "0" + s_phone_number
Next
End If
phone_number = s_phone_number
End Function
这是我的编辑器的外观,以防万一... Editor
答案 0 :(得分:0)
万一有人对此感兴趣,那就是我最终使用的功能...
我最终不得不清理一下数据,所以写了一个支持函数。它并不完美,我知道在某些情况下它行不通,但是当它们来临时,我会过桥!
Public Function prepare_the_subject(subject As String) As String
Dim bad_guys() As Variant
bad_guys = Array(" ", "-", ")", "(", "'", ".", "/", "\")
Dim test As String
test = subject
For Each guy In bad_guys
test = Replace(test, guy, "")
Next guy
prepare_the_subject = test
End Function
Public Function phone_number_trim(phone_number As String) As String
Dim phone_number_length As Integer
Dim s_phone_number As String
Dim i As Integer
Dim zero_len As Integer
s_phone_number = phone_number
s_phone_number = prepare_the_subject(s_phone_number)
phone_number_length = Len(s_phone_number)
zero_len = 10 - phone_number_length
If phone_number_length > 10 Then s_phone_number = Mid(s_phone_number, 2, 10)
If phone_number_length < 10 Then
For i = 1 To zero_len
s_phone_number = "0" + s_phone_number
Next
End If
phone_number_trim = s_phone_number
End Function