SOLVED

Modify macro for CSV

%3CLINGO-SUB%20id%3D%22lingo-sub-1316042%22%20slang%3D%22en-US%22%3EModify%20macro%20for%20CSV%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1316042%22%20slang%3D%22en-US%22%3E%3CP%3EI%20have%20this%20code%2C%20it%20will%20create%20one%20.csv%20file%20with%20header%20and%20row%20for%20each%20row%20in%20a%20table.%3C%2FP%3E%3CP%3EI%20copies%20from%20another%20place%20and%20did%20some%20minor%20modifications%2C%20now%20what%20I%20need%20is%20to%20add%20a%20way%20so%20the%20macro%20will%20name%20each%20file%20based%20on%20the%204th%20column%20per%20row%2C%20and%20not%20just%20add%20numbers%2C%20if%20anyone%20can%20help%20do%20that%20I%20would%20appreciate%2C%20thanks.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EAlso%20im%20trying%20to%20figure%20a%20way%20to%20make%20the%20macro%20write%20in%20the%20actual%20directory%20that%20the%20file%20.xlsm%20is%20placed%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-cpp%22%3E%3CCODE%3ESub%20SaveCSVfiles()%0A%20%20%20%20Dim%20last_row%20As%20Long%0A%20%20%20%20last_row%20%3D%20Cells.Find(What%3A%3D%22*%22%2C%20SearchDirection%3A%3DxlPrevious).Row%0A%20%20%20%20Const%20NumRows%20As%20Integer%20%3D%201%0A%20%20%20%20Const%20HeaderRow%20As%20Integer%20%3D%201%0A%20%20%20%20Const%20FolderPath%20As%20String%20%3D%20%22C%3A%5CUsers%5Cactiveuser%5CDesktop%5CTest%5C%22%20'change%20to%20suit%0A%20%20%20%20%0A%20%20%20%20Dim%20iRow%20As%20Integer%2C%20startSheet%20As%20Worksheet%2C%20tmpSheet%20As%20Worksheet%0A%20%20%20%20%0A%20%20%20%20Application.ScreenUpdating%20%3D%20False%0A%0A%20%20%20%20iRow%20%3D%20HeaderRow%20%2B%201%0A%20%20%20%20Set%20startSheet%20%3D%20ActiveSheet%0A%20%20%20%20Do%20Until%20iRow%20%26gt%3B%20last_row%20%2B%20HeaderRow%0A%20%20%20%20%20%20%20%20Set%20tmpSheet%20%3D%20Worksheets.Add%0A%20%20%20%20%20%20%20%20'copy%20the%20header%0A%20%20%20%20%20%20%20%20startSheet.Range(HeaderRow%20%26amp%3B%20%22%3A%22%20%26amp%3B%20HeaderRow).EntireRow.Copy%20tmpSheet.Range(%22A1%22)%0A%20%20%20%20%20%20%20%20'copy%20the%20data%0A%20%20%20%20%20%20%20%20startSheet.Range(iRow%20%26amp%3B%20%22%3A%22%20%26amp%3B%20(iRow%20%2B%20NumRows%20-%20HeaderRow)).EntireRow.Copy%20tmpSheet.Range(%22A2%22)%0A%20%20%20%20%20%20%20%20'save%20tmpSheet%20to%20CSV%20file%0A%20%20%20%20%20%20%20%20tmpSheet.Move%0A%20%20%20%20%20%20%20%20With%20ActiveWorkbook%0A%20%20%20%20%20%20%20%20%20%20%20%20.SaveAs%20Filename%3A%3DFolderPath%20%26amp%3B%20(iRow%20%2B%20NumRows%20-%20HeaderRow%20-%201)%20%26amp%3B%20%22.csv%22%2C%20FileFormat%3A%3DxlCSVMSDOS%0A%20%20%20%20%20%20%20%20%20%20%20%20.Close%20True%0A%20%20%20%20%20%20%20%20End%20With%0A%20%20%20%20%20%20%20%20iRow%20%3D%20iRow%20%2B%20NumRows%0A%20%20%20%20Loop%0A%20%20%20%20%0A%20%20%20%20Application.ScreenUpdating%20%3D%20True%0A%20%20%20%20%0AEnd%20Sub%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-1316042%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EMacros%20and%20VBA%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1317752%22%20slang%3D%22en-US%22%3ERe%3A%20Modify%20macro%20for%20CSV%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1317752%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F438859%22%20target%3D%22_blank%22%3E%40Ocasio27%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3ETo%20include%204th%20column%20header%20in%20the%20file%20name%20you%20need%20to%20declare%20a%20variable%20first.%20For%20example%3A%3C%2FP%3E%3CP%3E%3CSTRONG%3EDim%20fName%20as%20String%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThen%20you%20have%20to%20read%20out%20the%20value%20of%20the%20header%20and%20assign%20it%20to%20the%20fName.%20This%20could%20be%20something%20like%20this%3A%3C%2FP%3E%3CP%3E%3CSTRONG%3EfName%20%3D%20Range(%22D1%22)%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThen%20you%20insert%20fName%20into%20the%20code%20where%20the%20file%20is%20being%20saved.%20For%20instance%3A%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-cpp%22%3E%3CCODE%3E.SaveAs%20Filename%3A%3DFolderPath%20%26amp%3B%20fName%20%26amp%3B%20etc.....%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EChange%20the%20folder%20path%20to%20where%20you%20want%20to%20save%20the%20CSV%20file%20or%20leave%20it%20out%20if%20you%20want%20to%20save%20it%20into%20your%20current%20active%20folder%2C%20i.e.%20the%20folder%20from%20which%20you%20opened%20your%20workbook.%20That%20should%20work%2C%20I%20believe.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ENow%2C%20having%20said%20all%20of%20this%2C%20I%20have%20not%20been%20able%20to%20test%20any%20of%20it%2C%20since%20I%20don't%20have%20your%20file.%20Try%20tweaking%20the%20code%20along%20these%20lines%20and%20just%20test%20it%20yourself.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1324474%22%20slang%3D%22en-US%22%3ERe%3A%20Modify%20macro%20for%20CSV%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1324474%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F403176%22%20target%3D%22_blank%22%3E%40Riny_van_Eekelen%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThis%20is%20a%20modified%20version%20of%20the%20file%20to%20protect%20sensitive%20information.%20The%20name%20of%20each%20file%20should%20be%20A%20column.%20I%20am%20trying%20but%20it%20keeps%20trowing%20all%20the%20files%20in%20document%20folder%20and%20also%20it%20names%20all%20files%20with%20number%2C%201%2C2%2C3%2C4.....%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1324790%22%20slang%3D%22en-US%22%3ERe%3A%20Modify%20macro%20for%20CSV%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1324790%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F438859%22%20target%3D%22_blank%22%3E%40Ocasio27%3C%2FA%3E%3C%2FP%3E%3CP%3EAdjusted%20your%20code%20a%20little%20bit.%20You%20need%20to%20include%20your%20variables%20%22sWorkbookPath%22%20and%20%22fName%22%20in%20the%20code%20that%20saves%20the%20workbook.%20But%20before%20that%2C%20you%20must%20pick-up%20the%20file%20name%20within%20the%20loop%2C%20so%20that%20it%20starts%20with%20A2%2C%20then%20A3%2C%20then%20A4%20etc.%26nbsp%3B%3C%2FP%3E%3CP%3ESee%20if%20you%20can%20get%20it%20to%20work.%20If%20not%2C%20come%20back!%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-markup%22%3E%3CCODE%3ESub%20SaveCSVfiles()%0A%20%20%20%20Dim%20last_row%20As%20Long%0A%20%20%20%20last_row%20%3D%20Cells.Find(What%3A%3D%22*%22%2C%20SearchDirection%3A%3DxlPrevious).Row%0A%20%20%20%20Dim%20sWorkbookPath%20As%20String%0A%20%20%20%20Dim%20fName%20As%20String%0A%0A%20%20%20%20sWorkbookPath%20%3D%20ActiveWorkbook.Path%0A%20%20%20%20MsgBox%20%22Active%20Workbook%20Path%20is%20%3A%20%22%20%26amp%3B%20sWorkbookPath%2C%20vbInformation%2C%20%22VBAF1%22%0A%20%20%20%20Const%20NumRows%20As%20Integer%20%3D%201%0A%20%20%20%20Const%20HeaderRow%20As%20Integer%20%3D%201%0A%20%20%20%20%0A%20%20%20%20Dim%20iRow%20As%20Integer%2C%20startSheet%20As%20Worksheet%2C%20tmpSheet%20As%20Worksheet%0A%20%20%20%20%0A%20%20%20%20Application.ScreenUpdating%20%3D%20False%0A%0A%20%20%20%20iRow%20%3D%20HeaderRow%20%2B%201%0A%20%20%20%20Set%20startSheet%20%3D%20ActiveSheet%0A%20%20%20%20Do%20Until%20iRow%20%26gt%3B%20last_row%20%2B%20HeaderRow%0A%20%20%20%20%20%20%20%20Set%20tmpSheet%20%3D%20Worksheets.Add%0A%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20fName%20%3D%20startSheet.Range(%22A%22%20%26amp%3B%20iRow)%0A%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20'copy%20the%20header%0A%20%20%20%20%20%20%20%20startSheet.Range(HeaderRow%20%26amp%3B%20%22%3A%22%20%26amp%3B%20HeaderRow).EntireRow.Copy%20tmpSheet.Range(%22A1%22)%0A%20%20%20%20%20%20%20%20'copy%20the%20data%0A%20%20%20%20%20%20%20%20startSheet.Range(iRow%20%26amp%3B%20%22%3A%22%20%26amp%3B%20(iRow%20%2B%20NumRows%20-%20HeaderRow)).EntireRow.Copy%20tmpSheet.Range(%22A2%22)%0A%20%20%20%20%20%20%20%20'save%20tmpSheet%20to%20CSV%20file%0A%20%20%20%20%20%20%20%20tmpSheet.Move%0A%20%20%20%20%20%20%20%20With%20ActiveWorkbook%0A%20%20%20%20%20%20%20%20%20%20%20%20.SaveAs%20Filename%3A%3DsWorkbookPath%20%26amp%3B%20fName%20%26amp%3B%20(iRow%20%2B%20NumRows%20-%20HeaderRow%20-%201)%20%26amp%3B%20%22.csv%22%2C%20FileFormat%3A%3DxlCSVMSDOS%0A%20%20%20%20%20%20%20%20%20%20%20%20.Close%20True%0A%20%20%20%20%20%20%20%20End%20With%0A%20%20%20%20%20%20%20%20iRow%20%3D%20iRow%20%2B%20NumRows%0A%20%20%20%20Loop%0A%20%20%20%20%0A%20%20%20%20Application.ScreenUpdating%20%3D%20True%0A%20%20%20%20%0AEnd%20Sub%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1325133%22%20slang%3D%22en-US%22%3ERe%3A%20Modify%20macro%20for%20CSV%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1325133%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F403176%22%20target%3D%22_blank%22%3E%40Riny_van_Eekelen%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EIts%20working%20perfectly%20except%20that%20for%20some%20strange%20reason%20it%20is%20generating%20the%20.csv%20files%20one%20folder%20up.%3C%2FP%3E%3CP%3EExample%3C%2FP%3E%3CP%3E%2FfirstF%2FSecondF%20%3D%20Location%20of%20.xlsm%3C%2FP%3E%3CP%3E%2Ffirstf%2F%20%3D%20Location%20is%20placing%20the%20.csv%20files%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThis%20might%20be%20useful%20someday%2C%20but%20for%20now%2C%20how%20can%20i%20fix%20this.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1325172%22%20slang%3D%22en-US%22%3ERe%3A%20Modify%20macro%20for%20CSV%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1325172%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F438859%22%20target%3D%22_blank%22%3E%40Ocasio27%3C%2FA%3E%26nbsp%3BSorry%2C%20can't%20really%20help%20out%20there.%20I'm%20running%20the%20PC%20version%20of%20Excel%2C%20virtually%20on%20a%20Mac.%20It%20saves%20file%20in%20a%20location%20on%20the%20virtual%20PC.%20Difficult%20for%20me%20to%20determine%20how%20this%20would%20work%20on%20a%20real%20PC.%3C%2FP%3E%3C%2FLINGO-BODY%3E
Highlighted
Frequent Contributor

