VBA to split xlsx into multiple CSV files based in format

diwio

New Member
Joined
Jun 14, 2023
Messages
3
Office Version
  1. 2019
Platform
  1. Windows
Hi all,

I'm struggling with this, I need to split this document so I get a CSV for each unique site formatted as per below.. Users will be in multiple sites

Current
Export DataUsernameSite NameCurrent PermNew Group
site_exampleSite_SiteManagerexampleUser
exampleSite
siteManagerGG_exampleSite_exampleUser_Modify
site_exampleSite_SiteReadexampleUser1
exampleSiteBlah
siteReadGG_exampleSiteBlah_exampleUser_Read
site_exampleSite_SiteContributeexampleUser2exampleSitesiteContributeGG_exampleSite_exampleUser2_Modify
site_exampleSiteBlah_SiteManagerexampleUserBlah
exampleSiteBlah
siteContributeGG_exampleSiteBlah_exampleUserBlah_Modify
site_exampleSiteBlah_SiteReadexampleUserBlah1
exampleSiteBlah
siteReadGG_exampleSiteBlah_exampleUserBlah1_Read
site_exampleSiteBlah_SiteContributeexampleUserBlah2exampleSitesiteContributeGG_exampleSite_exampleUser2Blah_Modify

Desired
CSV file for each unique siteName formatted as per:
Group (contains New Group data)User-Name (Contains Username data)
GG_exampleSite_exampleUser_ModifyexampleUser
GG_exampleSiteBlah_exampleUser_ReadexampleUser1


Edit, the end goal is to do an AD Import to add users to their respective groups based on the sitename and permission level required.
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
try this,
VBA Code:
Sub output_to_textfile()
mypath = "C:\test\"

For r = 2 To Cells(Rows.Count, "A").End(xlUp).Row
    data = Cells(r, "E") & "," & Cells(r, "B")
    Open mypath & Cells(r, "C") & ".csv" For Append As #1
    Print #1, data
    Close #1

Next r
 
MsgBox "Done"

End Sub
 
Upvote 0
try this,
VBA Code:
Sub output_to_textfile()
mypath = "C:\test\"

For r = 2 To Cells(Rows.Count, "A").End(xlUp).Row
    data = Cells(r, "E") & "," & Cells(r, "B")
    Open mypath & Cells(r, "C") & ".csv" For Append As #1
    Print #1, data
    Close #1

Next r
 
MsgBox "Done"

End Sub
That looks to work perfectly thanks!

On second thoughts, it'd probably be better to have them generated as separate csv's like they are currently, and then also combined into one big one, would make AD import MUCH easier? Any chance of adding that functionality - Much appreciated!
 
Upvote 0
Sub output_to_textfile()
mypath = "C:\test\"
all_text=""

For r = 2 To Cells(Rows.Count, "A").End(xlUp).Row
data = Cells(r, "E") & "," & Cells(r, "B")
Open mypath & Cells(r, "C") & ".csv" For Append As #1
Print #1, data
Close #1
all_text=alltext & data & vbcr & vblf
Next r

Open mypath & "all.csv" For Append As #1
Print #1, all_text
Close #1


MsgBox "Done"

End Sub
 
Upvote 0
Sub output_to_textfile()
mypath = "C:\test\"
all_text=""

For r = 2 To Cells(Rows.Count, "A").End(xlUp).Row
data = Cells(r, "E") & "," & Cells(r, "B")
Open mypath & Cells(r, "C") & ".csv" For Append As #1
Print #1, data
Close #1
all_text=alltext & data & vbcr & vblf
Next r

Open mypath & "all.csv" For Append As #1
Print #1, all_text
Close #1


MsgBox "Done"

End Sub
In the all.csv output it doesn't seem to insert the data correctly.

Left side of screenshot is the all.csv vs the right side which shows the individual sheets.
 

Attachments

  • Untitled.png
    Untitled.png
    91.6 KB · Views: 25
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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