remove columns whose headers don't match sheet name

Kaps_mr2

Well-known Member
Joined
Jul 5, 2008
Messages
1,589
Office Version
  1. 365
Platform
  1. Windows
Hello,

I am trying to loop through all the sheets in a workbook and delete any columns where the value in row 1 is not equal to the sheet name. The data always starts in column 16 and extends for a variable number of columns to the right. My code is below:-

VBA Code:
Public Sub remove_extra_categories()
Dim sh As Long
Dim c As Long
Dim category_data_range As Range
Dim sheet_name As String
Dim last_column_of_data As String
Dim current_column_header As String

For sh = 1 To no_of_categories
    sheet_name = output_workbook.Sheets(sh).Name
    last_column_of_data = output_workbook.Sheets(sh).Cells(1, Columns.Count).End(xlToLeft).Column - 13
    'MsgBox sheet_name & " " & last_column_of_data
    
    For c = last_column_of_data To 16 Step -1
        current_column_header = output_workbook.Sheets(sh).Range("o1").Offset(0, c).Value
        
        If current_column_header <> sheet_name Then
        output_workbook.Sheets(sh).Columns(c).Delete
        End If
        
    Next c
    
Next sh
 

Attachments

  • data.png
    data.png
    168.4 KB · Views: 8
Where is the code that runs the User file select dialog?
VBA Code:
Public Sub select_file()
Set fd = Application.FileDialog(msoFileDialogFilePicker)
    
    With fd
        .Filters.Clear
        .Title = "Select an Excel File"
        .Filters.Add "Excel Files", "*.xlsx", 1
        .AllowMultiSelect = False
        
        Dim sFile As String
    
        If .Show = True Then
            sFile = .SelectedItems(1)
        End If
    End With
    
    If sFile <> "" Then
        Workbooks.Open sFile     ' Open the Excel file.
        Set source_file = ActiveWorkbook
    End If




End Sub
 
Upvote 0

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
Change wb to:
VBA Code:
Set wb = ActiveWorkbook
It should work if the Workbook you want to edit is currently ActiveWorkbook. Otherwise, we may have to add code for user to select Workbook, or create a Class Module to store the Workbook name.
 
Upvote 0
Change wb to:
VBA Code:
Set wb = ActiveWorkbook
It should work if the Workbook you want to edit is currently ActiveWorkbook. Otherwise, we may have to add code for user to select Workbook, or create a Class Module to store the Workbook name.
it doesn't work. I can't see why using : set wb =output_workbook doesn't work.
 
Upvote 0
Because in that Sub you haven't defined the Variable output_workbook. Once the select_File Sub completes, all of its Variables are killed.
 
Upvote 0
Because in that Sub you haven't defined the Variable output_workbook. Once the select_File Sub completes, all of its Variables are killed.
Example:
VBA Code:
Dim output_workbook as Workbook
Set output_workbook = Workbooks("NameOfWorkbook.xlsx")
 
Upvote 0

Forum statistics

Threads
1,224,825
Messages
6,181,190
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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