Solved

VBA to resize column when clicked on.

Posted on 2014-09-22
4
156 Views
Last Modified: 2014-09-22
I would like code that would fire when certain cells are clicked on.  See attached example.  normally the column widths are 0.44.  I have over 400 columns of these.  When the merged cells in row 4 are clicked, I would like to expand the 7th column over to width of 5. (Highlighted in the example).  When the cell is clicked again I would like to shrink that same column (7 over) back to 0.44 width. ANy help on this please?

Thanks in advance.
0
Comment
Question by:acdecal
  • 2
  • 2
4 Comments
 

Author Comment

by:acdecal
ID: 40337127
Sorry forgot the attached file.
Example1.xlsx
0
 
LVL 27

Accepted Solution

by:
Glenn Ray earned 500 total points
ID: 40337247
You can add a Worksheet_SelectionChange event to the Sheet object in VBA:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If ActiveCell.Row = 5 And IsDate(ActiveCell.Value) Then
        If ActiveCell.Offset(0, 3).EntireColumn.ColumnWidth < 1 Then
            ActiveCell.Offset(0, 3).EntireColumn.ColumnWidth = 5
        Else
            ActiveCell.Offset(0, 3).EntireColumn.ColumnWidth = 0.44
        End If
    End If
End Sub

Open in new window

Note that because of the merged cells in row 5, one only offsets 3 cells to the right to execute the column width change.

Modified workbook attached.

-Glenn
EE-Example1.xlsm
0
 

Author Comment

by:acdecal
ID: 40337256
That works.  Thanks!
0
 
LVL 27

Expert Comment

by:Glenn Ray
ID: 40337288
You're Welcome.  One caveat:  If you select a cell to expand/shrink the weekly total, clicking again on the cell does not reverse the change.  One has to click/move to another cell and then click the original cell again.

-Glenn
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
Introduction While answering a recent question (http:/Q_27311462.html), I created an alternative function to the Excel Concatenate() function that you might find useful.  I tested several solutions and share the results in this article as well as t…
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now