Extract data to multiple worksheets/books based on Column Value

Anzer_ke

New Member
Joined
Mar 8, 2016
Messages
2
Hi, I'm something of a script kiddy and my attempts to find a macro that fits my needs have failed.

The files look like this:

TimeT2RT2LT3RT3LA1RA1LA2RA2LA3RA3LA4RA4LA5RA5LA6RA6LA7RA7LA8RA8LTrackName
00.0909240.1238390.1510380.1507560.1781180.1785470.2053240.2167120.2237210.2571170.259410.2264610.3183880.2619290.3091830.3665770.257720.2918690.1583940.12345p
0.20.0674860.0968050.1391450.1225910.154350.1343050.1758380.167670.1904530.2253370.2139360.1865070.26080.2171030.2579890.3291310.3027620.2921930.1732790.141501p
0.40.0625250.0705610.1208790.0979680.1432020.1198930.1508510.1426180.1651710.1811650.1821290.1491840.2260560.1837690.2143670.2752240.2690770.2840950.1722110.136847p
0.60.077510.0558930.1382360.0743050.1494750.0927970.1422620.1134040.1472180.1534590.163710.1261340.1848420.1525930.1755870.2285910.2304760.2419430.1506180.124349p
0.80.1096740.0585060.1752930.0777670.1783360.087410.1693530.1227910.1530060.1474460.1408790.1097450.180490.1249040.1511850.1956720.1778950.1906950.1352470.099613p
10.6723060.7069440.7487820.6650140.7242590.6516510.6503780.6461020.6184940.621720.5851960.5291460.5766210.5296240.5195480.5877890.5648070.5911670.7627290.715483p
1.20.1724740.0787450.291410.0700210.2730520.0967940.250170.1266990.196860.130370.1592940.0669240.1615580.0846130.1062140.1200560.1212460.1035190.1024160.041954p
1.40.2169020.071310.3506090.0727910.3198960.079940.2809760.1214830.2129340.1080870.1566920.0511170.1562480.0515510.0762820.0925870.0697310.0507750.0777210.014561right turn
1.60.2545960.0931650.4109660.0769550.3544530.0844330.3189030.1275380.2397410.0910070.1656140.0398990.1559160.0396950.0617160.0718790.0383580.0199790.055968-0.01739TB
1.80.2404330.0859550.4251520.0595350.3603180.0680180.3419630.1196060.2879330.1287730.2162630.057560.1786170.0396760.0661580.0641240.020834-0.001280.033785-0.03775TB
20.2273860.0662970.3961660.0313210.3357930.0508810.3239310.1134150.2963010.178510.277450.1224770.2435690.1034460.081740.0883190.017908-0.009780.036473-0.0299TB
2.20.1838190.0466840.3645660.0153990.2992480.0338320.2828080.0994320.2709010.1526620.2741670.1472370.3016250.1674360.1525070.1609590.0498630.0247510.036133-0.02581TB
2.40.1483020.0278920.3263350.0040010.2710280.0281520.2501210.0833510.231390.1361920.24220.1209880.2929920.181640.2063330.2289290.1177440.0979260.053820.000703TB
2.60.1107970.0239590.28736-0.002720.2326690.010930.2222520.055220.2016280.1082860.2114110.1065460.2583530.1546620.2290130.263280.185340.1691680.0950970.052153p
2.80.0992640.0284220.255029-0.006690.2106450.0039530.2015950.05150.1955960.1006920.1900670.0768280.2284460.1355470.2109850.2525580.2269760.2198910.1373070.089219p
30.0712130.0197510.227323-0.023430.1917250.018420.1821620.0365420.1627750.0817390.15950.0790740.2012060.1018150.1952050.2168220.2116620.2241340.1357750.106554p
3.20.0643610.0143240.212866-0.012880.1799120.0212630.1527680.0333230.1493190.0746380.1473280.0633130.1676210.0965510.1639810.1909070.1826610.1974510.1351960.103357p
3.40.0794350.0361450.1992460.0130010.1807010.039210.1483560.0547320.1337330.0762120.1226170.0540740.1408810.0811780.1189040.1613230.1486220.1569220.1184270.076915p
3.60.0918260.0897740.2192350.0685170.1906570.0752120.1596020.0972420.1303080.0911120.0956760.0506560.1276650.0741060.0938820.133030.1046170.124960.0944310.060119p
3.80.1132360.12180.2106230.1220960.1749530.1336050.150130.1409040.1174020.1244410.0790410.0658750.101650.0671340.0649930.1084340.073690.0906620.0772350.033351left turn
40.1223590.1811130.1971780.1805450.1653760.1686250.1348650.1835340.0977640.1542880.0612580.0896510.0763290.0698160.0479610.0990750.0354380.0600170.0559390.019039left turn
4.20.1577090.2234130.2093860.2317280.1530520.2157310.129140.2158240.0820690.1825150.0514820.0888790.064750.0578920.014090.0824860.006430.0251130.031239-0.01256left turn
4.40.1667210.2610070.207480.3021130.141460.2626280.1227680.2736960.0629810.21190.036160.1061580.04070.053773-0.00830.06526-0.019270.0001330.025646-0.03279left turn
4.60.1830470.2934420.1991770.3363230.1299050.292980.1149370.2956160.0550610.2326360.0181070.1078960.0330480.045569-0.023260.047701-0.04928-0.024570.019616-0.04695TB
4.80.1642670.2834440.1784660.3501320.1327210.300020.1289470.3185040.0856570.2696860.0465590.1284780.0457070.056857-0.030660.038699-0.05635-0.04157-0.00216-0.04739TB
50.1147120.2372960.1483090.3069580.1200840.2818820.1515860.3026980.1463010.3029980.1179050.1769820.0939240.091124-0.012870.063889-0.05574-0.0512-0.00138-0.0647TB
5.20.0873190.197750.109840.2638340.0807740.2441450.1242880.2754370.1659350.2852040.1676070.206040.1868980.1464920.0368370.117452-0.04796-0.037680.003126-0.06285TB
5.40.0676180.152150.08660.2288840.0806940.2000720.1035140.2281220.1362110.2549320.1739510.1911540.2366680.1893160.1225610.1921550.0113950.0108750.026924-0.04172TB
5.60.058730.1393750.0644750.192910.0677920.1864440.0872260.1957310.1119380.2314950.1358390.1700290.218510.1748410.1806930.2517470.1094370.1018750.05494-0.00504TB

