Question posted 2012 · +4 upvotes
I have an excel sheet which has some huge data. Data is organized as follows, A set of 7 columns and n rows; as in a table, and 1000s of such tables are placed horizontally with an empty column to separate. A screenshot is below..
…
I just want to have data of every ‘table’ saved into a different file. Manually it would take ever! So, Is there a macro or something I would automate this task with. I am not well versed with writing macros or any VBA stuff.
Thanks,
Accepted answer +6 upvotes
Tony has a valid point when he says
If the table starting at C1 finishes on row 21, does the next table start at C23? If the table starting at K1 finishes on row 15, does the next table start at K17 or K23?
So here is a code which will work in any condition i.e data is set horizontally or vertically.
DATA SNAPSHOT

CODE
'~~> Change this to the relevant Output folder
Const FilePath As String = "C:Temp"
Dim FileNumb As Long
Sub Sample()
Dim Rng As Range
Dim AddrToCopy() As String
Dim i As Long
On Error GoTo Whoa
Application.ScreenUpdating = False
Set Rng = ActiveSheet.Cells.SpecialCells(xlCellTypeConstants, xlTextValues)
If Not Rng Is Nothing Then
AddrToCopy = Split(Rng.Address, ",")
FileNumb = 1
For i = LBound(AddrToCopy) To UBound(AddrToCopy)
ExportToSheet (AddrToCopy(i))
Next i
End If
MsgBox "Export Done Successfully"
LetsContinue:
Application.ScreenUpdating = True
Exit Sub
Whoa:
MsgBox Err.Description
Resume LetsContinue
End Sub
Sub ExportToSheet(rngAddr As String)
Range(rngAddr).Copy
Workbooks.Add
ActiveSheet.Paste
ActiveWorkbook.SaveAs Filename:= _
FilePath & "Output" & FileNumb & ".csv" _
, FileFormat:=xlCSV, CreateBackup:=False
Application.DisplayAlerts = False
ActiveWorkbook.Close
Application.DisplayAlerts = True
FileNumb = FileNumb + 1
End Sub
NOTE: The above code will work for cells with only Text Values. For cells with only Numeric Values you have to use
Set Rng = ActiveSheet.Cells.SpecialCells(xlCellTypeConstants, xlNumbers)
And for AlphaNumeric Values (As in your question above), use this
Set Rng = ActiveSheet.Cells.SpecialCells(xlCellTypeConstants)
HTH
Sid
3 code variants in this answer
- Variant 1 — 52 lines, starts with
'~~> Change this to the relevant Output folder - Variant 2 — 1 lines, starts with
Set Rng = ActiveSheet.Cells.SpecialCells(xlCellTypeConstant… - Variant 3 — 1 lines, starts with
Set Rng = ActiveSheet.Cells.SpecialCells(xlCellTypeConstant…
Excel VBA objects referenced (5)
ActiveSheet.Cells— Refer to Cells by Using a Range ObjectActiveSheet.Cells— Looping Through a Range of CellsApplication— Using events with the Application objectApplication— Working with Other ApplicationsRange— Delete Duplicate Entries in a Range
Top excel-vba Q&A (6)
- How to clear the entire array? +58 (2010)
- How to change Format of a Cell to Text using VBA +55 (2011)
- Download attachment from Outlook and Open in Excel +43 (2012)
- Can a VBA function in Excel return a range? +36 (2009)
- 2 Dimensional array from range +34 (2013)
- Hiding an Excel worksheet with VBA +33 (2009)
excel-vba solutions on this site
.