S
sbitaxi
Hi all:
I'm working on a macro to extract data from a workbook and parse it
out to separate workbooks based on values in column A. I acquired this
from an older post that put the data in separate worksheets. I've been
tweaking it to suit my requirements but it chokes whenever it gets to
the point of copying the data to a new workbook. I suspect it has
something to do with switching between the source and the new
workbook.
This macro first analyzes the data in column A and creates a
collection based on the different values that occur. It then applies a
filter based on those values and copies the data to a new workbook
named using the value in the filter and the current date.
Any suggestions are greatly appreciated.
Steven
Sub CreateWorksheets()
Dim wkbkCurrent As Workbook
Dim wsData As Worksheet
Dim wsFilter As Worksheet
Dim ws As Worksheet
Dim cell As Range
Dim colBranch As New Collection
Dim vntBranch As Variant
Dim lngNumRows As Long
Dim wb As Workbook
Set wkbkCurrent = ActiveWorkbook
Set wsData = wkbkCurrent.Worksheets("CustomKFCDonation")
Set wsFilter = wkbkCurrent.Worksheets("CustomKFCDonation")
Application.StatusBar = "Creating workbooks. Please wait..."
Application.ScreenUpdating = False
'Count the number of rows
lngNumRows = wsData.Range("A" & Rows.Count).End(xlUp).Row
'Create a collection of Branch from values in column A
On Error Resume Next
For Each cell In wsData.Range("A2:A" & lngNumRows)
colBranch.Add cell.Value, CStr(cell.Value)
Next cell
On Error GoTo 0
'Filter on each Branch, create workbook,
'save workbook and close workbook
For Each vntBranch In colBranch
'Put the Branch's name into the filter criteria range
wkbkCurrent.Worksheets("CustomKFCDonation").Range("A2").Value
= vntBranch
' Set ws = wkbkCurrent.Worksheets.Add
Set wb = Workbooks.Add
'Change the sheet name
' wb.Name = vntBranch & Format(Now(), "yyyy_mmdd")
ActiveWorkbook.SaveAs vntBranch & Format(Now(), "yyyy_mmdd")
wkbkCurrent.Activate
'Filter the data based on your criteria range
'and copy the filtered data to the new workbook
wkbkCurrent.Range("A1").CurrentRegion.AdvancedFilter _
Action:=xlFilterCopy, _
CriteriaRange:=wsFilter.Range("A1:A2"), _
CopyToRange:=wb.Sheets("Sheet1").Range("A1")
Next vntBranch
LeaveSub:
Set colBranch = Nothing
Set cell = Nothing
Set wsData = Nothing
Set ws = Nothing
Set wsFilter = Nothing
Set wkbkCurrent = Nothing
Application.ScreenUpdating = True
Application.StatusBar = False
End Sub
I'm working on a macro to extract data from a workbook and parse it
out to separate workbooks based on values in column A. I acquired this
from an older post that put the data in separate worksheets. I've been
tweaking it to suit my requirements but it chokes whenever it gets to
the point of copying the data to a new workbook. I suspect it has
something to do with switching between the source and the new
workbook.
This macro first analyzes the data in column A and creates a
collection based on the different values that occur. It then applies a
filter based on those values and copies the data to a new workbook
named using the value in the filter and the current date.
Any suggestions are greatly appreciated.
Steven
Sub CreateWorksheets()
Dim wkbkCurrent As Workbook
Dim wsData As Worksheet
Dim wsFilter As Worksheet
Dim ws As Worksheet
Dim cell As Range
Dim colBranch As New Collection
Dim vntBranch As Variant
Dim lngNumRows As Long
Dim wb As Workbook
Set wkbkCurrent = ActiveWorkbook
Set wsData = wkbkCurrent.Worksheets("CustomKFCDonation")
Set wsFilter = wkbkCurrent.Worksheets("CustomKFCDonation")
Application.StatusBar = "Creating workbooks. Please wait..."
Application.ScreenUpdating = False
'Count the number of rows
lngNumRows = wsData.Range("A" & Rows.Count).End(xlUp).Row
'Create a collection of Branch from values in column A
On Error Resume Next
For Each cell In wsData.Range("A2:A" & lngNumRows)
colBranch.Add cell.Value, CStr(cell.Value)
Next cell
On Error GoTo 0
'Filter on each Branch, create workbook,
'save workbook and close workbook
For Each vntBranch In colBranch
'Put the Branch's name into the filter criteria range
wkbkCurrent.Worksheets("CustomKFCDonation").Range("A2").Value
= vntBranch
' Set ws = wkbkCurrent.Worksheets.Add
Set wb = Workbooks.Add
'Change the sheet name
' wb.Name = vntBranch & Format(Now(), "yyyy_mmdd")
ActiveWorkbook.SaveAs vntBranch & Format(Now(), "yyyy_mmdd")
wkbkCurrent.Activate
'Filter the data based on your criteria range
'and copy the filtered data to the new workbook
wkbkCurrent.Range("A1").CurrentRegion.AdvancedFilter _
Action:=xlFilterCopy, _
CriteriaRange:=wsFilter.Range("A1:A2"), _
CopyToRange:=wb.Sheets("Sheet1").Range("A1")
Next vntBranch
LeaveSub:
Set colBranch = Nothing
Set cell = Nothing
Set wsData = Nothing
Set ws = Nothing
Set wsFilter = Nothing
Set wkbkCurrent = Nothing
Application.ScreenUpdating = True
Application.StatusBar = False
End Sub