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

edge37

Board Regular
Joined
Sep 1, 2016
Messages
102
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: 5

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
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

Forum statistics

Threads
1,224,728
Messages
6,180,594
Members
452,988
Latest member
wcself81

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