' Excel Spreadsheet Data
' Column 1: Area
' Column 2: City
' Column 3: Dollars
Sub Split_Workbooks()
Dim header1 As String, header2 As String, header3 As String
Dim area As String, prev_area As String, city As String, dollars As Double
Dim i As Long, j As Long, last_row As Long
Dim data_workbook As Workbook, new_workbook As Workbook, path_name As String
Set data_workbook = ActiveWorkbook
With data_workbook.Sheets(1)
header1 = Trim(.Cells(1, 1))
header2 = Trim(.Cells(1, 2))
header3 = Trim(.Cells(1, 3))
End With
path_name = "C:\Temp\"
prev_area = ""
last_row = ActiveSheet.Cells.Find(What:="*", SearchDirection:=xlPrevious, SearchOrder:=xlByRows).Row
Application.DisplayAlerts = False
If last_row < 2 Then
MsgBox "No data"
Exit Sub
End If
For i = 2 To last_row
With data_workbook.Sheets(1)
area = Trim(.Cells(i, 1))
city = Trim(.Cells(i, 2))
dollars = Trim(.Cells(i, 3))
End With
If area = "" Then
Exit For
End If
If area <> prev_area Then
If prev_area <> "" Then
new_workbook.SaveAs (path_name & prev_area & ".xls")
new_workbook.Close
End If
Application.Workbooks.Add
Set new_workbook = ActiveWorkbook
j = 2
Call Fill_Workbook(new_workbook, j, area, city, dollars, header1, header2, header3)
Else
j = j + 1
Call Fill_Workbook(new_workbook, j, area, city, dollars)
End If
prev_area = area
Next i
new_workbook.SaveAs (path_name & prev_area & ".xls")
new_workbook.Close
End Sub
Private Sub Fill_Workbook(new_workbook As Workbook, j As Long, _
area As String, city As String, dollars As Double, _
Optional header1 As String = "", Optional header2 As String = "", Optional header3 As String = "")
With new_workbook.Sheets(1)
If header1 <> "" Then
.Cells(1, 1) = header1
.Cells(1, 2) = header2
.Cells(1, 3) = header3
End If
.Cells(j, 3).NumberFormat = "#,###.00_);[Red](#,###.00)"
.Cells(j, 1) = area
.Cells(j, 2) = city
.Cells(j, 3) = dollars
End With
End Sub