I have this code, it will create one .csv file with header and row for each row in a table.

I copies from another place and did some minor modifications, now what I need is to add a way so the macro will name each file based on the 4th column per row, and not just add numbers, if anyone can help do that I would appreciate, thanks.

 

Also im trying to figure a way to make the macro write in the actual directory that the file .xlsm is placed

 

 

Sub SaveCSVfiles()
    Dim last_row As Long
    last_row = Cells.Find(What:="*", SearchDirection:=xlPrevious).Row
    Const NumRows As Integer = 1
    Const HeaderRow As Integer = 1
    Const FolderPath As String = "C:\Users\activeuser\Desktop\Test\" 'change to suit
    
    Dim iRow As Integer, startSheet As Worksheet, tmpSheet As Worksheet
    
    Application.ScreenUpdating = False

    iRow = HeaderRow + 1
    Set startSheet = ActiveSheet
    Do Until iRow > last_row + HeaderRow
        Set tmpSheet = Worksheets.Add
        'copy the header
        startSheet.Range(HeaderRow & ":" & HeaderRow).EntireRow.Copy tmpSheet.Range("A1")
        'copy the data
        startSheet.Range(iRow & ":" & (iRow + NumRows - HeaderRow)).EntireRow.Copy tmpSheet.Range("A2")
        'save tmpSheet to CSV file
        tmpSheet.Move
        With ActiveWorkbook
            .SaveAs Filename:=FolderPath & (iRow + NumRows - HeaderRow - 1) & ".csv", FileFormat:=xlCSVMSDOS
            .Close True
        End With
        iRow = iRow + NumRows
    Loop
    
    Application.ScreenUpdating = True
    
