You are here:

Excel/VBA code to convert .dat to .xls

Advertisement


Question
QUESTION: Dear Tom,

Hope you're doing well.

Need you assistance on the below code. I trying to use the code to ocnvert the .dat files to .xls files, however I am getting "Compile error" on Line "Dim f As New FilesystemObject."

Can you tell me as to why the code is not working and the fix?

Sub Convert()

Dim f As New FileSystemObject
Dim fil As File

For Each fil In f.GetFolder("H:\
       f.CopyFile fil.Path, _
         "H:\Convert" & f.GetBaseName(fil.Path) & ".xls"
   End If
Next

Set f = Nothing

End Sub


Thank you again for your help
Regards,
Nishant

ANSWER: Vish,

Do you have a reference set to the microsoft scripting runtime?  To declare something as FileSystemObject, you would need a reference to a library that defines that object.

I pasted in your code and fixed the obvious typo  so it looked like

Sub Convert()

Dim f As New FileSystemObject
Dim fil As File

For Each fil In f.GetFolder("H:\")
 f.CopyFile fil.Path, _
        "H:\Convert" & f.GetBaseName(fil.Path) & ".xls"

Next

Set f = Nothing

End Sub

Until I set the reference, I got an error on the line you mentioned

When I created the reference to the microsoft scripting runtime, then the error no longer appeared.   Hopefully that is all you need to do to remove the error.  

--
Regards,
Tom Ogilvy


---------- FOLLOW-UP ----------

QUESTION: Hi Tom,

Thank you for the correct. However can I adjust the code in a way that does not require the reference. The purpose of the code is to loop in specified folder and change all the .dat files into .xls files.

Regards,
Nishant

Answer
Nishant,

You can use late binding.  I modified your code to use late binding.  I did not try to debug your code for other errors.  You can do that.

Sub Convert()

Dim f As Object
Dim fil As File

Set f = CreateObject("scripting.filesystemobject")
For Each fil In f.GetFolder("H:\")
f.CopyFile fil.Path, _
       "H:\Convert" & f.GetBaseName(fil.Path) & ".xls"

Next

Set f = Nothing

End Sub

--
Regards,
Tom Ogilvy

About Excel
This topic answers questions related to Microsoft Excel spreadsheet (or workbook) stand-alone or Mircrosoft Office Excel including Excel 2003, Excel 2007, Office 2000, and Office XP. You can get Excel help on Excel formulas(or functions), Excell macros, charting in Excel, advanced features, and the general use of Excel. This does not provide a general Excel tutorial nor the basics of using a spreadsheet. It provides specific answers to using Microsoft Excel only. If you do not see your Excel question answered in this area then please ask an Excel question here

Excel

All Answers


Answers by Expert:


Ask Experts

Volunteer


Tom Ogilvy

Expertise

Selected as an Excel MVP by Microsoft since 1999. Answering Excel questions in Allexperts since its inception in 2001. Able to answer questions on almost all aspects of Excel's internal capabilities. If seeking a VBA solution, please specify that in your question itself so I give you the answer you want. [Excel has weak protection - if you are distributing an application, I don't answer questions on how to protect your project from your users.]

Experience

Extensive experience.

Education/Credentials
Master of Science (MS) degree Operations Research (ORSA)

Awards and Honors
Microsoft MVP in Excel.

©2016 About.com. All rights reserved.