+ Reply to Thread
Results 1 to 12 of 12

Save the Output in Separate CSV Files

Hybrid View

aashishni Save the Output in Separate... 10-18-2018, 03:57 PM
Leith Ross Re: Save the Output in... 10-18-2018, 10:34 PM
aashishni Re: Save the Output in... 10-19-2018, 08:20 AM
Kaper Re: Save the Output in... 10-19-2018, 09:24 AM
aashishni Re: Save the Output in... 10-19-2018, 10:10 AM
Leith Ross Re: Save the Output in... 10-19-2018, 10:49 AM
aashishni Re: Save the Output in... 10-19-2018, 10:58 AM
Leith Ross Re: Save the Output in... 10-19-2018, 11:05 AM
aashishni Re: Save the Output in... 10-19-2018, 12:43 PM
Leith Ross Re: Save the Output in... 10-19-2018, 02:50 PM
aashishni Re: Save the Output in... 10-20-2018, 02:47 PM
Leith Ross Re: Save the Output in... 10-20-2018, 03:53 PM
  1. #1
    Registered User
    Join Date
    02-24-2014
    Location
    NOIDA
    MS-Off Ver
    Excel 2007
    Posts
    22

    Question Save the Output in Separate CSV Files

    Hi All,
    I have the below script which produces output in different cells , I want to be able to save separate CSV files of the output generated.Kindly help. (File Attached)

    Sub test()
        Dim a, e, i As Long, myOS, x, y, n As Long, dic As Object
        Set dic = CreateObject("Scripting.Dictionary")
        dic.CompareMode = 1
        myOS = Array(Array("WIN", 50), Array("UNIX", 100), Array("AIX", 100), _
                     Array("Linux", 100), Array("Solaris", 100), Array("etc", 100))
        For Each e In myOS
            dic(e(0)) = Array(Empty, 0, e(1))
        Next
        a = Cells(1).CurrentRegion.Value
        For i = 2 To UBound(a, 1)
            If dic.exists(a(i, 2)) Then
                If IsEmpty(dic(a(i, 2))(0)) Then
                    ReDim x(1 To 1), y(1 To dic(a(i, 2))(2))
                Else
                    x = dic(a(i, 2))(0): y = x(UBound(x))
                End If
                n = dic(a(i, 2))(1) + 1
                If n > dic(a(i, 2))(2) Then
                    ReDim Preserve x(1 To UBound(x) + 1)
                    ReDim y(1 To dic(a(i, 2))(2)): n = 1
                End If
                y(n) = a(i, 1): x(UBound(x)) = y: dic(a(i, 2)) = Array(x, n, dic(a(i, 2))(2))
            End If
        Next
        Columns("e").ClearContents: n = 3
        For Each e In dic
            x = dic(e)(0)
            If Not IsEmpty(x) Then
                For i = 1 To UBound(x)
                    n = n + 1: Cells(n, "e").Value = e & " " & i
                    n = n + 1: Cells(n, "e").Value = Join(x(i), ", ")
                Next
                n = n + 1
            End If
        Next
    End Sub
    Attached Files Attached Files
    Last edited by Leith Ross; 10-18-2018 at 06:22 PM. Reason: Added Code Tags

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Save the Output in Separate CSV Files

    Hello aashishni ,

    This macro will create CSV files based on the OS name and the limit for the IP addresses to be saved.

    Sub SaveAsCSV()
    
        Dim Cell    As Range
        Dim Counts  As Object
        Dim Data    As Variant
        Dim Dict    As Object
        Dim File    As String
        Dim IP      As String
        Dim Item    As Variant
        Dim Key     As Variant
        Dim Limits  As New Collection
        Dim OS      As String
        Dim Path    As String
        Dim Rng     As Range
        
            Path = "C:\Test"
            Path = IIf(Right(Path, 1) <> "\", Path & "\", Path)
            
            Set Counts = CreateObject("Scripting.Dictionary")
                Counts.CompareMode = vbTextCompare
                
            For Each Item In Array("Win 50", "UNIX 100", "AIX 100", "Linux 100", "Solaris 100")
                Item = Split(Item, " ")
                Limits.Add CLng(Item(1)), Item(0)
                Counts.Add Item(0), 1
            Next Item
            
            Set Rng = Range("A1").CurrentRegion
            Set Rng = Intersect(Rng, Rng.Offset(1, 0))
            Set Rng = Rng.Resize(ColumnSize:=1)
            
            Set Dict = CreateObject("Scripting.Dictionary")
                Dict.CompareMode = vbTextCompare
                
                For Each Cell In Rng
                    OS = Trim(Cell.Offset(0, 1))
                    IP = Trim(Cell)
                    If OS <> "" Then
                        If Not Dict.Exists(OS) Then
                            ReDim Data(1)
                            Data(0) = 1
                            Data(1) = IP
                            Dict.Add OS, Data
                        Else
                            Data = Dict(OS)
                            Data(0) = Data(0) + 1
                            Data(1) = Data(1) & "," & IP
                            
                            If Data(0) = Limits(OS) Then
                                File = Path & OS & Counts(OS) & ".csv"
                                Open File For Output As #1
                                    Print #1, OS & Counts(OS)
                                    Print #1, Data(1)
                                Close #1
                                Counts(OS) = Counts(OS) + 1
                                Dict.Remove OS
                            Else
                                Dict(OS) = Data
                            End If
                       End If
                    End If
                Next Cell
            
            For Each Key In Dict.Keys
                Data = Dict(Key)
                File = Path & Key & Counts(Key) & ".csv"
                Open File For Output As #1
                    Print #1, Key & Counts(Key)
                    Print #1, Data(1)
                Close #1
            Next Key
            
    End Sub
    Attached Files Attached Files
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  3. #3
    Registered User
    Join Date
    02-24-2014
    Location
    NOIDA
    MS-Off Ver
    Excel 2007
    Posts
    22

    Re: Save the Output in Separate CSV Files

    Hi Leith,
    Thanks a lot for your inputs, however I am getting a runtime error when I execute the macro, the error prompt being "RUN TIME ERROR '76'". and on debugging it takes to "Open File For Output As #1" in the code.
    Please suggest.

    I downloaded the file you attached and executed the same.

    Thanks

  4. #4
    Forum Guru Kaper's Avatar
    Join Date
    12-14-2013
    Location
    Warsaw, Poland
    MS-Off Ver
    most often: Office 365 in Windows environment
    Posts
    8,864

    Re: Save the Output in Separate CSV Files

    Hi aashishni,

    Have you seen Rule 8 in: https://www.excelforum.com/forum-rul...rum-rules.html
    Please do follow it.
    Best Regards,

    Kaper

  5. #5
    Registered User
    Join Date
    02-24-2014
    Location
    NOIDA
    MS-Off Ver
    Excel 2007
    Posts
    22

    Re: Save the Output in Separate CSV Files

    Hi Kaper,

    thanks for pointing it out..sure will follow it !

    Thanks

  6. #6
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Save the Output in Separate CSV Files

    Hello aashishni ,

    You need to change the variable Path from "C:\Test" to the folder where you want the files to be saved.

  7. #7
    Registered User
    Join Date
    02-24-2014
    Location
    NOIDA
    MS-Off Ver
    Excel 2007
    Posts
    22

    Re: Save the Output in Separate CSV Files

    Hi Leith,

    Ok..I got the error resolved by changing the path name however the output is not correct, it is putting the IP addresses in one row multiple cells whereas we need it in one cell separated by a comma.
    Can you please check that ? And is it possible to get a prompt when the macro has completed the execution?

    Thanks

  8. #8
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Save the Output in Separate CSV Files

    Hello aashishni ,

    Here is the amended macro. Change Path before you run the macro.

    Sub SaveAsCSV()
    
        Dim Cell    As Range
        Dim Counts  As Object
        Dim Data    As Variant
        Dim Dict    As Object
        Dim File    As String
        Dim IP      As String
        Dim Item    As Variant
        Dim Key     As Variant
        Dim Limits  As New Collection
        Dim OS      As String
        Dim Path    As String
        Dim Rng     As Range
        
            Path = "C:\Test"
            Path = IIf(Right(Path, 1) <> "\", Path & "\", Path)
            
            Set Counts = CreateObject("Scripting.Dictionary")
                Counts.CompareMode = vbTextCompare
                
            For Each Item In Array("Win 50", "UNIX 100", "AIX 100", "Linux 100", "Solaris 100")
                Item = Split(Item, " ")
                Limits.Add CLng(Item(1)), Item(0)
                Counts.Add Item(0), 1
            Next Item
            
            Set Rng = Range("A1").CurrentRegion
            Set Rng = Intersect(Rng, Rng.Offset(1, 0))
            Set Rng = Rng.Resize(ColumnSize:=1)
            
            Set Dict = CreateObject("Scripting.Dictionary")
                Dict.CompareMode = vbTextCompare
                
                For Each Cell In Rng
                    OS = Trim(Cell.Offset(0, 1))
                    IP = Trim(Cell)
                    If OS <> "" Then
                        If Not Dict.Exists(OS) Then
                            ReDim Data(1)
                            Data(0) = 1
                            Data(1) = IP
                            Dict.Add OS, Data
                        Else
                            Data = Dict(OS)
                            Data(0) = Data(0) + 1
                            Data(1) = Data(1) & "," & IP
                            
                            If Data(0) = Limits(OS) Then
                                File = Path & OS & Counts(OS) & ".csv"
                                Open File For Output As #1
                                    Print #1, OS & Counts(OS)
                                    Print #1, Chr(34) & Data(1) & Chr(34)
                                Close #1
                                Counts(OS) = Counts(OS) + 1
                                Dict.Remove OS
                            Else
                                Dict(OS) = Data
                            End If
                       End If
                    End If
                Next Cell
            
            For Each Key In Dict.Keys
                Data = Dict(Key)
                File = Path & Key & Counts(Key) & ".csv"
                Open File For Output As #1
                    Print #1, Key & Counts(Key)
                    Print #1, Chr(34) & Data(1) & Chr(34)
                Close #1
            Next Key
            
            MsgBox "CSV Files have been saved."
            
    End Sub

  9. #9
    Registered User
    Join Date
    02-24-2014
    Location
    NOIDA
    MS-Off Ver
    Excel 2007
    Posts
    22

    Re: Save the Output in Separate CSV Files

    It works now !!! Yay !!!
    However, I used a data where I had multiple OS mentioned and then the CSV file for AIX did not populate correctly, had incorrect values (IP address) and multiple 00,00,00 entries.
    Any suggestions ?
    Apologies if I am bugging you too much but this is really helpful for me :-)
    Attached Files Attached Files

  10. #10
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Save the Output in Separate CSV Files

    Hello aashishni,

    Here is the updated macro and workbook. The macro let's you select the destination folder. I change it back it want.

    Sub SaveAsCSV()
    
        Dim Cell    As Range
        Dim Counts  As Object
        Dim Data    As Variant
        Dim Dict    As Object
        Dim File    As String
        Dim IP      As String
        Dim Item    As Variant
        Dim Key     As Variant
        Dim Limits  As New Collection
        Dim OS      As String
        Dim Path    As String
        Dim Rng     As Range
        
            With Application.FileDialog(msoFileDialogFolderPicker)
                If .Show Then Path = .SelectedItems(1) Else Exit Sub
            End With
            
            Path = IIf(Right(Path, 1) <> "\", Path & "\", Path)
            
            Set Counts = CreateObject("Scripting.Dictionary")
                Counts.CompareMode = vbTextCompare
                
            For Each Item In Array("Win 50", "UNIX 100", "AIX 100", "Linux 100", "Solaris 100")
                Item = Split(Item, " ")
                Limits.Add CLng(Item(1)), Item(0)
                Counts.Add Item(0), 1
            Next Item
            
            Set Rng = Range("A1").CurrentRegion
            Set Rng = Intersect(Rng, Rng.Offset(1, 0))
            Set Rng = Rng.Resize(ColumnSize:=1)
            
            Set Dict = CreateObject("Scripting.Dictionary")
                Dict.CompareMode = vbTextCompare
                
                For Each Cell In Rng
                    OS = Trim(Cell.Offset(0, 1))
                    IP = Trim(Cell)
                    If IP <> "" Then
                        If OS <> "" Then
                            If Not Dict.Exists(OS) Then
                                ReDim Data(1)
                                Data(0) = 1
                                Data(1) = IP
                                Dict.Add OS, Data
                            Else
                                Data = Dict(OS)
                                Data(0) = Data(0) + 1
                                Data(1) = Data(1) & "," & IP
                            
                                If Data(0) = Limits(OS) Then
                                    File = Path & OS & Counts(OS) & ".csv"
                                    Open File For Output As #1
                                        Print #1, OS & Counts(OS)
                                        Print #1, Chr(34) & Data(1) & Chr(34)
                                    Close #1
                                    Counts(OS) = Counts(OS) + 1
                                    Dict.Remove OS
                                Else
                                    Dict(OS) = Data
                                End If
                            End If
                        End If
                    End If
                Next Cell
            
            For Each Key In Dict.Keys
                Data = Dict(Key)
                File = Path & Key & Counts(Key) & ".csv"
                Open File For Output As #1
                    Print #1, Key & Counts(Key)
                    Print #1, Chr(34) & Data(1) & Chr(34)
                Close #1
            Next Key
            
            MsgBox "CSV files are saved in " & Chr(34) & Path & Chr(34)
            
    End Sub
    Attached Files Attached Files

  11. #11
    Registered User
    Join Date
    02-24-2014
    Location
    NOIDA
    MS-Off Ver
    Excel 2007
    Posts
    22

    Re: Save the Output in Separate CSV Files

    WOW !!!! That works wonderfully ! I owe you a lot !
    Resolving the thread , will reach out to the forum again in case of any help required !

  12. #12
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Save the Output in Separate CSV Files

    Hello aashishni,

    Another satisfied customer! Glad I could help.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. How to browse the path for the input files and save the output using a userform
    By priyankaseshadri in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 03-22-2014, 03:14 PM
  2. Divide worsheet and save as separate files
    By Colin Hayes in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 09-06-2005, 12:05 PM
  3. Divide worsheet and save as separate files
    By Colin Hayes in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 09-06-2005, 04:05 AM
  4. Divide worsheet and save as separate files
    By Colin Hayes in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 09-06-2005, 03:05 AM
  5. Divide worsheet and save as separate files
    By Colin Hayes in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 09-06-2005, 02:05 AM
  6. Divide worsheet and save as separate files
    By Colin Hayes in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 09-05-2005, 11:05 PM
  7. Divide worsheet and save as separate files
    By Colin Hayes in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 09-05-2005, 10:05 PM
  8. How do I save sheets in a workbook to separate files?
    By Omzala in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 01-13-2005, 03:06 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1