Save XLSM and CSV at same time

Dazzawm

Well-known Member
Joined
Jan 24, 2011
Messages
3,714
Office Version
  1. 365
Platform
  1. Windows
Is it possible that when I close a macro enabled workbook to one location it can be saved as a CSV in a different location at the sametime?
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
I would say yes, you would have to look at the workbook options in the VBA screen and add some code to Before Save or After Save.

In the VBA Screen (use Alt + F11) double click on the project window where it states ThisWorkBook then change the drop down from Object to Workbook and on the right change the procedure to BeforeSave or AfterSave. I asssume you would Save as A copy for the CSV file before or After Saving the Workbook.

Or do you want to use a Macro in a modlue sheet to give you a button or something to save as both?
 
Upvote 0
I dont really understand what you mean but at the moment I have 20 different workbooks that perform certain actions via code before they close and are saved as XLSM. I need some code to save also as CSV to a different location at the same time.
 
Upvote 0
Who created your code that you run?

Try recording a macro to use the Save As and then select CSV and give a name and select a location.

Then in the same Macro do the same for the Macro Saved Workbook it will then give you the code you need to add to your current code.....
 
Upvote 0
Its some code that has been written for me and some I have recorded and added in. I have tried as you suggested but it doesnt work properly because it looks like in conflicts with some code already.
 
Upvote 0
OK add your code to your thread and state the workbook name you want to use and CSV file name you want to use as well.

If we get one working then you can adapt it.
 
Upvote 0
The output names will be the same, and this code below works for all the files as I dont think they are workbook name specific.

Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
ActiveSheet.Name = "Sheet1"
Sheets("Sheet1").Range("H" & Rows.Count).End(xlUp).Value = Date
Dim lr As Long
lr = Cells.Find(What:="*", SearchDirection:=xlPrevious, SearchOrder:=xlByRows).Row
Range("A2:A3").AutoFill Destination:=Range("A2:A" & lr)
Dim ws As Worksheet
    Dim d As Object, colData As String, colOut As String
    Dim x&, zr&, k, e, t!
    
    t = Timer
    
    Set ws = ActiveSheet    '
    colData = "C"           'Column of data to count
    colOut = "D"            'Output column for running count
    
    Set d = CreateObject("scripting.dictionary")
    zr = ws.Cells(Rows.Count, colData).End(xlUp).Row
    e = ws.Range(ws.Cells(2, colData), ws.Cells(zr, colData))
    ReDim k(1 To UBound(e, 1), 1 To 1)
    For x = LBound(e, 1) To UBound(e, 1)
        If d.exists(e(x, 1)) Then
            d(e(x, 1)) = d(e(x, 1)) + 1
            k(x, 1) = d(e(x, 1))
        Else
            d(e(x, 1)) = 1
            k(x, 1) = 1
        End If
    Next x
    
    ws.Range(ws.Cells(2, colOut), ws.Cells(zr, colOut)) = k
    'MsgBox Format(Timer - t, "0.00 secs"), , "Process Time"
Cells.Select
Cells.EntireColumn.AutoFit
    With Selection.Interior
        .Pattern = xlNone
        .TintAndShade = 0
        .PatternTintAndShade = 0
    End With
    
Range("A1").Select
End Sub
 
Upvote 0

Forum statistics

Threads
1,203,728
Messages
6,057,021
Members
444,902
Latest member
ExerciseInFutility

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