I had earlier code on this but could not get it to work. This is new code that is supposed to work, but still has a glitch somewhere. What the HideLabelColumns macro is supposed to do is look at a part of my range (only columns I through AW) and it should examine them from right to left-- if the column range (for each of the columns, the data are rows 8 through 14 although this will vary every day when the macro is run), if they are all blank, it should hide the column, then move to the left one column and do the same thing, continuing until it hits a column that has data in it-- when it finds that, it exits the sub. I tried to attach my workbook but it is too large. I have data in at least one cell in all the columns except AW and AV, so the macro should hide those columns only and leave all the rest. But when I run it, it hides nothing. The code looks like this:
Can anyone tell me what I have wrong in this sub? Would you absolutely need a copy of my workbook to look at? I might be able to remove a lot of other macros and send you that version if it makes it small enough.
Code:
Sub HideLabelColumns()
Application.ScreenUpdating = False
Dim LastRow As Long
LastRow = Sheets("general_report").Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
Dim lCol As Long
lCol = ActiveSheet.UsedRange.Columns.Count
Dim x As Long
For x = 49 To 1 Step -1
If WorksheetFunction.CountA(Range(Cells(2, x), Cells(LastRow, x))) = 0 Then
Columns(x).EntireColumn.Hidden = True
Exit Sub
End If
Next x
Application.ScreenUpdating = True
End Sub
Can anyone tell me what I have wrong in this sub? Would you absolutely need a copy of my workbook to look at? I might be able to remove a lot of other macros and send you that version if it makes it small enough.