Toggle hide/unhide columns without affecting tables or text boxes on top

edge37

Board Regular
Joined
Sep 1, 2016
Messages
105
Office Version
  1. 365
Platform
  1. Windows
Hi! I'm trying this code to toggle hide/unhide columns in my worksheet:
VBA Code:
Sub ToggleColumnsAFtoAIWithoutAffectingObjects()
    Dim ws As Worksheet
    Dim targetColumns As Range
    Dim obj As Shape

    ' Set the worksheet
    Set ws = ThisWorkbook.Sheets("10A (1)")
    
    ' Set the target columns
    Set targetColumns = ws.Columns("AF:AI")
    
    ' Check current visibility and toggle
    If targetColumns.EntireColumn.Hidden Then
        targetColumns.EntireColumn.Hidden = False ' Unhide columns
    Else
        targetColumns.EntireColumn.Hidden = True ' Hide columns
    End If

    ' Loop through shapes (objects) and ensure they remain visible
    For Each obj In ws.Shapes
        ' If the shape's top-left cell overlaps the target columns, make it visible
        If Not Intersect(ws.Range(obj.TopLeftCell.Address), targetColumns) Is Nothing Then
            obj.Visible = True
        End If
    Next obj
End Sub
But when I do so, the objects on top of those columns (tables, drawings or text boxes) gets resized. Is there a way to hide/show columns in a way I can keep objects on top of them in the same place and size? Pic I sent show before/after applying the code.

Thanks
 

Attachments

  • Screenshot_36.jpg
    Screenshot_36.jpg
    77.8 KB · Views: 12

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
See if the code in blue fixes it for you.
Technically you shouldn't need to run this each time, so see if you want to remove after it has changed the property to FreeFloating.

Rich (BB code):
Sub ToggleColumnsAFtoAIWithoutAffectingObjects()
    Dim ws As Worksheet
    Dim targetColumns As Range
    Dim obj As Shape

    ' Set the worksheet
    Set ws = ThisWorkbook.Sheets("10A (1)")
    
    ' Set the target columns
    Set targetColumns = ws.Columns("AF:AI")
    
    For Each obj In ws.Shapes
        ' If the shape's top-left cell overlaps the target columns, make it visible
        If Not Intersect(ws.Range(obj.TopLeftCell.Address), targetColumns) Is Nothing Then
            obj.Placement = xlFreeFloating
        End If
    Next obj
    
    ' Check current visibility and toggle
    If targetColumns.EntireColumn.Hidden Then
        targetColumns.EntireColumn.Hidden = False ' Unhide columns
    Else
        targetColumns.EntireColumn.Hidden = True ' Hide columns
    End If

    ' Loop through shapes (objects) and ensure they remain visible
    For Each obj In ws.Shapes
        ' If the shape's top-left cell overlaps the target columns, make it visible
        If Not Intersect(ws.Range(obj.TopLeftCell.Address), targetColumns) Is Nothing Then
            obj.Visible = True
        End If
    Next obj
End Sub
 
Upvote 0
Solution
Not related to your question about the shapes but if you are interested, this section ..
VBA Code:
' Check current visibility and toggle
If targetColumns.EntireColumn.Hidden Then
    targetColumns.EntireColumn.Hidden = False ' Unhide columns
Else
    targetColumns.EntireColumn.Hidden = True ' Hide columns
End If

.. could be replaced with this
VBA Code:
'Toggle current visibility
targetColumns.Hidden = Not targetColumns.Hidden

(noting also that .EntireColumn has been removed as unnecessary since targetColumns was already defined as entire columns :))
 
Upvote 0
Thanks for letting me know. Glad I could help.
Hello again

Alex Blakenburg

Can you please help me with something in your code, please? I need to create many pages that needs this code, but because this part:
VBA Code:
Set ws = ThisWorkbook.Sheets("10A (1)")
I can only make it work for that specific sheet, If I copy the worksheet (and name it, for example "10B (1)") and apply the code, it affects only the columns in "10A (1) sheet, not in the sheet it is on. Each of the 7 tabs I need are going to be named: "10A (1)", "10B (1)", "10C (1)", "11A (1)", "11B (1)" , "11C (1)", and "11C BIO (1)".

Thank you for your assistance again.
 
Upvote 0
Or should I just modify the code for each page? (Eventhough I wanted to avoid this to leave the VBA less clogged with macros, but...)
 
Upvote 0
How are you triggering the code to run ? If it is being triggered from the ActiveSheet then all you need to do is change the Set ws line to:
VBA Code:
    ' Set the worksheet
    Set ws = ActiveSheet
 
Upvote 0

Forum statistics

Threads
1,225,610
Messages
6,185,986
Members
453,333
Latest member
BioCoder84

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