macro issue (creating new worksheets)

bluepenink

Well-known Member
Joined
Dec 21, 2010
Messages
585
Hello

the following macro i have copies data from my "master" worksheet and creates new worksheets based on column "I" where "I" is the sales ppl positions. basically, it filters and creates new worksheets based on positions to help us sort data out quicker.

the only issue is, i inserted new columns so my range is from A-R. the new columns i created are N, P, R

for some reason however, it copies all the data, except for the data in from the new columns. can someone pls help me with this issue!

Code:
Option Explicit
Sub FilterCities()
'consolidated employees by current role - hhaque
    Dim myCell As Range
    Dim wks As Worksheet
    Dim DataBaseWks As Worksheet
    Dim ListRange As Range
    Dim dummyRng As Range
    Dim myDatabase As Range
    Dim TempWks As Worksheet
    Dim rsp As Integer
    Dim i As Long
    Dim colWidths(20) As Variant
    Dim cwlc As Long    ' column width loop counter
    'include bottom most header row
    Const TopLeftCellOfDataBase As String = "A8"
    'what column has your key values
    Const KeyColumn As String = "I"
    'where's your data
    Set DataBaseWks = Worksheets("Master")
    i = DataBaseWks.Range(TopLeftCellOfDataBase).Row - 1
    
    With DataBaseWks
        For cwlc = 1 To 20
            colWidths(cwlc) = .Columns(cwlc).ColumnWidth
        Next cwlc
    End With
    rsp = MsgBox("Include headings?", vbYesNo, "Filter by current role")
    Application.ScreenUpdating = False
    Set TempWks = Worksheets.Add
    With DataBaseWks
        Set dummyRng = .UsedRange
        Set myDatabase = .Range(TopLeftCellOfDataBase, _
                            .Cells.SpecialCells(xlCellTypeLastCell))
    End With
    'rebuild the List
    With DataBaseWks
        Intersect(myDatabase, .Columns(KeyColumn)).AdvancedFilter _
            Action:=xlFilterCopy, _
            CopyToRange:=TempWks.Range("A1"), _
            Unique:=True
        'Add the heading to the criteria area
        TempWks.Range("D1").Value = _
            .Cells(.Range(TopLeftCellOfDataBase).Row, KeyColumn).Value
    End With
    With TempWks
        Set ListRange = .Range("a2", .Cells(.Rows.Count, "A").End(xlUp))
    End With
    With ListRange
        .Sort Key1:=.Cells(1), Order1:=xlAscending, _
            Header:=xlNo, OrderCustom:=1, _
            MatchCase:=False, Orientation:=xlTopToBottom
    End With
    'check for individual City worksheets
    For Each myCell In ListRange.Cells
        If WksExists(myCell.Value) = False Then
            Set wks = Sheets.Add
            On Error Resume Next
            wks.Name = myCell.Value
            If Err.Number = 5 Then
                MsgBox "Please rename: " & wks.Name
                Err.Clear
            End If
            On Error GoTo 0
            wks.Move After:=Sheets(Sheets.Count)
        Else
            Set wks = Worksheets(myCell.Value)
            wks.Cells.Clear
        End If
        If rsp = 6 Then
          DataBaseWks.Rows("1:" & i).Copy Destination:=wks.Range("A1")
        End If
        
        'change the criteria in the Criteria range
        TempWks.Range("D2").Value = "=" & Chr(34) & "=" & myCell.Value & Chr(34)
        'transfer data to individual City worksheets
        If rsp = 6 Then
          myDatabase.AdvancedFilter _
              Action:=xlFilterCopy, _
              CriteriaRange:=TempWks.Range("D1:D2"), _
              CopyToRange:=wks.Range("A1").Offset(i, 0), _
              Unique:=False
        Else
          myDatabase.AdvancedFilter _
              Action:=xlFilterCopy, _
              CriteriaRange:=TempWks.Range("D1:D2"), _
              CopyToRange:=wks.Range("A1"), _
              Unique:=False
        End If
    With wks
        For cwlc = 1 To 20
            .Columns(cwlc).ColumnWidth = colWidths(cwlc)
        Next cwlc
        
    ActiveWindow.DisplayGridlines = Not ActiveWindow.DisplayGridlines
    
' new code
       With .PageSetup
            .LeftHeader = ""
            .CenterHeader = ""
            .RightHeader = "&""Arial,Regular""&8DRAFT"
            .LeftFooter = "&""Arial,Regular""&8&F"
            .CenterFooter = "&""Arial,Regular""&8Confidential"
            .RightFooter = "&""Arial,Regular""&8Page: &P of &N"
            .LeftMargin = Application.InchesToPoints(0.17)
            .RightMargin = Application.InchesToPoints(0.17)
            .TopMargin = Application.InchesToPoints(0.24)
            .BottomMargin = Application.InchesToPoints(0.26)
            .HeaderMargin = Application.InchesToPoints(0.17)
            .FooterMargin = Application.InchesToPoints(0.16)
            .Orientation = xlPortrait
            .Zoom = False
            .FitToPagesWide = 1
            .FitToPagesTall = False
        End With
' end of new code
    End With
    Next myCell
    Application.DisplayAlerts = False
    TempWks.Delete
    Application.DisplayAlerts = True
    Application.ScreenUpdating = True
    
    MsgBox "Employees filtered by current role complete"
End Sub
Function WksExists(wksName As String) As Boolean
    On Error Resume Next
    WksExists = CBool(Len(Worksheets(wksName).Name) > 0)
End Function

thx you so much!
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
Hey jw01. My guess is that the new columns do not contain any data in the first row after the headers, i.e. row 8 of columns N, P, and R. If row 8 contains data in the new columns, then the entire columns data should be copied.
 
Upvote 0
hello

yes, you are right.

basically, i have a master sheet of about 200 employees, tracking their training.

there are three levels we are capturing (positions), so
i.e. John Doe might not have any data in the first 2 columns, so column R might have data.

any idea how i can intregrate that into the macro so the all data is copied over?...much appreciated!
 
Upvote 0
here is the data in from columns M-R, respectively..example of couple of rows

