datacontrol
Registered User.
- Local time
- Today, 12:35
- Joined
- Jul 16, 2003
- Messages
- 142
I need to convert a csv file to xls via the push of a button. Can anyone help?
Thanks
Thanks
Public Sub ConvertCSVtoXL(strCSVPath As String)
Dim appExcel As Excel.Application
'Switch to Microsoft Excel so it won't go away when you finish.
On Error Resume Next
AppActivate "Microsoft Excel"
'If Excel isn't running, start and activate it
If Err Then
Shell "c:\Program Files\Microsoft Office\Office\" _
& "Excel /Automation", vbHide
AppActivate "Microsoft Excel"
End If
On Error GoTo 0
'Get an Application object so you can automate Excel.
Set appExcel = GetObject(, "Excel.Application")
With appExcel
.Workbooks.Open FileName:=strCSVPath
ActiveWorkbook.SaveAs FileName:=Left(strCSVPath, Len(strCSVPath) - 3) & "xls" _
, FileFormat:=xlNormal
End With
appExcel.Quit
Set appExcel = Nothing
MsgBox "File '" & strCSVPath & "' has been converted to excel under the same " & _
"filename with an XLS extension"
End Sub
ConvertCSVtoXL "C:\YourDocs\YourFile.csv"
- really they're not. Open a .csv file with notepad and you'll see that it is a text format. Open an .xls file with notepad and see the difference. I know that you wouldn't intentionally open a Word document with Excel but you would be very confused if you opened what you thought was an .xls file and got an import dialog.CSV and XLS are very closely related
With appExcel
.Workbooks.Open FileName:=strCSVPath
ActiveWorkbook.SaveAs FileName:=Left(strCSVPath, Len(strCSVPath) - 3) & "xls" _
, FileFormat:=xlNormal
End With
With appExcel
.Workbooks.Open FileName:=strCSVPath
.Columns("A:A").NumberFormat = "0"
.ActiveWorkbook.SaveAs FileName:=Left(strCSVPath, Len(strCSVPath) - 3) & "xls" _
, FileFormat:=xlNormal
End With