End Sub

 

 

 

5 Replies
Highlighted

@Ocasio27 

To include 4th column header in the file name you need to declare a variable first. For example:

Dim fName as String

 

Then you have to read out the value of the header and assign it to the fName. This could be something like this:

fName = Range("D1")

 

Then you insert fName into the code where the file is being saved. For instance:

.SaveAs Filename:=FolderPath & fName & etc.....

 

Change the folder path to where you want to save the CSV file or leave it out if you want to save it into your current active folder, i.e. the folder from which you opened your workbook. That should work, I believe.

 

Now, having said all of this, I have not been able to test any of it, since I don't have your file. Try tweaking the code along these lines and just test it yourself.

 

Highlighted

@Riny_van_Eekelen 

 

This is a modified version of the file to protect sensitive information. The name of each file should be A column. I am trying but it keeps trowing all the files in document folder and also it names all files with number, 1,2,3,4.....

Highlighted
Best Response confirmed by Ocasio27 (Frequent Contributor)
Solution

@Ocasio27

Adjusted your code a little bit. You need to include your variables "sWorkbookPath" and "fName" in the code that saves the workbook. But before that, you must pick-up the file name within the loop, so that it starts with A2, then A3, then A4 etc. 

See if you can get it to work. If not, come back!

 

Sub SaveCSVfiles()
    Dim last_row As Long
    last_row = Cells.Find(What:="*", SearchDirection:=xlPrevious).Row
    Dim sWorkbookPath As String
    Dim fName As String

    sWorkbookPath = ActiveWorkbook.Path
    MsgBox "Active Workbook Path is : " & sWorkbookPath, vbInformation, "VBAF1"
    Const NumRows As Integer = 1
    Const HeaderRow As Integer = 1
    
    Dim iRow As Integer, startSheet As Worksheet, tmpSheet As Worksheet
    
    Application.ScreenUpdating = False

    iRow = HeaderRow + 1
    Set startSheet = ActiveSheet
    Do Until iRow > last_row + HeaderRow
        Set tmpSheet = Worksheets.Add
        
        fName = startSheet.Range("A" & iRow)
        
        'copy the header
        startSheet.Range(HeaderRow & ":" & HeaderRow).EntireRow.Copy tmpSheet.Range("A1")
        'copy the data
        startSheet.Range(iRow & ":" & (iRow + NumRows - HeaderRow)).EntireRow.Copy tmpSheet.Range("A2")
        'save tmpSheet to CSV file
        tmpSheet.Move
        With ActiveWorkbook
            .SaveAs Filename:=sWorkbookPath & fName & (iRow + NumRows - HeaderRow - 1) & ".csv", FileFormat:=xlCSVMSDOS
            .Close True
        End With
        iRow = iRow + NumRows
    Loop
    
    Application.ScreenUpdating = True
    
End Sub

 

@Riny_van_Eekelen 

 

Its working perfectly except that for some strange reason it is generating the .csv files one folder up.

Example

/firstF/SecondF = Location of .xlsm

/firstf/ = Location is placing the .csv files

 

This might be useful someday, but for now, how can i fix this.

Highlighted

@Ocasio27 Sorry, can't really help out there. I'm running the PC version of Excel, virtually on a Mac. It saves file in a location on the virtual PC. Difficult for me to determine how this would work on a real PC.