silverlucky5
New Member
- Joined
- Sep 8, 2009
- Messages
- 35
Hi, I hope someone out there can help me out with this problem. Here is what I am attempting to do:
1. I have a workbook that contains 4 worksheets.
2. The worksheets are named as follows:
1<sup>st</sup> worksheet name = Medical
2<sup>nd</sup> worksheet name = RX
3<sup>rd</sup> worksheet name = Dental
4<sup>th</sup> worksheet name = Vision
3. I want to save each worksheet as a separate csv file with the date and time as part of the file name..
4. At first I thought that using the worksheet name would be the best way to name the new csv file.
For example, the csv file for the first worksheet would be called “C:\Medical yyyymmdd-hhmmss .csv. I used the code below (cutting and pasting from various snippets of code I found) and that worked fine.
5. But it turns out that the worksheet names cannot be used to name the csv files and I need to provide specific names for each worksheet.
6. So I thought that I could embed some “If..Then..Else” statements while still in the For..Next Loop. For example, I want to do something like this:
If first worksheet (i = 1) THEN save as a certain name ELSE if worksheet is the second worksheet (i=2) THEN save as another name ELSE, etc.
7. My problem is that I am quite a novice and just learning VBA and don’t know how to embed the “If..Then..Else” statements while still in the For..Next loop or even if I CAN embed it. Any help you can provide will be greatly appreciated! Thanks..
Here is the code I am using
Option Explicit
Sub SplitWorkbookTest()
Application.DisplayAlerts = False
Dim i As Long
For i = 1 To ActiveWorkbook.Worksheets.Count
Sheets(i).Copy
With ActiveSheet.UsedRange
.Value = .Value
End With
ActiveWorkbook.SaveAs Filename:="C:\" & ActiveSheet.Name & " " & _
Format(Date, "yyyymmdd") & "-" & Format(Time, "hhnnss") & ".csv", FileFormat:=xlCSV
ActiveWindow.Close
Next i
ActiveWorkbook.Close False
Application.DisplayAlerts = True
End Sub
[FONT="]
[/FONT]
1. I have a workbook that contains 4 worksheets.
2. The worksheets are named as follows:
1<sup>st</sup> worksheet name = Medical
2<sup>nd</sup> worksheet name = RX
3<sup>rd</sup> worksheet name = Dental
4<sup>th</sup> worksheet name = Vision
3. I want to save each worksheet as a separate csv file with the date and time as part of the file name..
4. At first I thought that using the worksheet name would be the best way to name the new csv file.
For example, the csv file for the first worksheet would be called “C:\Medical yyyymmdd-hhmmss .csv. I used the code below (cutting and pasting from various snippets of code I found) and that worked fine.
5. But it turns out that the worksheet names cannot be used to name the csv files and I need to provide specific names for each worksheet.
6. So I thought that I could embed some “If..Then..Else” statements while still in the For..Next Loop. For example, I want to do something like this:
If first worksheet (i = 1) THEN save as a certain name ELSE if worksheet is the second worksheet (i=2) THEN save as another name ELSE, etc.
7. My problem is that I am quite a novice and just learning VBA and don’t know how to embed the “If..Then..Else” statements while still in the For..Next loop or even if I CAN embed it. Any help you can provide will be greatly appreciated! Thanks..
Here is the code I am using
Option Explicit
Sub SplitWorkbookTest()
Application.DisplayAlerts = False
Dim i As Long
For i = 1 To ActiveWorkbook.Worksheets.Count
Sheets(i).Copy
With ActiveSheet.UsedRange
.Value = .Value
End With
ActiveWorkbook.SaveAs Filename:="C:\" & ActiveSheet.Name & " " & _
Format(Date, "yyyymmdd") & "-" & Format(Time, "hhnnss") & ".csv", FileFormat:=xlCSV
ActiveWindow.Close
Next i
ActiveWorkbook.Close False
Application.DisplayAlerts = True
End Sub
[FONT="]
[/FONT]