Search code examples
excelrangecellvba

Hide last column in range and clear cells


I have a checklist workbook that allows you to check multiple items within a work order. The maximum amount of items is 15. I have a piece of code that will allow me to hide the columns, starting from the farthest right, that are not needed but cannot figure out how to clear a range within the column that will be hidden. I need to clear the data from row 7, 10, & 13:28 of the column that will be hidden.

Here is what I have:

Sub RemoveItem()
Dim i As Long
For i = 20 To 7 Step -1
    If Columns(i).Hidden = False Then
        bfirst = True
        Columns(i).Hidden = True
        Exit For
    End If
Next
End Sub

Any help would be much appreciated, Thanks!


Solution

  • This can probably be done better, but the below code will get the job done:

    Sub test()
    
    Dim i As Long
    
    For i = 20 To 7 Step -1  
        If Columns(i).Hidden = False Then  
            bfirst = True  
            Columns(i).Hidden = True
            Rows(7).ClearContents  
            Rows(10).ClearContents  
            Range(Cells(13, i), Cells(28, i)).ClearContents  
        End If  
    Next
    
    End Sub