<TABLE style="WIDTH: 534pt; BORDER-COLLAPSE: collapse" border=0 cellSpacing=0 cellPadding=0 width=714><COLGROUP><COL style="WIDTH: 89pt; mso-width-source: userset; mso-width-alt: 4352" width=119><COL style="WIDTH: 89pt; mso-width-source: userset; mso-width-alt: 4352; mso-outline-level: 1" width=119><COL style="WIDTH: 89pt; mso-width-source: userset; mso-width-alt: 4352" width=119><COL style="WIDTH: 89pt; mso-width-source: userset; mso-width-alt: 4352; mso-outline-level: 1" width=119><COL style="WIDTH: 89pt; mso-width-source: userset; mso-width-alt: 4352" width=119><COL style="WIDTH: 89pt; mso-width-source: userset; mso-width-alt: 4352; mso-outline-level: 1" width=119><TBODY><TR style="HEIGHT: 31.5pt; mso-height-source: userset" height=42><TD style="BORDER-BOTTOM: windowtext 0.5pt solid; BORDER-LEFT: windowtext 0.5pt solid; BACKGROUND-COLOR: silver; WIDTH: 89pt; HEIGHT: 31.5pt; BORDER-TOP: windowtext 0.5pt solid; BORDER-RIGHT: #f0f0f0" class=xl69 height=42 width=119>ISE Training</TD><TD style="BORDER-BOTTOM: windowtext 0.5pt solid; BORDER-LEFT: #f0f0f0; BACKGROUND-COLOR: silver; WIDTH: 89pt; BORDER-TOP: windowtext 0.5pt solid; BORDER-RIGHT: #f0f0f0" class=xl70 width=119>Training City</TD><TD style="BORDER-BOTTOM: windowtext 0.5pt solid; BORDER-LEFT: #f0f0f0; BACKGROUND-COLOR: silver; WIDTH: 89pt; BORDER-TOP: windowtext 0.5pt solid; BORDER-RIGHT: #f0f0f0" class=xl70 width=119>SE Training</TD><TD style="BORDER-BOTTOM: windowtext 0.5pt solid; BORDER-LEFT: #f0f0f0; BACKGROUND-COLOR: silver; WIDTH: 89pt; BORDER-TOP: windowtext 0.5pt solid; BORDER-RIGHT: #f0f0f0" class=xl70 width=119>Training City</TD><TD style="BORDER-BOTTOM: windowtext 0.5pt solid; BORDER-LEFT: #f0f0f0; BACKGROUND-COLOR: silver; WIDTH: 89pt; BORDER-TOP: windowtext 0.5pt solid; BORDER-RIGHT: #f0f0f0" class=xl75 width=119>DSM Training</TD><TD style="BORDER-BOTTOM: windowtext 0.5pt solid; BORDER-LEFT: #f0f0f0; BACKGROUND-COLOR: silver; WIDTH: 89pt; BORDER-TOP: windowtext 0.5pt solid; BORDER-RIGHT: windowtext 0.5pt solid" class=xl71 width=119>Training City</TD></TR><TR style="HEIGHT: 12.75pt" height=17><TD style="BORDER-BOTTOM: windowtext 0.5pt solid; BORDER-LEFT: windowtext 0.5pt solid; BACKGROUND-COLOR: transparent; HEIGHT: 12.75pt; BORDER-TOP: windowtext; BORDER-RIGHT: windowtext 0.5pt solid" class=xl72 height=17></TD><TD style="BORDER-BOTTOM: windowtext 0.5pt solid; BORDER-LEFT: windowtext; BACKGROUND-COLOR: silver; BORDER-TOP: windowtext; BORDER-RIGHT: windowtext 0.5pt solid" class=xl74></TD><TD style="BORDER-BOTTOM: windowtext 0.5pt solid; BORDER-LEFT: windowtext; BACKGROUND-COLOR: transparent; BORDER-TOP: windowtext; BORDER-RIGHT: windowtext 0.5pt solid" class=xl72>15-Nov-10</TD><TD style="BORDER-BOTTOM: windowtext 0.5pt solid; BORDER-LEFT: windowtext; BACKGROUND-COLOR: silver; BORDER-TOP: windowtext; BORDER-RIGHT: windowtext 0.5pt solid" class=xl74>Philadelphia</TD><TD style="BORDER-BOTTOM: windowtext 0.5pt solid; BORDER-LEFT: windowtext; BACKGROUND-COLOR: transparent; BORDER-TOP: windowtext; BORDER-RIGHT: windowtext 0.5pt solid" class=xl72></TD><TD style="BORDER-BOTTOM: windowtext 0.5pt solid; BORDER-LEFT: windowtext; BACKGROUND-COLOR: silver; BORDER-TOP: windowtext; BORDER-RIGHT: windowtext 0.5pt solid" class=xl74></TD></TR><TR style="HEIGHT: 12.75pt" height=17><TD style="BORDER-BOTTOM: windowtext 0.5pt solid; BORDER-LEFT: windowtext 0.5pt solid; BACKGROUND-COLOR: transparent; HEIGHT: 12.75pt; BORDER-TOP: windowtext; BORDER-RIGHT: windowtext 0.5pt solid" class=xl72 height=17></TD><TD style="BORDER-BOTTOM: windowtext 0.5pt solid; BORDER-LEFT: windowtext; BACKGROUND-COLOR: silver; BORDER-TOP: windowtext; BORDER-RIGHT: windowtext 0.5pt solid" class=xl74></TD><TD style="BORDER-BOTTOM: windowtext 0.5pt solid; BORDER-LEFT: windowtext; BACKGROUND-COLOR: transparent; BORDER-TOP: windowtext; BORDER-RIGHT: windowtext 0.5pt solid" class=xl72></TD><TD style="BORDER-BOTTOM: windowtext 0.5pt solid; BORDER-LEFT: windowtext; BACKGROUND-COLOR: silver; BORDER-TOP: windowtext; BORDER-RIGHT: windowtext 0.5pt solid" class=xl74></TD><TD style="BORDER-BOTTOM: windowtext 0.5pt solid; BORDER-LEFT: windowtext; BACKGROUND-COLOR: transparent; BORDER-TOP: windowtext; BORDER-RIGHT: windowtext 0.5pt solid" class=xl72>1-Jun-10</TD><TD style="BORDER-BOTTOM: windowtext 0.5pt solid; BORDER-LEFT: windowtext; BACKGROUND-COLOR: silver; BORDER-TOP: windowtext; BORDER-RIGHT: windowtext 0.5pt solid" class=xl74>Support Center</TD></TR></TBODY></TABLE>
 
Upvote 0
The problem arises because your macro is using AdvancedFilter to filter the rows and copy the data. AdvancedFilter expects the first row given to it (i.e. row 8 in the case of your macro) to be headers. If the headers row contains empty cells, then those columns aren't copied. I hope the following changes (marked in red) to your macro may overcome this. The changes assume that each cell of row 7 of your sheet contains a unique header.

