Xls到CSV宏数字的转换格式

时间:2013-08-20 10:59:08

标签: excel vba excel-vba

我有一个宏'macro1',可以将文件夹中的所有* .xls文件转换为* .csv。转换后,我使用命令提示符合并行。我使用macro2将合并的文件转换为xls。一切都工作正常但是当值类似于123456789123456时,生成的csv值类似于1234E + 11。如何在转换后的文件之间保留数字格式?

这是我的宏

宏1

Option Explicit

Sub ConvertToCSV()

Dim i As Long
Dim NumFiles As Long
Dim FileName As String
Dim FileNames() As String


FileName = Dir(ThisWorkbook.Path & "/*.xls")

NumFiles = 1
ReDim Preserve FileNames(1 To NumFiles)
FileNames(NumFiles) = FileName


Do While FileName <> ""
FileName = Dir()
If FileName <> "" Then
NumFiles = NumFiles + 1
ReDim Preserve FileNames(1 To NumFiles)
FileNames(NumFiles) = FileName
End If
Loop


Application.DisplayAlerts = False
For i = 1 To UBound(FileNames)
If FileNames(i) <> ThisWorkbook.Name Then

Workbooks.Open FileName:=ThisWorkbook.Path & "\" & FileNames(i)
ActiveWorkbook.SaveAs _
FileName:=Left(FileNames(i), Len(FileNames(i)) - 4) & ".csv", _
FileFormat:=xlCSV
ActiveWorkbook.Close
End If
Next i
Application.DisplayAlerts = True

End Sub

和macro2

Sub FromCSVToXLS()
Dim myWB As Workbook, WB As Workbook
Dim L As Long, x As Long, i As Long

Dim v As Variant

Dim myPath

Dim myFile
Set myWB = ThisWorkbook
Application.ScreenUpdating = False

Sheets(1).Cells.ClearContents
myPath = "C:\Folder1\Folder2\"   '<<< change path
myFile = "myFile.csv"   '<<< change file name
Set WB = Workbooks.Open(myPath & myFile)
ActiveSheet.UsedRange.Copy myWB.Sheets(1).Range("A1")
ActiveWorkbook.Close False
L = myWB.Sheets(1).UsedRange.Rows.Count
For i = 1 To L
v = Split(Cells(i, 1), ",")
For x = 0 To UBound(v)
Cells(i, x + 1) = v(x)
Next x
Next i
Application.DisplayAlerts = False
ActiveWorkbook.SaveAs Filename:= "D:\data folder\1.xls"

Application.DisplayAlerts = True
myWB.Save
Application.ScreenUpdating = True
End Sub

PS:命令提示符不会改变数字格式。 VERIFIED!

谢谢

1 个答案:

答案 0 :(得分:0)

解决!!

我这样做了:

打开合并文件

选择相关列

列上的右clic

我更改了所有列单元格的格式。