Hide Merged Columns

Takes2ToTango

Board Regular
Joined
May 23, 2023
Messages
63
Office Version
  1. 365
Platform
  1. Windows
Hi All,

I have tried different codes but nothing seems to work.

I am after some code that will know where the merged cells end and then hide the columns it appears but leaves the first column

For example:

1725284446274.png


I double click The merged cell 'Test Merge'. It then know that the range A1:I1 is the merged cell range and proceeds to hide all columns besides A1.

This may be impossible but if anyone could provide any insight or alternatives, i would be very greatful
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
I don't really understand. Have you tried "center across selection" instead of merged cells?
 
Upvote 0
.. what proceeds to hide all columns besides A1?
.. and do you mean all columns besides column A?
Hi,

So it would be a double click event which then hides the correct columns. The end result would look like this.
1725285123525.png


Column A remains visible but the rest are hidden until you right click and unhide the columns
 
Upvote 0
I don't really understand. Have you tried "center across selection" instead of merged cells?
I have given it a go, however I would still need to hide the selected columns.
If they were a fixed range it would be an easy fix but users could add or remove columns within the range,
 
Upvote 0
For a double-click inside that merged area, try this
VBA Code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
    If Not Intersect(Target, Cells(1, 1).MergeArea) Is Nothing Then
        Cancel = True
        Range(Cells(1, 2), Cells(1, Cells(1, 1).MergeArea.Columns.Count)).EntireColumn.Hidden = True
    End If
End Sub
 
Upvote 0
Solution
For a double-click inside that merged area, try this
VBA Code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
    If Not Intersect(Target, Cells(1, 1).MergeArea) Is Nothing Then
        Cancel = True
        Range(Cells(1, 2), Cells(1, Cells(1, 1).MergeArea.Columns.Count)).EntireColumn.Hidden = True
    End If
End Sub
That works perfectly,

Many thanks for your help!
 
Upvote 0
Just another thought, depending on why you are doing this and how you would use it.
You could Group (on the Data ribbon tab) columns B:I so that you had the Collapse/Expand options at the top of the worksheet like below. Then instead of using double-click, you could just click the 1/2 buttons at the left or the -/+ buttons above to collapse and expend the columns.

1725317705313.png


1725317730770.png
 
Upvote 0

Forum statistics

Threads
1,221,309
Messages
6,159,164
Members
451,543
Latest member
cesymcox

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