Split workbook into separate worksheets and copy only certain columns to new worksheets

Imtiaz

New Member
Joined
Mar 18, 2014
Messages
12
Hi....I found this code on the forum to split workbook into separate worksheets which works perfectly but I only want to copy across certain columns to the new worksheets. I want columns I, M and W to copy across the new tabs. Can someone please assist. This
Code:
Sub columntosheets()


Const sname As String = "Master" 'change to whatever starting sheet
Const s As String = "M" 'change to whatever criterion column
Dim d As Object, a, cc&
Dim p&, i&, rws&, cls&
Set d = CreateObject("scripting.dictionary")
With Sheets(sname)
    rws = .Cells.Find("*", , , , xlByRows, xlPrevious).Row
    cls = .Cells.Find("*", , , , xlByColumns, xlPrevious).Column
    cc = .Columns(s).Column
End With
For Each sh In Worksheets
    d(sh.Name) = 1
Next sh


Application.ScreenUpdating = False
With Sheets.Add(after:=Sheets(sname))
Sheets(sname).Cells(1).Resize(rws, cls).Copy .Cells(1)
.Cells(1).Resize(rws, cls).Sort .Cells(cc), 2, Header:=xlYes
a = .Cells(cc).Resize(rws + 1, 1)
p = 2
For i = 2 To rws + 1
    If a(i, 1) <> a(p, 1) Then
        If d(a(p, 1)) <> 1 Then
            Sheets.Add.Name = a(p, 1)
            .Cells(1).Resize(, cls).Copy Cells(1)
            .Cells(p, 1).Resize(i - p, cls).Copy Cells(2, 1)
        End If
        p = i
    End If
Next i
Application.DisplayAlerts = False
    .Delete
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End With
Sheets(sname).Activate


End Sub
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
How about
Code:
Sub Imtiaz()
   Dim cl As Range
   Dim ws As Worksheet
   Dim Ky As Variant
   
   Application.ScreenUpdating = False
   Set ws = Sheets("Master")
   With CreateObject("scripting.dictionary")
      For Each cl In ws.Range("M2", ws.Range("M" & Rows.Count).End(xlUp))
         If Not .Exists(cl.Value) Then .Add cl.Value, Nothing
      Next cl
      For Each Ky In .Keys
         ws.Range("A1").AutoFilter 13, Ky
         If Not Evaluate("=ISREF('" & Ky & "'!A1)") Then
            Sheets.Add(, ws).Name = Ky
            Intersect(ws.AutoFilter.Range.EntireRow, ws.Range("I:I,M:M,W:W")).Copy Sheets(Ky).Range("A1")
            Sheets(Ky).Columns.AutoFit
         End If
      Next Ky
   End With
   ws.AutoFilterMode = False
   ws.Activate
End Sub
 
Upvote 0
Thanks so much for that code. It does split the worksheets and copy over the selected columns but I get a debug error - mismatch on
Code:
If Not Evaluate("=ISREF('" & Ky & "'!A1)") Then

Thanks
 
Upvote 0
Do you have any blank cells in col M?
Also what sort of values do you have in col M
 
Upvote 0
There are no blank cells and they are all text values the biggest is 11 characters. Some cells are all capitals some are lower case.
 
Upvote 0
When you get the error hover the mouse of Ky, and it should give you the value that Ky holds, what is it?
 
Upvote 0
Apologies there is a blank cell in M on the last line where the total is

When I removed the last row with the total it works fine without the error. Is there a way to exclude the last total row?

Also once the code has run all the rows on the master sheet become hidden. Is it possible to leave the master sheet as it is to show all the rows.

Thanks.
 
Upvote 0
This will ignore the last row
Code:
Sub Imtiaz()
   Dim cl As Range
   Dim ws As Worksheet
   Dim Ky As Variant
   
   Application.ScreenUpdating = False
   Set ws = Sheets("Master")
   With CreateObject("scripting.dictionary")
      For Each cl In ws.Range("M2", ws.Range("M" & Rows.Count).End(xlUp)[COLOR=#ff0000].Offset(-1)[/COLOR])
         If Not .Exists(cl.Value) Then .Add cl.Value, Nothing
      Next cl
      For Each Ky In .Keys
         ws.Range("A1").AutoFilter 13, Ky
         If Not Evaluate("=ISREF('" & Ky & "'!A1)") Then
            Sheets.Add(, ws).Name = Ky
            Intersect(ws.AutoFilter.Range.EntireRow, ws.Range("I:I,M:M,W:W")).Copy Sheets(Ky).Range("A1")
            Sheets(Ky).Columns.AutoFit
         End If
      Next Ky
   End With
   [COLOR=#0000ff]ws.AutoFilterMode = False[/COLOR]
   ws.Activate
End Sub
The line in blue should also remove the filter leaving everything visible
 
Upvote 0

Forum statistics

Threads
1,215,394
Messages
6,124,683
Members
449,180
Latest member
kfhw720

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