Solved

VBA to resize column when clicked on.

Posted on 2014-09-22
4
164 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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Approximate matching with VLOOKUP and MATCH seems to me to be a greatly under-used technique, and one which is vital for getting good performance out of large lookups. Until recently I would always have advised using an exact match for simplicity an…
: Microsoft Office Collaborate for free and online versions of Microsoft  Word, Excel, Powerpoint, OneNote, Onedrive , Email, Calendar etc. In short we can say that Microsoft office is a suite of servers, applications and services developed by  Micr…
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

856 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