Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hide columns I am confused now. Could you tell me what is wrong below code? Sub

ID: 3561118 • Letter: H

Question

Hide columns

I am confused now.

Could you tell me what is wrong below code?

Sub HideColumn()

Dim IRsh As Worksheet
Dim lC As Long
Dim G As Range
Dim ColumnTotal As Long
Dim lCol As Long
Dim rCell As Range
Dim LASTr As Long
Set IRsh = ThisWorkbook.Worksheets("ABC")
'lC = LastColm(IRsh)
lC = IRsh.Cells(1, Columns.Count).End(xlToLeft).Column
LASTr = IRsh.Cells(Rows.Count, 1).End(xlUp).Row
For lCol = 1 To lC Step 1
Debug.Print lCol

  
    Set rCell = IRsh.Cells(1, lCol)
   
    If rCell.Value = "PRE" Then
       
   
      With IRsh
       .Range(lCol & ":" & lC).EntireColumn.Hidden = True

     End With
End If
Next
   
End Sub

Explanation / Answer

%Try using this snippet for your loop.

    For lCol = 1 To lC Step 1
        Debug.Print lCol
        Set rCell = IRsh.Cells(1, lCol)
        If UCase(rCell.Value) = "PRE" Then
            IRsh.Cells(1, lCol).Resize(1, (lC - lCol) + 1).EntireColumn.Hidden = _

               Not IRsh.Cells(1, lCol).EntireColumn.Hidden
            Exit For
        End If
    Next

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote