PC Review


Reply
Thread Tools Rate Thread

How Do I Use List Box for VBA Script that Converts Excel Files to Any FIle Type?

 
 
socrtwo
Guest
Posts: n/a
 
      22nd Oct 2006
Hi,

I'm a newbie to VBA. This is only the second script I've worked with
and I still don't know some basic things.

I found an excellent Dave Peterson script here:
http://tinyurl.com/yd9dp3. I modified it slightly with a folder path
input box, and stopped Excel from asking whether to update links by
adding "UpdateLinks:=False" to the Workbooks.Open statement. At any
rate, it extracts all the worksheets from the Excel files in a folder
and converts them to CSV files. I would like to change the script so
that it can convert the Excel files to any file type in the Excel
arsenal.

I found the XlFileFormat class list here:
http://msdn2.microsoft.com/en-us/lib...ileformat.aspx.
In addition to the folder path InputBox, I would like to display a
list box where I can choose the file format to covert the files to.

I got as far as creating a form which is populated by values from my
second sheet, which are simply taken from the XlFileFormat class list
which is 43 rows by 2 columns. I tried to use
FileFormat:=lbxExcelFileClass.Value to both display the form and feed
the chosen value into the script. I get an error "Compile Error:
Variable not defined.". I have no code in the list box itself, which
is right now just as below:

Private Sub lbxExcelFileClass_Click()

End Sub

Here is the working script right now where xlCSV stands in where I
would like the value from the list box to be fed in:

Option Explicit

Private Sub CommandButton1_Click()


Dim myfiles() As String
Dim i As Integer
Dim myfile As String
Dim myfolder As String
Dim strpath As String
Dim strfilename As String


Dim wks As Worksheet


