首页 > 应用开发 > IT综合 > 将EXCEL里的电话号码用逗号分隔导出
有个同事需要在EXCEL表中某一列导出电话号码,并用逗号隔开,GOOGLE查了一些文档后,调用VB来完成这个转换,总结如下:
1、EXCEL里,选择菜单,工具-宏-Visual Basic 编辑器,粘贴以下代码。
Sub QuoteCommaExport()
' Dimension all variables.
Dim DestFile As String
Dim FileNum As Integer
Dim ColumnCount As Integer
Dim RowCount As Integer
' Prompt user for destination file name.
DestFile = InputBox("Enter the destination filename" _
& Chr(10) & "(with complete path):", "Quote-Comma Exporter")
' Obtain next free file handle number.
FileNum = FreeFile()
' Turn error checking off.
On Error Resume Next
' Attempt to open destination file for output.
Open DestFile For Output As #FileNum
' If an error occurs report it and end.
If Err <> 0 Then
MsgBox "Cannot open filename " & DestFile
End
End If
' Turn error checking on.
On Error GoTo 0
' Loop for each row in selection.
For RowCount = 1 To Selection.Rows.Count
' Loop for each column in selection.
For ColumnCount = 1 To Selection.Columns.Count
' Write current cell's text to file with quotation marks.
Print #FileNum, Selection.Cells(RowCount, _
ColumnCount).Text & ",";
' Check if cell is in last column.
If ColumnCount = Selection.Columns.Count Then
' If so, then write a blank line.
Print #FileNum,
Else
' Otherwise, write a comma.
Print #FileNum, ",";
End If
' Start next iteration of ColumnCount loop.
Next ColumnCount
' Start next iteration of RowCount loop.
Next RowCount
' Close destination file.
Close #FileNum
End Sub
2、在EXCEL表中选择你要导出的那一列电话号码,点击菜单,工具-宏-宏,选择QuoteCommaExport,点击“执行”。
3、在弹出的对话框中,输入路径和导出的文件名。回车,电话号码就在新的文件用逗号分隔开来了。
4、在新的文件里发现一个电话号码为一行,NND,同事说不符合要求,必须所有号码为一行。后来用UltraEdit-32转换了,选择所有号码,点击菜单,格式-转换回车/换行符为换行,所有号码为一行了。
[@more@]来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/21100044/viewspace-1019592/ ,如需转载,请注明出处,否则将追究法律责任。