views:

264

answers:

4

Below was a macro provided, but I need to know how to do some adjustments to save it to the file name fixed to a specific worksheet, and specific cell with dates( eg. worksheet name is: AA on cell "B2" with ZZ ). So the filename will be ZZ AA 23122008.csv

Thanks in advance.


Public Sub SaveWorksheetsAsCsv()

Dim WS As Excel.Worksheet
Dim SaveToDirectory As String

Dim CurrentWorkbook As String
Dim CurrentFormat As Long

CurrentWorkbook = ThisWorkbook.FullName
CurrentFormat = ThisWorkbook.FileFormat
' Store current details for the workbook

SaveToDirectory = "C:\temp\"

For Each WS In ThisWorkbook.Worksheets
  WS.SaveAs SaveToDirectory & WS.Name, xlCSV
Next

Application.DisplayAlerts = False
ThisWorkbook.SaveAs filename:=CurrentWorkbook, FileFormat:=CurrentFormat
Application.DisplayAlerts = True
' Temporarily turn alerts off to prevent the user being prompted
'  about overwriting the original file.

End Sub


+1  A: 

U can initialize

ThisDate  = Format(Now(), "ddmmmyy")

and if you want to take the value of WS from second row, second column then

WS.SaveAs SaveToDirectory & WS.Name & WS.Cells(2,2) & ThisDate , xlCSV

will be your answer.. In more details...

Public Sub SaveWorksheetsAsCsv()

Dim WS As Excel.Worksheet
Dim SaveToDirectory As String

Dim CurrentWorkbook As String
Dim CurrentFormat As Long

CurrentWorkbook = ThisWorkbook.FullName

CurrentFormat = ThisWorkbook.FileFormat

' Store current details for the workbook '
ThisDate = Format(Now(), "ddmmmyy") 

SaveToDirectory = "C:\temp\"

For Each WS In ThisWorkbook.Worksheets
  WS.SaveAs SaveToDirectory & WS.Name & ThisDate & WS.Cells(2,2) , xlCSV
Next

......

and so on.

lakshmanaraj
A: 

Thank you, lakshmanaraj for your quick response. But do you mind to elaborate where to initialize? or which line do I add to.

Before save to directory add thisdate = ....
lakshmanaraj
ie) Before SaveToDirectory = "C:\temp\" add the line ThisDate = Format(Now(), "ddmmmyy")
lakshmanaraj
The style of Stack overflow is to use comments for responses such as this. 'Answers' is limited to answers to the original question. If you check your code, you will see you have a line ws.SaveAs, subsitute lakshmanaraj's suggestion.
Remou
If you want to use Fixed worksheet instead of WS.Cells(2,2) then use Sheets(index).Cells(2,2) where index is your fixed integer of worksheet index.
lakshmanaraj
A: 

After adding the line for initialise the date and added the line WS.SaveAs SaveToDirectory & WS.Name & WS.Cells(2,2) & ThisDate , xlCSV I get an error 1004 application defined or object defined error. If I am not mistaken from the code, will it take each worksheet cell (2,2) and save it as per the work sheet. Can I use a fixed worksheet?

Please use comments for comments such as this.
Remou
Noted. but I could not add comments earlier as I do not have enough points. Thanking you in advance.
Then edit, your question. ;-)
Gamecat
Oh I see. Thank you for the advise.
Yes you can use fixed worksheet asSheets(Index).cells(2,2).. Where index can be a fixed integer such as "2"...
lakshmanaraj
Noted, but I still get error 1004. Anything to go on?
can u look at www.mrexcel.com can u look at http://www.mrexcel.com/forum/showthread.php?t=50647or some other vba site to look for such errors.
lakshmanaraj
A: 

I have amended the codes and now it does save the different worksheet to a *.csv file. But now it keeps saving my current worksheets in the workbook to the *.csv name. How do I stop this?

Dim WS As Excel.Worksheet Dim SaveToDirectory As String

Dim CurrentWorkbook As String Dim CurrentFormat As Long Dim myName As String myName = myName & Application.Cells(2, 2) 'cell B2

MyMonth = Left(MonthName(Month(Date)), 3) MyDay = Day(Date)

CurrentWorkbook = ThisWorkbook.FullName

CurrentFormat = ThisWorkbook.FileFormat

' Store current details for the workbook '

SaveToDirectory = "C:\temp\"

For Each WS In ThisWorkbook.Worksheets WS.SaveAs SaveToDirectory & myName & Left(MonthName(Month(Date)), 3) & Day(Date) & WS.Name, xlCSV

Next

Application.DisplayAlerts = False ActiveWorkbook.SaveAs Filename:=CurrentWorkbook, FileFormat:=CurrentFormat

Application.DisplayAlerts = False

' Temporarily turn alerts off to prevent the user being prompted ' about overwriting the original file.