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:-
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