<tbody>
</tbody>

Though a lot larger (~50,000 - ~100,000 rows)

I need to go through them and split off sequences of the same TrackName into separate files (worksheet or workbook, it doesn't matter which) with the same header row. So the first 7 rows, then the 8th row, then the next 5 and so on. The trick is that it needs to keep the sequences separate, rather than just grouping all the "p"s together, all the "TB"s together and so on. I've found a couple macros that will split it according to the TrackName column, but nothing that can split it into each sequence of rows with the same TrackName.

Ideally I also need the macro to delete the Time and TrackName columns from the created worksheets/books and save them as Tab-delimited txt files, but that step is at least somewhat possible to do manually, whereas splitting the files into each sequence manually would take an infeasibly long time.

The macro from http://www.mrexcel.com/forum/excel-questions/396069-copy-new-worksheets.html is the closest I've gotten so far, but it gives me this kind of output:

TimeT2RT2LT3RT3LA1RA1LA2RA2LA3RA3LA4RA4LA5RA5LA6RA6LA7RA7LA8RA8LTrackName
10.60.2179750.0559620.2799610.088880.2477240.0959540.2346250.1077720.1628860.0885780.1080380.0146820.1280190.0019720.0203670.045757-0.00496-0.015540.041308-0.03127b
10.80.1984450.0404740.264780.05490.2367330.0751980.2535620.13490.2168360.1558990.1625830.0696430.1401120.0295760.0227520.030432-0.01633-0.03510.037202-0.05016b
110.1627070.0356170.2351470.0316780.2077560.0548390.2269580.1266380.2362820.1803560.219220.1287540.200830.0860110.0497520.063374-0.02519-0.040190.030119-0.0544b
11.20.1325030.0219130.2082260.0193080.1850410.0372950.1982080.0972390.1963140.1620.2294070.1526610.259410.1250670.1136060.1321060.008907-0.007340.022419-0.03618b
11.40.0937440.0067470.1872880.0176550.157860.0322480.1727790.074810.1777930.1305250.1974890.1264680.2594210.1508610.1689320.2033760.0783440.0628850.055963-0.01486b
11.60.071035-0.000820.155297-0.004030.1465320.0202820.1495470.0657120.1541130.1230930.1832440.1114470.2369770.1346860.1939210.2311110.1531540.1469840.0827980.035427b
11.80.056772-0.010220.133204-0.010720.1302420.0117920.1324730.0475950.1410990.1041860.1620060.0981410.2080110.1131220.1805110.2159150.1974820.2027220.1240580.07239b
120.055788-0.004540.130163-0.013250.1212750.0175230.1254970.0441430.1193790.0866760.1376890.0842080.1729070.0937230.1556210.1969430.2006980.2024930.1249820.09462b
12.20.053227-0.013770.123428-0.010870.1288140.0137190.1177260.0336720.1122060.0829780.1181960.0642040.1615610.0808690.1299380.1603930.1824970.1749340.1227180.08431b
12.40.0898120.0001120.156157-0.006630.1474160.022620.1231730.0331840.1028660.0679910.094720.0586830.1326380.066460.103450.1409680.1340520.1453790.1077750.059909b
12.6
0.1201870.0208020.1869230.0072170.1814330.0427650.1351260.046770.1064460.0551010.0992660.0437430.1151830.044640.084650.1143010.1048810.1037250.0949980.051358b
15
0.1198030.0487720.3220710.0053170.2907210.0169640.2153670.0258920.1418960.0145250.080998-0.020840.043994-0.04685-0.05326-0.04465-0.09187-0.09341-0.02574-0.08869b
15.20.098740.0310040.2848420.0064750.2746840.0266950.2282550.0615350.1839270.0566340.099930.0095860.054931-0.03316-0.05446-0.05103-0.09503-0.11651-0.03603-0.08883b
15.40.0814210.0119760.248103-0.015420.237230.010280.2072740.0660020.1961520.0971580.1589470.0821420.1112820.032079-0.027-0.01392-0.09109-0.12113-0.04332-0.10222b
15.60.0637790.0063210.200885-0.016820.183674-0.003450.1680690.0441660.1723650.0883280.1870470.1178080.1873050.1073610.0274780.058289-0.07617-0.08888-0.03508-0.0859b
15.80.0404940.001530.167639-0.036060.152338-0.019860.1334610.0204860.1426420.0465710.174290.1117640.2104910.1517050.112970.160595-0.01898-0.02582-0.01324-0.06371b
160.031055-0.009240.153814-0.047680.144072-0.042730.124742-0.005430.1229050.0206510.1502080.0789730.1994280.1432220.1525610.1969480.0703050.0594680.024602-0.0274b
16.20.022942-0.004420.136653-0.052520.135352-0.038990.116681-0.019080.1222970.0058330.1252370.0442040.1615970.1162480.1503560.2000090.1299590.1216210.0673650.02572b
16.40.0251250.0193230.122356-0.034280.13034-0.026570.107256-0.016910.1138390.0203720.1064880.0462050.142860.0900350.1291080.1801020.12290.1313310.0774180.035033b
16.60.0397530.0493780.1109260.0007150.117577-0.002260.0915290.0145630.0999960.0267910.0875030.0406770.1224760.0626770.099440.1514930.1078050.1066940.0811330.024343b
17.60.0675850.2435850.0568280.2530420.0542460.2122110.052050.1850570.0284290.135350.0083840.0775740.0463450.038782-0.000920.05451-0.02459-0.022950.012996-0.04391b
17.80.0446680.2379160.04750.2786770.0460560.2235990.0502670.2321690.0392240.1594760.0202790.0917280.0399090.034883-0.018070.04297-0.0482-0.049840.0008-0.04526b
180.0329840.2133730.027970.2697230.0440660.2310510.0811910.2471550.0809940.1979450.0540460.1333230.0506340.058568-0.027410.046677-0.05261-0.066160.001192-0.05352b
18.20.0078960.1850230.005410.2507390.023890.2102660.0797670.2255720.1126790.2227220.1095740.17550.1069520.099682-0.000360.073685-0.05957-0.06809-0.00406-0.06262b
18.40.0002370.162273-0.016730.2129910.0123930.1738950.0591310.1932840.1008340.2066220.1326410.1835710.1680220.1429790.055680.13196-0.03258-0.044930.005786-0.0565b
18.6-0.001880.132993-0.021150.1878220.0096860.1514290.0458230.1532590.0773560.1691170.1189840.1553670.183460.1507730.1140550.1959440.0224340.0121830.017141-0.03556b
18.8-0.009670.112522-0.019670.1511150.0046190.1290540.0370450.1343220.0649010.1434060.0914180.132780.1586160.1357550.1391940.2244780.0891730.0872510.054629-0.00538b
19-0.003280.080878-0.020350.1340950.0103590.1137450.0336160.1106550.0601950.1172160.0718670.1052240.1398650.1107110.1264980.2082750.1150850.1227250.0717490.020974b
19.20.0030330.074836-0.005380.1080020.0123050.0965580.0335210.0986770.0519980.0991960.0571450.0718690.1191390.088060.0996150.1740990.1082730.1095940.0633480.021973b

<tbody>
</tbody>

And for the full size files having to go through and manually separate the files wherever there's a break in the time sequence is still not feasible.
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
Figured it out.

If anyone else ever has the same problem, this is the code I cobbled together.

Public Sub SplitToFiles()

' MACRO SplitToFiles
' Last update: 2012-03-04
' Author: mtone
' Version 1.1
' Description:
' Loops through a specified column, and split each distinct values into a separate file by making a copy and deleting rows below and above
'
' Note: Values in the column should be unique or sorted.
'
' The following cells are ignored when delimiting sections:
' - blank cells, or containing spaces only
' - same value repeated
' - cells containing "total"
'
' Files are saved in a "Split" subfolder from the location of the source workbook, and named after the section name.

Dim osh As Worksheet ' Original sheet
Dim iRow As Long ' Cursors
Dim iCol As Long
Dim iFirstRow As Long ' Constant
Dim iTotalRows As Long ' Constant
Dim iStartRow As Long ' Section delimiters
Dim iStopRow As Long
Dim sSectionName As String ' Section name (and filename)
Dim sFileNames As String ' filename
Dim sFinalName As String ' filename
Dim rCell As Range ' current cell
Dim owb As Workbook ' Original workbook
Dim sFilePath As String ' Constant
Dim iCount As Integer ' # of documents created

iCol = Application.InputBox("Enter the column number used for splitting", "Select column", 2, , , , , 1)
iRow = Application.InputBox("Enter the starting row number (to skip header)", "Select row", 5, , , , , 1)
iFirstRow = iRow

Set osh = Application.ActiveSheet
Set owb = Application.ActiveWorkbook
iTotalRows = osh.UsedRange.Rows.Count
sFilePath = Application.ActiveWorkbook.Path

If Dir(sFilePath + "\Split", vbDirectory) = "" Then
MkDir sFilePath + "\Split"
End If

'Turn Off Screen Updating Events
Application.EnableEvents = False
Application.ScreenUpdating = False

Do
' Get cell at cursor
Set rCell = osh.Cells(iRow, iCol)
sCell = Replace(rCell.Text, " ", "")

If sCell = "" Or (rCell.Text = sSectionName And iStartRow <> 0) Or InStr(1, rCell.Text, "total", vbTextCompare) <> 0 Then
' Skip condition met
Else
' Found new section
If iStartRow = 0 Then
' StartRow delimiter not set, meaning beginning a new section
sSectionName = rCell.Text
iStartRow = iRow
Else
' StartRow delimiter set, meaning we reached the end of a section
iStopRow = iRow - 1

' file name as count
sFileNames = iCount

' Pass variables to a separate sub to create and save the new worksheet
CopySheet osh, iFirstRow, iStartRow, iStopRow, iTotalRows, sFilePath, sSectionName, sFileNames, sFinalName, owb.fileFormat
iCount = iCount + 1

' Reset section delimiters
iStartRow = 0
iStopRow = 0

' Ready to continue loop
iRow = iRow - 1
End If
End If

' Continue until last row is reached
If iRow < iTotalRows Then
iRow = iRow + 1
Else
' Finished. Save the last section
iStopRow = iRow
CopySheet osh, iFirstRow, iStartRow, iStopRow, iTotalRows, sFilePath, sSectionName, sFileNames, sFinalName, owb.fileFormat
iCount = iCount + 1

' Exit
Exit Do
End If
Loop

'Turn On Screen Updating Events
Application.ScreenUpdating = True
Application.EnableEvents = True

MsgBox Str(iCount) + " documents saved in " + sFilePath


End Sub

Public Sub DeleteRows(targetSheet As Worksheet, RowFrom As Long, RowTo As Long)

Dim rngRange As Range
Set rngRange = Range(targetSheet.Cells(RowFrom, 1), targetSheet.Cells(RowTo, 1)).EntireRow
rngRange.Select
rngRange.Delete

End Sub


Public Sub CopySheet(osh As Worksheet, iFirstRow As Long, iStartRow As Long, iStopRow As Long, iTotalRows As Long, sFilePath As String, sSectionName As String, sFileNames As String, sFinalName As String, fileFormat As XlFileFormat)
Dim ash As Worksheet ' Copied sheet
Dim awb As Workbook ' New workbook

' Copy book
osh.Copy
Set ash = Application.ActiveSheet

' Delete Rows after section
If iTotalRows > iStopRow Then
DeleteRows ash, iStopRow + 1, iTotalRows
End If

' Delete Rows before section
If iStartRow > iFirstRow Then
DeleteRows ash, iFirstRow, iStartRow - 1
End If

' Select left-topmost cell
ash.Cells(1, 1).Select

' Clean up a few characters to prevent invalid filename
sSectionName = Replace(sSectionName, "/", " ")
sSectionName = Replace(sSectionName, "\", " ")
sSectionName = Replace(sSectionName, ":", " ")
sSectionName = Replace(sSectionName, "=", " ")
sSectionName = Replace(sSectionName, "*", " ")
sSectionName = Replace(sSectionName, ".", " ")
sSectionName = Replace(sSectionName, "?", ".q.")

' Delete Columns
Columns([22]).EntireColumn.Delete
Columns([1]).EntireColumn.Delete

sFinalName = sSectionName & "_" & sFileNames

' Save in same format as original workbook, need to get it to save section name before file count
ash.SaveAs sFilePath + "\Split\" + sSectionName & "_" & sFileNames, xlTextWindows

' Close
Set awb = ash.Parent
awb.Close SaveChanges:=False
End Sub

Based on what I found here: Can I split a spreadsheet into multiple files based on a column in Excel 2007? - Super User
 
Upvote 0

Forum statistics

Threads
1,215,242
Messages
6,123,827
Members
449,127
Latest member
Cyko

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