Identifying Similar Column Headers using a VBA Macro

aryaden

Board Regular
Joined
Jun 9, 2021
Messages
101
Office Version
  1. 2019
Platform
  1. Windows
I Currently have a sheet with multiple columns: Monday1, Monday2, Monday3, Tuesday, Wednesday1, Wednesday2, Thursday, Friday...

I want to create a macro that can identify all the Columns with "Monday" and create a worksheet in the same workbook with those columns listed. Ideally I'd be able to do the same for Wednesday.

I have to deal with many workbooks with column titles that are similar that need to be selected and made into a new worksheet. I am looking to make a macro that can select and group similar column titles regardless of the actual text. For example, if column titles have the same first 5 letters, is there anyway to identify and select those?
 
Is there anyway to modify this code so that the days there aren't duplicates of, such as Tuesday, Wednesday, Thursday, and Saturday (from the example), would not have the column with the Xs in the output Sheet?
Try
VBA Code:
Sub Make_Sheets_v2()
  Dim d As Object
  Dim Ky As Variant
  Dim wsOrig As Worksheet
  Dim cell As Range
  Dim col As Long, LastCol As Long
  
  Set wsOrig = Sheets(1)
  Set d = CreateObject("Scripting.Dictionary")
  With wsOrig
    LastCol = .Cells(1, Columns.Count).End(xlToLeft).Column
    For Each cell In .Range("B1").Resize(, LastCol - 1)
      d(cell.Value) = Empty
    Next cell
  End With
  Application.ScreenUpdating = False
  For Each Ky In d.Keys
    wsOrig.Copy After:=Sheets(Sheets.Count)
    With Sheets(Sheets.Count)
      For col = LastCol To 2 Step -1
        If Not .Cells(1, col).Text = Ky Then .Columns(col).Delete
      Next col
      With .UsedRange
        If .Columns.Count = 2 Then
          .Columns(2).Delete
        Else
          For col = 2 To .Columns.Count
            .AutoFilter Field:=col, Criteria1:=""
          Next col
          .Offset(1).EntireRow.Delete
          .Parent.AutoFilterMode = False
        End If
      End With
      .Name = Ky
    End With
  Next Ky
  wsOrig.Activate
  Application.ScreenUpdating = True
End Sub
 
Upvote 0

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
Try
VBA Code:
Sub Make_Sheets_v2()
  Dim d As Object
  Dim Ky As Variant
  Dim wsOrig As Worksheet
  Dim cell As Range
  Dim col As Long, LastCol As Long
 
  Set wsOrig = Sheets(1)
  Set d = CreateObject("Scripting.Dictionary")
  With wsOrig
    LastCol = .Cells(1, Columns.Count).End(xlToLeft).Column
    For Each cell In .Range("B1").Resize(, LastCol - 1)
      d(cell.Value) = Empty
    Next cell
  End With
  Application.ScreenUpdating = False
  For Each Ky In d.Keys
    wsOrig.Copy After:=Sheets(Sheets.Count)
    With Sheets(Sheets.Count)
      For col = LastCol To 2 Step -1
        If Not .Cells(1, col).Text = Ky Then .Columns(col).Delete
      Next col
      With .UsedRange
        If .Columns.Count = 2 Then
          .Columns(2).Delete
        Else
          For col = 2 To .Columns.Count
            .AutoFilter Field:=col, Criteria1:=""
          Next col
          .Offset(1).EntireRow.Delete
          .Parent.AutoFilterMode = False
        End If
      End With
      .Name = Ky
    End With
  Next Ky
  wsOrig.Activate
  Application.ScreenUpdating = True
End Sub
Thank you so much for all the help! I was able to be much more productive and useful at work because of this.
 
Upvote 0
You're welcome. Glad we could help. :)
Hi I am sorry for all the questions, I just realized that I was not clear in the last question I posted.

For the days there aren't duplicates of, such as Tuesday, Wednesday, Thursday, and Saturday (from the example), I would still like the column on the left that says description to still only have the descriptions (AAAA-GGGG in example) that have an X in the column to show up, I was just trying to get rid of the column with the day listed in the first cell to disappear.

For example for Tuesday the Sheet would look like this:
Example Sheet.xlsm
AB
1Description
2
3BBBB
4CCCC
5FFFF
6GGGG
Tuesday


I am sorry for the hassle and I really appreciate the help
 
Upvote 0
This?
VBA Code:
Sub Make_Sheets_v3()
  Dim d As Object
  Dim Ky As Variant
  Dim wsOrig As Worksheet
  Dim cell As Range
  Dim col As Long, LastCol As Long
  
  Set wsOrig = Sheets(1)
  Set d = CreateObject("Scripting.Dictionary")
  With wsOrig
    LastCol = .Cells(1, Columns.Count).End(xlToLeft).Column
    For Each cell In .Range("B1").Resize(, LastCol - 1)
      d(cell.Value) = Empty
    Next cell
  End With
  Application.ScreenUpdating = False
  For Each Ky In d.Keys
    wsOrig.Copy After:=Sheets(Sheets.Count)
    With Sheets(Sheets.Count)
      For col = LastCol To 2 Step -1
        If Not .Cells(1, col).Text = Ky Then .Columns(col).Delete
      Next col
      With .UsedRange
          For col = 2 To .Columns.Count
            .AutoFilter Field:=col, Criteria1:=""
          Next col
          .Offset(1).EntireRow.Delete
          .Parent.AutoFilterMode = False
          If .Columns.Count = 2 Then .Columns(2).Delete
      End With
      .Name = Ky
    End With
  Next Ky
  wsOrig.Activate
  Application.ScreenUpdating = True
End Sub
 
Upvote 0
Solution
This?
VBA Code:
Sub Make_Sheets_v3()
  Dim d As Object
  Dim Ky As Variant
  Dim wsOrig As Worksheet
  Dim cell As Range
  Dim col As Long, LastCol As Long
 
  Set wsOrig = Sheets(1)
  Set d = CreateObject("Scripting.Dictionary")
  With wsOrig
    LastCol = .Cells(1, Columns.Count).End(xlToLeft).Column
    For Each cell In .Range("B1").Resize(, LastCol - 1)
      d(cell.Value) = Empty
    Next cell
  End With
  Application.ScreenUpdating = False
  For Each Ky In d.Keys
    wsOrig.Copy After:=Sheets(Sheets.Count)
    With Sheets(Sheets.Count)
      For col = LastCol To 2 Step -1
        If Not .Cells(1, col).Text = Ky Then .Columns(col).Delete
      Next col
      With .UsedRange
          For col = 2 To .Columns.Count
            .AutoFilter Field:=col, Criteria1:=""
          Next col
          .Offset(1).EntireRow.Delete
          .Parent.AutoFilterMode = False
          If .Columns.Count = 2 Then .Columns(2).Delete
      End With
      .Name = Ky
    End With
  Next Ky
  wsOrig.Activate
  Application.ScreenUpdating = True
End Sub
Yes that is perfect! Thank you for all the input and patience.
 
Upvote 0

Forum statistics

Threads
1,214,653
Messages
6,120,755
Members
448,989
Latest member
mariah3

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