Solved

VBA to resize column when clicked on.

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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

Improved? Move/Copy Add-in Replacement - How to avoid the annoying, “A formula or sheet you want to move or copy contains the name XXX, which already exists on the destination worksheet.” David Miller (dlmille)  It was one of those days… I wa…
: 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 how to use longer labels with horizontal bar charts instead of the vertical column chart.
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.

751 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