Solved

VBA to resize column when clicked on.

Posted on 2014-09-22
4
174 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
This article describes how you can use Custom Document Properties to store settings and other information in your workbook so that they will be available the next time you open the workbook.
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

622 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