LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Change code to make worksheets instead of workbooks

I finally came across a post for someone else that had code to do what I
want, but with a slight variation:

I want to make multiple worksheets, and this code makes multiple
workbooks from one worksheet.

The problem is...

From one sheet:

10 ABC ...
10 ABC ...
20 EFG ...
20 EFG ...
20 EFG ...
20 EFG ...
30 QRS ...
30 QRS ...
30 QRS ...

I would want multiple sheets:

Sheet A =
10 ABC ...
10 ABC ...

Sheet B =
20 EFG ...
20 EFG ...
20 EFG ...
20 EFG ...

Sheet C =
30 QRS ...
30 QRS ...
30 QRS ...


Here is the code:


Sub CreateWorkbooks()

Dim wkbkCurrent As Workbook
Dim wkbkNew As Workbook
Dim wsData As Worksheet
Dim wsFilter As Worksheet
Dim ws As Worksheet
Dim cell As Range
Dim colManagers As New Collection
Dim vntManager As Variant
Dim lngNumRows As Long
Dim strName As String

Set wkbkCurrent = ActiveWorkbook
Set wsData = wkbkCurrent.Worksheets("MyData")
Set wsFilter = wkbkCurrent.Worksheets("MyFilter")

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 managers from values in column A
On Error Resume Next
For Each cell In wsData.Range("A2:A" & lngNumRows)
colManagers.Add cell.Value, CStr(cell.Value)
Next cell
On Error GoTo 0

'Filter on each manager, create workbook,
'save workbook and close workbook
For Each vntManager In colManagers

Set wkbkNew = Application.Workbooks.Add

'Put the manager's name into the filter criteria range
wkbkCurrent.Worksheets("MyFilter").Range("A2").Val ue =
vntManager

'Create a new worksheet in the new workbook
wkbkNew.Sheets.Add befo=wkbkNew.Worksheets("Sheet1")

Set ws = ActiveSheet

'Change the sheet name
ws.Name = vntManager

'Filter the data based on your criteria range
'and copy the filtered data to the new workbook
wsData.Range("A1").CurrentRegion.AdvancedFilter _
Action:=xlFilterCopy, _
CriteriaRange:=wsFilter.Range("A1:A2"), _
CopyToRange:=ws.Range("A1")

'Create a file name, save and close
strName = "C:\MyFiles\" & "MyData " & vntManager
wkbkNew.SaveAs (strName)
wkbkNew.Close (False)

Next vntManager

LeaveSub:

Set colManagers = Nothing
Set cell = Nothing
Set wsData = Nothing
Set ws = Nothing
Set wsFilter = Nothing
Set wkbkNew = Nothing
Set wkbkCurrent = Nothing

Application.ScreenUpdating = True
Application.StatusBar = False

End Sub



Another problem I'm having is that there are 3 blank colums which need
to stay in the worksheet - but this code stops copying data when it
hits a blank column.

Thanks in advance for the help
-Mike


---
Message posted from http://www.ExcelForum.com/

 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
vba code to open workbooks Rebecca1 Excel Worksheet Functions 3 March 5th 08 05:15 PM
Copy/ move selected data from workbooks to seperate worksheets or workbooks Positive Excel Worksheet Functions 1 August 30th 07 04:54 PM
how do i change or make this macro??? pictures and code included dispelthemyth Charts and Charting in Excel 0 March 17th 06 01:02 AM
Need code to protect worksheets - amount of worksheets varies Sandy[_3_] Excel Programming 1 September 9th 03 02:17 AM
VBA code for looping through open workbooks and worksheets Jamie Martin[_2_] Excel Programming 1 July 24th 03 06:44 PM


All times are GMT +1. The time now is 06:09 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"