格式化电话号码 - vba
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19019878/
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
Formatting phone numbers - vba
提问by hell_666
I have large data set - 10 000 records with the phone numbers:
我有大数据集 - 10 000 条电话号码记录:
Phone_N
5656666666
6676767677
6767677777
5555555555
5555567888
6666777777
I need to format it like:
我需要将其格式化为:
Phone_N
(565) 666-6666
(222) 767-3333
(676) 777-7777
etc....
等等....
My data already have no spaces, "-", "/" or any other characters.
我的数据已经没有空格、“-”、“/”或任何其他字符。
It just needed to be formatted in a proper phone format.
它只需要格式化为适当的电话格式。
It would be very helpful if you could point me the right direction to start.
如果您能指出我正确的开始方向,那将非常有帮助。
回答by HansUp
You can use the Format Functionto format Phone_N
as you wish. I'm not sure whether Phone_N
is text or numeric data type, but the following Format
example will accept either.
您可以使用格式化功能Phone_N
按您的意愿进行格式化。我不确定Phone_N
是文本数据类型还是数字数据类型,但下面的Format
示例将接受。
Phone_N = 2227673333 ' (Phone_N is numeric)
? Format(Phone_N, "(###) ###-####")
(222) 767-3333
Phone_N = "2227673333" ' (Phone_N is text)
? Format(Phone_N, "(###) ###-####")
(222) 767-3333
回答by HansUp
You could use a UDF (user defined function)
您可以使用 UDF(用户定义函数)
stick the below code in a standard module in VBE (Visual Basic Editor) ALT+F11right click anywhere in the Project Explorer window » Insert » Module
将以下代码粘贴到 VBE(Visual Basic 编辑器)中的标准模块中ALT+F11右键单击项目资源管理器窗口中的任意位置»插入»模块
after formatting use ADO to UPDATE your table
格式化后使用 ADO 更新您的表
Function FORMAT_NUMBERS(r As Range)
FORMAT_NUMBERS = "(" & Left(r, 3) & ") " & Right(Left(r, 6), 3) & "-" & Right(r, 4)
End Function
example
例子
回答by Nancy
no need to use VBA, just try Notepad++, edit your data in cloumn pattern.
无需使用 VBA,只需尝试 Notepad++,以 cloumn 模式编辑数据。
回答by Linger
This can be done in VBA. You could use the following to accomplish this.
这可以在 VBA 中完成。您可以使用以下方法来完成此操作。
Dim rs As DAO.Recordset, TempN As String
Set rs = CurrentDb.OpenRecordset("SELECT Phone_N FROM MyTableName", dbOpenDynaset)
If (rs.RecordCount <> 0) Then
rs.MoveFirst
Do While rs.EOF <> True
TempN = rs.Fields("[Phone_N]").value
rs.Edit
rs.Fields("[Phone_N]").value = "(" & Left(TempN, 3) & ") " & _
Left(Right(TempN, 7), 3) & "-" & _
Right(TempN, 4)
rs.Update
rs.MoveNext
Loop
End If