Rich (BB code):
Option Explicit
Sub FilterCities()
'consolidated employees by current role - hhaque
    Dim myCell As Range
    Dim wks As Worksheet
    Dim DataBaseWks As Worksheet
    Dim ListRange As Range
    Dim dummyRng As Range
    Dim myDatabase As Range
    Dim TempWks As Worksheet
    Dim rsp As Integer
    Dim i As Long
    Dim colWidths(20) As Variant
    Dim cwlc As Long    ' column width loop counter
    'include bottom most header row
    Const TopLeftCellOfDataBase As String = "A7"
    'what column has your key values
    Const KeyColumn As String = "I"
    'where's your data
    Set DataBaseWks = Worksheets("Master")
    i = DataBaseWks.Range(TopLeftCellOfDataBase).Row - 1
 
    With DataBaseWks
        For cwlc = 1 To 20
            colWidths(cwlc) = .Columns(cwlc).ColumnWidth
        Next cwlc
    End With
    rsp = MsgBox("Include headings?", vbYesNo, "Filter by current role")
    Application.ScreenUpdating = False
    Set TempWks = Worksheets.Add
    With DataBaseWks
        Set dummyRng = .UsedRange
        Set myDatabase = .Range(TopLeftCellOfDataBase, _
                            .Cells.SpecialCells(xlCellTypeLastCell))
    End With
    'rebuild the List
    With DataBaseWks
        Intersect(myDatabase, .Columns(KeyColumn)).AdvancedFilter _
            Action:=xlFilterCopy, _
            CopyToRange:=TempWks.Range("A1"), _
            Unique:=True
        'Add the heading to the criteria area
        TempWks.Range("D1").Value = _
            .Cells(.Range(TopLeftCellOfDataBase).Row, KeyColumn).Value
    End With
    With TempWks
        Set ListRange = .Range("a2", .Cells(.Rows.Count, "A").End(xlUp))
    End With
    With ListRange
        .Sort Key1:=.Cells(1), Order1:=xlAscending, _
            Header:=xlNo, OrderCustom:=1, _
            MatchCase:=False, Orientation:=xlTopToBottom
    End With
    'check for individual City worksheets
    For Each myCell In ListRange.Cells
        If WksExists(myCell.Value) = False Then
            Set wks = Sheets.Add
            On Error Resume Next
            wks.Name = myCell.Value
            If Err.Number = 5 Then
                MsgBox "Please rename: " & wks.Name
                Err.Clear
            End If
            On Error GoTo 0
            wks.Move After:=Sheets(Sheets.Count)
        Else
            Set wks = Worksheets(myCell.Value)
            wks.Cells.Clear
        End If
        If rsp = 6 Then
          DataBaseWks.Rows("1:" & i).Copy Destination:=wks.Range("A1")
        End If
 
        'change the criteria in the Criteria range
        TempWks.Range("D2").Value = "=" & Chr(34) & "=" & myCell.Value & Chr(34)
        'transfer data to individual City worksheets
        If rsp = 6 Then
          myDatabase.AdvancedFilter _
              Action:=xlFilterCopy, _
              CriteriaRange:=TempWks.Range("D1:D2"), _
              CopyToRange:=wks.Range("A1").Offset(i, 0), _
              Unique:=False
        Else
          myDatabase.AdvancedFilter _
              Action:=xlFilterCopy, _
              CriteriaRange:=TempWks.Range("D1:D2"), _
              CopyToRange:=wks.Range("A1"), _
              Unique:=False
          wks.Rows(1).Delete
        End If
    With wks
        For cwlc = 1 To 20
            .Columns(cwlc).ColumnWidth = colWidths(cwlc)
        Next cwlc
 
    ActiveWindow.DisplayGridlines = Not ActiveWindow.DisplayGridlines
 
' new code
       With .PageSetup
            .LeftHeader = ""
            .CenterHeader = ""
            .RightHeader = "&""Arial,Regular""&8DRAFT"
            .LeftFooter = "&""Arial,Regular""&8&F"
            .CenterFooter = "&""Arial,Regular""&8Confidential"
            .RightFooter = "&""Arial,Regular""&8Page: &P of &N"
            .LeftMargin = Application.InchesToPoints(0.17)
            .RightMargin = Application.InchesToPoints(0.17)
            .TopMargin = Application.InchesToPoints(0.24)
            .BottomMargin = Application.InchesToPoints(0.26)
            .HeaderMargin = Application.InchesToPoints(0.17)
            .FooterMargin = Application.InchesToPoints(0.16)
            .Orientation = xlPortrait
            .Zoom = False
            .FitToPagesWide = 1
            .FitToPagesTall = False
        End With
' end of new code
    End With
    Next myCell
    Application.DisplayAlerts = False
    TempWks.Delete
    Application.DisplayAlerts = True
    Application.ScreenUpdating = True
 
    MsgBox "Employees filtered by current role complete"
End Sub
Function WksExists(wksName As String) As Boolean
    On Error Resume Next
    WksExists = CBool(Len(Worksheets(wksName).Name) > 0)
End Function

Be sure to test this on a copy of your data first as results may be unexpected.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,618
Messages
6,179,916
Members
452,949
Latest member
beartooth91

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