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

edge37

Board Regular
Joined
Sep 1, 2016
Messages
73
Office Version
  1. 2021
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: 4

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
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

Forum statistics

Threads
1,223,702
Messages
6,173,961
Members
452,539
Latest member
delvey

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