Moving Worksheets to a new Workbook

termeric

Active Member
Joined
Jun 21, 2005
Messages
280
Hello, I am trying to move a group of worksheets to a new workbook. I have the code working when I hard code the worksheet names in the code, but I want to make those variables that will be filled by values on one of the worksheets, so that this can be run for different groups.

This is the original code that works;

Code:
Sub Seperate_Sheets()

Dim Path1 As String

Path1 = ActiveWorkbook.Path & "\" & "Tracker 1"

Sheets(Array("sheet1", "sheet2", "sheet3")).Move
    ActiveWorkbook.SaveAs Filename:=Path1, FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
    ActiveWindow.Close

End Sub


This is the code that I have incorporated it into, where I build the array based on the team selected;

Code:
Sub newMovement()

Dim ExDir As String
Dim Managers() As String
Dim MgrCount As Integer
Dim r As Integer, lr As Integer, ldir As Integer, o As Integer


o = 1
lr = Sheets("Swap").UsedRange.Rows.Count
ldir = Sheets("Directory").UsedRange.Rows.Count
ExDir = Sheets("Control").Range("A2").Value
MgrCount = 0

'determing how many managers are on each team
For r = 2 To lr
    If ExDir = Sheets("Swap").Range("A" & r).Value Then
        MgrCount = Sheets("Swap").Range("b" & r).Value
    End If
Next r

'size array
ReDim Managers(MgrCount) As String

'build list of managers on selected team
For r = 1 To ldir
    If Sheets("Directory").Range("h" & r).Value = ExDir Then
        Managers(o) = Sheets("Directory").Range("a" & r).Value
        o = o + 1
    End If
Next r

'move report tabs to new workbook
Sheets(array(Managers()).Move
    ActiveWorkbook.SaveAs Filename:=Path1, FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
    ActiveWindow.Close

End Sub

I'm not sure what the proper syntax is to use the Managers() array, do I need to write another loop, or is there another way to just say use everything in here?
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
How about
Code:
Sub termeric()
   Dim Cl As Range
   Dim ExDir As Variant
   Dim Ws As Worksheet
   
   Set Ws = Sheets("Directory")
   ExDir = Sheets("Control").Range("A2").Value

   With CreateObject("scripting.dictionary")
      For Each Cl In Ws.Range("A2", Ws.Range("A" & Rows.Count).End(xlUp))
         If Cl.Offset(, 7).Value = ExDir Then .item(Cl.Value) = Empty
      Next Cl
      Sheets(.keys).Move
      ActiveWorkbook.SaveAs FileName:=Path1, FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
      ActiveWindow.Close
   End With
End Sub
You will need to assign a value to the Path1 variable
 
Upvote 0
This works great, thank you. one question though, how would I trim the name that I am entering into the array? I've discovered that some of my teams are too long to be a worksheet name, so I want to take the left 25 characters.

I tried this, and it didn't work
Code:
If Cl.Offset(, 7).Value = ExDir Then Left(.Item(Cl.Value), 25) = Empty


I haven't used the scripting.dictionary before so I'm not sure where to do this

thank you
 
Upvote 0
It would need to be
Code:
.item(left(Cl.Value,25))
But it will need to exactly match the sheet name
 
Upvote 0
Hi Fluff, one more question for you, how would I add one more item to the scripting that is the same every time? So after I loop through the variable list, I can also pick up the "overview" page?
 
Upvote 0
Simply add this line
Code:
Next Cl
[COLOR=#0000ff].Add "overview", Nothing[/COLOR]
Sheets(.keys).Move
 
Upvote 0
thanks, I got it to work with this, but I figured there had to be a better way.


If Cl.Offset(, 7).Value = ExDir Then .Item("overview") = Empty
 
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,157
Members
453,021
Latest member
Justyna P

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