I Love This VBA for Multiple .xlsx Files Converted to .csv Files Except...

bmst1972

New Member
Joined
Mar 3, 2018
Messages
15
Hi...My name is Robert, and I am a schoolteacher who is taking a dip in the Excel VBA pool. I consider myself to be very knowledgeable of using spreadsheet formulas, but I am definitely an amateur when it comes to coding. I am trying to control my frustration of not being able to find the correct code or being capable of modifying code to satisfy my needs. My goal is to find a macro that will automate the process of converting multiple .xlsx files(exactly 20 files) to .csv files with the CSV files having the same exact names as their corresponding XLSX files. Furthermore, I want new CSV files to overwrite existing CSV files without having to deal with the following annoying message: "A file named _____ already exists in this location. Do you want to replace it?". All of the XLSX files and CSV files are contained in the same folder. I provided you with the closest script I found on the Internet below this message. Although I modified it to allow for the selection of multiple files, the macro only returns one CSV file out of the 20 XLSX files that are located in the folder. I have a feeling the problem has something to do with the part of the script I typed in red. I tried changing .SelectedItems(1) to .SelectedItems(20), but the macro selects the 20th position of the file dialogue window rather than selecting all 20 Excel .xlsx files. Any assistance you can provide will be greatly appreciated.

Sub ConvertToCSV()


Dim myPath As String
Dim myString As Variant




'Turn off Any Alerts
Application.DisplayAlerts = False


'Open Workbook


With Application.FileDialog(msoFileDialogOpen)
.AllowMultiSelect = True
.Show
myPath = .SelectedItems(1)
End With


'Open Workbook
Workbooks.Open Filename:=myPath


'Remove Excel Extension from String
myString = Split(myPath, ".")
myPath = myString(0)

'Save as CVS
ActiveWorkbook.SaveAs Filename:=myPath & " .csv", FileFormat:=xlCSV, CreateBackup:=False


'Close Window
ActiveWindow.Close

'Turn on Alerts
Application.DisplayAlerts = True


End Sub
 
You are not going to believe what just happened! All of a sudden it finally worked! I can't explain it, but it worked...LOL!!!

Something must have changed but that's great it's finally working :)

Like MARK858 says you need to change the block of code I originally wrote to check for a trailing backslash i.e. \ not simply that it's not nothing "" :confused:

Robert
 
Upvote 0

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
I clearly see your point. However, I don't know how I posted "'Add trailing backslash if user hasn't If Right(strMyPath, 1) <> "" Then
strMyPath = strMyPath & "" End If" because I used "'Add trailing backslash if user hasn't If Right(strMyPath, 1) <> "" Then
strMyPath = strMyPath & "" End If" before you sent me your two most recent responses. All I know is what I have is finally working. Therefore, I have no reason to touch any part of the code...LOL
 
Upvote 0
All I know is what I have is finally working. Therefore, I have no reason to touch any part of the code...LOL

The whole purpose of those lines of code Robert posted is to make sure that the address in the cell has the last backslash and if not to add it in.
If you aren't going to use it (i.e. you know you will always have the last backslash in the cell) then remove the lines totally rather that test for a nothing.
 
Last edited:
Upvote 0
Hello...In preparation of the upcoming Pennsylvania state test(PSSA), my school principal recently sent me an Excel spreadsheet consisting of student names and their corresponding start dates for when they began the school year. The purpose of his spreadsheet is for his teachers to cross-reference his data with the teachers' data. I have already been using an Excel spreadsheet to take daily attendance for years. However, my students' start dates are not in the same column due to different late entries into my classes. I want to create a "Start Date" column that contains a formula that will return the first NON blank row cell from left to right(student row) with the corresponding date for each student. If you look to the far right, you will notice Student 1's start date is September 12(Cell P10 on my worksheet) while the other students were enrolled on the official start date of school which was September 6(Cell P11 on my worksheet). Any assistance you can provide will be greatly appreciated.

Robert

[TABLE="width: 1445"]
<colgroup><col><col span="10"><col span="5"></colgroup><tbody>[TR]
[TD]Student Name[/TD]
[TD]Yearly Total[/TD]
[TD]Yearly(P)[/TD]
[TD]Yearly(A)[/TD]
[TD]Yearly(T)[/TD]
[TD]Yearly(H)[/TD]
[TD]1st Q.(Total)[/TD]
[TD]1st Q.(P)[/TD]
[TD]1st Q.(A)[/TD]
[TD]1st Q.(T)[/TD]
[TD]1st Q.(H)[/TD]
[TD]6-Sep[/TD]
[TD]7-Sep[/TD]
[TD]8-Sep[/TD]
[TD]11-Sep[/TD]
[TD]12-Sep[/TD]
[/TR]
[TR]
[TD]Student 1[/TD]
[TD]128[/TD]
[TD]120[/TD]
[TD]8[/TD]
[TD]0[/TD]
[TD]0[/TD]
[TD]39[/TD]
[TD]36[/TD]
[TD]3[/TD]
[TD]0[/TD]
[TD]0[/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD]P[/TD]
[/TR]
[TR]
[TD]Student 2[/TD]
[TD]132[/TD]
[TD]124[/TD]
[TD]8[/TD]
[TD]0[/TD]
[TD]0[/TD]
[TD]43[/TD]
[TD]41[/TD]
[TD]2[/TD]
[TD]0[/TD]
[TD]0[/TD]
[TD]P[/TD]
[TD]P[/TD]
[TD]P[/TD]
[TD]P[/TD]
[TD]P[/TD]
[/TR]
[TR]
[TD]Student 3[/TD]
[TD]132[/TD]
[TD]114[/TD]
[TD]18[/TD]
[TD]0[/TD]
[TD]0[/TD]
[TD]43[/TD]
[TD]34[/TD]
[TD]9[/TD]
[TD]0[/TD]
[TD]0[/TD]
[TD]P[/TD]
[TD]P[/TD]
[TD]P[/TD]
[TD]P[/TD]
[TD]P[/TD]
[/TR]
</tbody>[/TABLE]
 
Upvote 0

Forum statistics

Threads
1,223,909
Messages
6,175,310
Members
452,634
Latest member
cpostell

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top