myfolder = InputBox("Enter complete path to the Excel files you wish to
convert to CSV format. Put an \ on the end of the path.", "Excel File
Folder Path")


With Application.FileSearch
.NewSearch
.LookIn = myfolder
.SearchSubFolders = True
.Filename = "*.xls"
If .Execute() > 0 Then
ReDim Preserve myfiles(1 To .FoundFiles.Count)
Application.StatusBar = "Found Files: " &
..FoundFiles.Count
For i = 1 To .FoundFiles.Count
myfiles(i) = .FoundFiles(i)
Next i
Else
MsgBox "There were no files found."
Exit Sub
End If


End With


For i = LBound(myfiles) To UBound(myfiles)
Application.StatusBar = "Processing #" & i & ": " & myfiles(i)


Workbooks.Open Filename:=myfiles(i), ReadOnly:=True,
UpdateLinks:=False


For Each wks In ActiveWorkbook.Worksheets
wks.Activate
Application.DisplayAlerts = False
ActiveWorkbook.SaveAs _
Filename:=Left(myfiles(i), Len(myfiles(i)) - 4) & "_" _
& wks.Name, _
FileFormat:=xlCSV
Application.DisplayAlerts = True
Next wks


ActiveWorkbook.Close savechanges:=False


Next i


Application.StatusBar = False

End Sub

 
Reply With Quote
 
 
 
 
Bob Phillips
Guest
Posts: n/a
 
      22nd Oct 2006
That is probably because you have loaded the listbox with the constant
names, but it is the constant value that is used by the VBA statement.

Create a 2 column list, one with the names, one with the value, like so,

xlAddin 18
xlCSV 6
etc.

and set the listbox to reference this list, then in the code use

ActiveWorkbook.SaveAs _
Filename:=Left(myfiles(i), Len(myfiles(i)) - 4) & "_" _
& wks.Name, _

FileFormat:=lbxExcelFileClass.List(lbxExcelFileClass.Listindex,1)


--
HTH

Bob Phillips

(replace somewhere in email address with gmail if mailing direct)

"socrtwo" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Hi,
>
> I'm a newbie to VBA. This is only the second script I've worked with
> and I still don't know some basic things.
>
> I found an excellent Dave Peterson script here:
> http://tinyurl.com/yd9dp3. I modified it slightly with a folder path
> input box, and stopped Excel from asking whether to update links by
> adding "UpdateLinks:=False" to the Workbooks.Open statement. At any
> rate, it extracts all the worksheets from the Excel files in a folder
> and converts them to CSV files. I would like to change the script so
> that it can convert the Excel files to any file type in the Excel
> arsenal.
>
> I found the XlFileFormat class list here:
>

http://msdn2.microsoft.com/en-us/lib...ileformat.aspx.
> In addition to the folder path InputBox, I would like to display a
> list box where I can choose the file format to covert the files to.
>
> I got as far as creating a form which is populated by values from my
> second sheet, which are simply taken from the XlFileFormat class list
> which is 43 rows by 2 columns. I tried to use
> FileFormat:=lbxExcelFileClass.Value to both display the form and feed
> the chosen value into the script. I get an error "Compile Error:
> Variable not defined.". I have no code in the list box itself, which
> is right now just as below:
>
> Private Sub lbxExcelFileClass_Click()
>
> End Sub
>
> Here is the working script right now where xlCSV stands in where I
> would like the value from the list box to be fed in:
>
> Option Explicit
>
> Private Sub CommandButton1_Click()
>
>
> Dim myfiles() As String
> Dim i As Integer
> Dim myfile As String
> Dim myfolder As String
> Dim strpath As String
> Dim strfilename As String
>
>
> Dim wks As Worksheet
>
>
> myfolder = InputBox("Enter complete path to the Excel files you wish to
> convert to CSV format. Put an \ on the end of the path.", "Excel File
> Folder Path")
>
>
> With Application.FileSearch
> .NewSearch
> .LookIn = myfolder
> .SearchSubFolders = True
> .Filename = "*.xls"
> If .Execute() > 0 Then
> ReDim Preserve myfiles(1 To .FoundFiles.Count)
> Application.StatusBar = "Found Files: " &
> .FoundFiles.Count
> For i = 1 To .FoundFiles.Count
> myfiles(i) = .FoundFiles(i)
> Next i
> Else
> MsgBox "There were no files found."
> Exit Sub
> End If
>
>
> End With
>
>
> For i = LBound(myfiles) To UBound(myfiles)
> Application.StatusBar = "Processing #" & i & ": " & myfiles(i)
>
>
> Workbooks.Open Filename:=myfiles(i), ReadOnly:=True,
> UpdateLinks:=False
>
>
> For Each wks In ActiveWorkbook.Worksheets
> wks.Activate
> Application.DisplayAlerts = False
> ActiveWorkbook.SaveAs _
> Filename:=Left(myfiles(i), Len(myfiles(i)) - 4) & "_" _
> & wks.Name, _
> FileFormat:=xlCSV
> Application.DisplayAlerts = True
> Next wks
>
>
> ActiveWorkbook.Close savechanges:=False
>
>
> Next i
>
>
> Application.StatusBar = False
>
> End Sub
>



 
Reply With Quote
 
socrtwo
Guest
Posts: n/a
 
      23rd Oct 2006

Bob Phillips wrote:
> That is probably because you have loaded the listbox with the constant
> names, but it is the constant value that is used by the VBA statement.
>
> Create a 2 column list, one with the names, one with the value, like so,
>
> xlAddin 18
> xlCSV 6
> etc.
>
> and set the listbox to reference this list, then in the code use
>
> ActiveWorkbook.SaveAs _
> Filename:=Left(myfiles(i), Len(myfiles(i)) - 4) & "_" _
> & wks.Name, _
>
> FileFormat:=lbxExcelFileClass.List(lbxExcelFileClass.Listindex,1)
>


Bob, thanks for replying. I see I may be asking for a value when it's
really a string I'm looking for. I looked up the table of the class
values here:
http://msdn.microsoft.com/library/de...HV01049962.asp
and replaced my table with that one.
I put in you code to replace mine and still got the error.

I looked up the error and it seems that since I started my code with
"Options Explicit", I needed to Dim the variable. So my question is,
can I keep my list which explains what each xl[file type] in the second
column, if I just Dim lbxExcelFileClass as a string?
Also, how do I make sure the form gets displayed?

If I put back my table the way it was originally and Dim
lbxExcelFileClass as a string and make
FileFormat:=lbxExcelFileClass.String. I get another compile error:
"invalid qualifier.
Any ideas?

 
Reply With Quote
 
Bob Phillips
Guest
Posts: n/a
 
      23rd Oct 2006

"socrtwo" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>
> Bob Phillips wrote:
> > That is probably because you have loaded the listbox with the constant
> > names, but it is the constant value that is used by the VBA statement.
> >
> > Create a 2 column list, one with the names, one with the value, like so,
> >
> > xlAddin 18
> > xlCSV 6
> > etc.
> >
> > and set the listbox to reference this list, then in the code use
> >
> > ActiveWorkbook.SaveAs _
> > Filename:=Left(myfiles(i), Len(myfiles(i)) - 4) & "_" _
> > & wks.Name, _
> >
> > FileFormat:=lbxExcelFileClass.List(lbxExcelFileClass.Listindex,1)
> >

>
> Bob, thanks for replying. I see I may be asking for a value when it's
> really a string I'm looking for. I looked up the table of the class
> values here:
>

http://msdn.microsoft.com/library/de...HV01049962.asp
> and replaced my table with that one.
> I put in you code to replace mine and still got the error.



Shouldn't have if you did it correctly.


> I looked up the error and it seems that since I started my code with
> "Options Explicit", I needed to Dim the variable. So my question is,
> can I keep my list which explains what each xl[file type] in the second
> column, if I just Dim lbxExcelFileClass as a string?
> Also, how do I make sure the form gets displayed?



What variable? lbxExccelClassFile is the name of your listb os I assume, so
it is already defined.



> If I put back my table the way it was originally and Dim
> lbxExcelFileClass as a string and make
> FileFormat:=lbxExcelFileClass.String. I get another compile error:
> "invalid qualifier.
> Any ideas?



Don't Dim lbxExcleClassFile, it is a listbox. I just ran another test and

ActiveWorkbook.SaveAs "C:\GetRidof",
FileFormat:=lbxExcelFileClass.List(lbxExcelFileClass.ListIndex, 1)

worked fine.


 
Reply With Quote
 
socrtwo
Guest
Posts: n/a
 
      23rd Oct 2006
Thanks for staying with this. BTW, I'm using Excel 2003.

> Shouldn't have if you did it correctly.


The table should be two columns right? And the the list box should
have two column listed in its properties?

> > I looked up the error and it seems that since I started my code with
> > "Options Explicit", I needed to Dim the variable. So my question is,
> > can I keep my list which explains what each xl[file type] in the second
> > column, if I just Dim lbxExcelFileClass as a string?
> > Also, how do I make sure the form gets displayed?

>
>
> What variable? lbxExccelClassFile is the name of your listb os I assume, so
> it is already defined.


The form itself has a different name. It's named lbForm. Does that
matter? Should it appear on its own or do I have to make it appear
with a line of code?

> > If I put back my table the way it was originally and Dim
> > lbxExcelFileClass as a string and make
> > FileFormat:=lbxExcelFileClass.String. I get another compile error:
> > "invalid qualifier.
> > Any ideas?

>
>
> Don't Dim lbxExcleClassFile, it is a listbox. I just ran another test and
>
> ActiveWorkbook.SaveAs "C:\GetRidof",
> FileFormat:=lbxExcelFileClass.List(lbxExcelFileClass.ListIndex, 1)
>
> worked fine.


 
Reply With Quote
 
Bob Phillips
Guest
Posts: n/a
 
      23rd Oct 2006


"socrtwo" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Thanks for staying with this. BTW, I'm using Excel 2003.



Shouldn't be material.

> The form itself has a different name. It's named lbForm. Does that
> matter? Should it appear on its own or do I have to make it appear
> with a line of code?



Shouldn';t be material.

I have posted an example at http://cjoint.com/?kxoJLUfCWS


 
Reply With Quote
 
socrtwo
Guest
Posts: n/a
 
      28th Oct 2006

Bob Phillips wrote:
> "socrtwo" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > Thanks for staying with this. BTW, I'm using Excel 2003.

>
>
> Shouldn't be material.
>
> > The form itself has a different name. It's named lbForm. Does that
> > matter? Should it appear on its own or do I have to make it appear
> > with a line of code?

>
>
> Shouldn';t be material.
>
> I have posted an example at http://cjoint.com/?kxoJLUfCWS


Nice form. Thanks for the help. I've got a new problem, so I'm starting
a new thread. It appears that if I choose any of the Excel formats and
not csv, the script simply produces multiple copies of the original
file instead of exporting each worksheet separately.

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Excel converts string in CSV file as number DavidC Microsoft Excel Misc 5 25th Jul 2008 02:37 PM
Excel file from IE auto converts to PDF =?Utf-8?B?Q2FyeSBL?= Microsoft Excel Misc 1 15th Aug 2006 05:43 PM
when i type in 0 in excel 2000 it converts into -1 =?Utf-8?B?TW9uYQ==?= Microsoft Excel Misc 5 10th May 2006 09:44 PM
when i type in 0 in excel 2000 it converts into -1 =?Utf-8?B?TW9uYQ==?= Microsoft Excel Misc 0 10th May 2006 08:32 PM
Excel not an option in 'Files of Type' list: can't insert .xls in =?Utf-8?B?UEFHQQ==?= Microsoft Frontpage 1 28th May 2005 12:09 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:51 PM.