Solved

VBA to resize column when clicked on.

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

749 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