Automatic Macro: Remove the click requirement?

SvelteRose

New Member
Joined
Mar 15, 2010
Messages
18
I installed an automatic macro but it only updates if you click on the worksheet. How do you make it so that the macro works instantly and not because you have to click it?

Here is my code:

Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If Range("H8").Value = 0 Then
        Columns("H:L").EntireColumn.Hidden = True
    Else
        Columns("H:L").EntireColumn.Hidden = False
    End If
    If Range("M8").Value = 0 Then
        Columns("M:Q").EntireColumn.Hidden = True
    Else
        Columns("M:Q").EntireColumn.Hidden = False
    End If
    If Range("R8").Value = 0 Then
        Columns("R:V").EntireColumn.Hidden = True
    Else
        Columns("R:V").EntireColumn.Hidden = False
    End If
    If Range("W8").Value = 0 Then
        Columns("W:AA").EntireColumn.Hidden = True
    Else
        Columns("W:AA").EntireColumn.Hidden = False
    End If
    If Range("AB8").Value = 0 Then
        Columns("AB:AG").EntireColumn.Hidden = True
    Else
        Columns("AB:AG").EntireColumn.Hidden = False
    End If
End Sub
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
You are probably using the wrong event!!
Exactly what are you trying to do?? How do the Values in Row 8 change??
When do you want the macro to run??

lenze
 
Upvote 0
You are probably using the wrong event!!
Exactly what are you trying to do?? How do the Values in Row 8 change??
When do you want the macro to run??

lenze


This is how my table is set up:


<table style="border-collapse: collapse; width: 407pt;" border="0" cellpadding="0" cellspacing="0" width="542"><col style="width: 48pt;" span="2" width="64"> <col style="width: 23pt;" width="30"> <col style="width: 48pt;" span="6" width="64"> <tbody><tr style="height: 12pt;" height="16"> <td class="xl70" style="height: 12pt; width: 48pt;" height="16" width="64">
</td> <td class="xl71" style="width: 48pt;" width="64">A</td> <td class="xl71" style="width: 23pt;" width="30">B</td> <td class="xl71" style="width: 48pt;" width="64">C</td> <td class="xl71" style="width: 48pt;" width="64">D</td> <td class="xl71" style="width: 48pt;" width="64">E</td> <td class="xl71" style="width: 48pt;" width="64">F</td> <td class="xl71" style="width: 48pt;" width="64">G</td> <td class="xl71" style="width: 48pt;" width="64">H</td> </tr> <tr style="height: 12pt;" height="16"> <td class="xl71" style="height: 12pt;" height="16">5</td> <td class="xl70">Act/Comp:</td> <td class="xl69" colspan="3" style="">Actual (1981-1985)</td> <td class="xl69">
</td> <td class="xl69">
</td> <td class="xl69">
</td> <td class="xl69">
</td> </tr> <tr style="height: 12pt;" height="16"> <td class="xl71" style="height: 12pt;" height="16">6</td> <td class="xl70">Acct Type:</td> <td class="xl69" colspan="2" style="">EBITDA</td> <td class="xl69">
</td> <td class="xl69">
</td> <td class="xl69">
</td> <td class="xl69">
</td> <td class="xl69">
</td> </tr> <tr style="height: 12pt;" height="16"> <td class="xl71" style="height: 12pt;" height="16">7</td> <td class="xl69">
</td> <td class="xl69">
</td> <td class="xl69">
</td> <td class="xl69">
</td> <td class="xl69">
</td> <td class="xl69">
</td> <td class="xl69">
</td> <td class="xl69">
</td> </tr> <tr style="height: 12pt;" height="16"> <td class="xl71" style="height: 12pt;" height="16">8</td> <td class="xl70">Years</td> <td class="xl69">
</td> <td class="xl69" align="right">1981</td> <td class="xl69" align="right">1982</td> <td class="xl69" align="right">1983</td> <td class="xl69" align="right">1984</td> <td class="xl69" align="right">1985</td> <td class="xl69" align="right">0</td> </tr> <tr style="height: 12pt;" height="16"> <td class="xl71" style="height: 12pt;" height="16">9</td> <td class="xl69">
</td> <td class="xl69">
</td> <td class="xl69" align="right">1</td> <td class="xl69" align="right">3</td> <td class="xl69" align="right">5</td> <td class="xl69" align="right">7</td> <td class="xl69" align="right">9</td> <td class="xl69" align="right">N/A</td> </tr> <tr style="height: 12pt;" height="16"> <td class="xl71" style="height: 12pt;" height="16">10</td> <td class="xl69">
</td> <td class="xl69">
</td> <td class="xl69" align="right">2</td> <td class="xl69" align="right">4</td> <td class="xl69" align="right">6</td> <td class="xl69" align="right">8</td> <td class="xl69" align="right">10</td> <td class="xl69" align="right">N/A</td> </tr> </tbody></table>
Basically, B5 determines what intervals will show. Since B5 says "Actual (1981-1985)", Row 8 will only go until 1985. The rest of the cells in Row 8 will read 0 until Column AG. If it was "Actual (1981-1990)", it would give us the years in Row 8 until 1990 and so forth. The cells below pull data from another worksheet using the year as a criteria which is why if it's 0, it pulls up N/A.

With the macro above, it hides any columns in which their Row 8 read 0. The macro works fine but the strange thing is, after I've updated B5 to another interval, I have to click on the worksheet in order for it to update. I am not sure what I am doing wrong, any suggestions?
 
Upvote 0
Once you get your other issue resolved, you should simplify your code. One example is
Code:
Columns("H:L").EntireColumn.Hidden = Range("H8").Value = 0
 
Upvote 0
Once you get your other issue resolved, you should simplify your code. One example is
Code:
Columns("H:L").EntireColumn.Hidden = Range("H8").Value = 0

I'm sorry, I'm unsure as to how I would incorporate this into my formula. :( I'm relatively new at macro and all self-taught so I'm still learning a few things.
 
Upvote 0
What are the choices fro B5?? You can run your macro when B5 is changed?

lenze
 
Upvote 0
B5 is a vlookup formula linked to a drop down menu from another worksheet. Basically, I'm creating a dashboard comparing certain financial data and B5 pulls up which intervals I want to view the financial data at. For instance, Comparable (5yr), Comparable (10yr), Actual (1981-1985), Actual (1981-1990), etc.

I need to be able to do a 5yr/10yr/15yr view on the dashboard chart. I got everything figured out except for the horizontal axis on the chart.

If you choose something like Actual (1981-1985), it will only pull up data for 1981 to 1985. 1986 and on will be #N/A and on the chart, the y-axis (where the years are defined), it won't have 1986 and on. However, even though the years don't show, the chart still reserves space for it.

If you hide the columns that 1986+ data is suppose to show up on, the axis only picks up data on the columns that haven't been hidden. That's why I needed an automatic macro that would hide columns based on a specific cell.

I know that explanation is very confusing to grasp so basically, I need to build a macro that will hide certain columns automatically based on a value in cell B5. The above works. Unfortunately, every time B5's value changes, the column hide macro will only update itself if you click on macro's worksheet.

I'm not sure where I'm screwing up with the coding as it is very simple. I'd like the macro to work the instant I change B5's value but my code isn't working. Suggestions please?
 
Upvote 0
You may want to consider a non-VBA solution. See
Dynamic Charts
http://www.tushar-mehta.com/excel/newsgroups/dynamic_charts/index.html

B5 is a vlookup formula linked to a drop down menu from another worksheet. Basically, I'm creating a dashboard comparing certain financial data and B5 pulls up which intervals I want to view the financial data at. For instance, Comparable (5yr), Comparable (10yr), Actual (1981-1985), Actual (1981-1990), etc.

I need to be able to do a 5yr/10yr/15yr view on the dashboard chart. I got everything figured out except for the horizontal axis on the chart.

If you choose something like Actual (1981-1985), it will only pull up data for 1981 to 1985. 1986 and on will be #N/A and on the chart, the y-axis (where the years are defined), it won't have 1986 and on. However, even though the years don't show, the chart still reserves space for it.

If you hide the columns that 1986+ data is suppose to show up on, the axis only picks up data on the columns that haven't been hidden. That's why I needed an automatic macro that would hide columns based on a specific cell.

I know that explanation is very confusing to grasp so basically, I need to build a macro that will hide certain columns automatically based on a value in cell B5. The above works. Unfortunately, every time B5's value changes, the column hide macro will only update itself if you click on macro's worksheet.

I'm not sure where I'm screwing up with the coding as it is very simple. I'd like the macro to work the instant I change B5's value but my code isn't working. Suggestions please?
 
Upvote 0
Unfortunately, that doesn't tell me at all where to start. I know how to define names and formulas. However, incorporating them into a multiple series chart is where I am getting lost. So once more, I shall try explaining. I've added ticker symbols to simply differentiate between the different series.

My table:

<table style="border-collapse: collapse; width: 407pt;" border="0" cellpadding="0" cellspacing="0" width="542"><tbody><tr style="height: 12pt;" height="16"><td class="xl70" style="height: 12pt; width: 48pt;" width="64" height="16">
</td> <td class="xl71" style="width: 48pt;" width="64">A</td> <td class="xl71" style="width: 23pt;" width="30">B</td> <td class="xl71" style="width: 48pt;" width="64">C</td> <td class="xl71" style="width: 48pt;" width="64">D</td> <td class="xl71" style="width: 48pt;" width="64">E</td> <td class="xl71" style="width: 48pt;" width="64">F</td> <td class="xl71" style="width: 48pt;" width="64">G</td> <td class="xl71" style="width: 48pt;" width="64">H</td> </tr> <tr style="height: 12pt;" height="16"> <td class="xl71" style="height: 12pt;" height="16">5</td> <td class="xl70">Act/Comp:</td> <td class="xl69" colspan="3" style="">Actual (1981-1985)</td> <td class="xl69">
</td> <td class="xl69">
</td> <td class="xl69">
</td> <td class="xl69">
</td> </tr> <tr style="height: 12pt;" height="16"> <td class="xl71" style="height: 12pt;" height="16">6</td> <td class="xl70">Acct Type:</td> <td class="xl69" colspan="2" style="">EBITDA</td> <td class="xl69">
</td> <td class="xl69">
</td> <td class="xl69">
</td> <td class="xl69">
</td> <td class="xl69">
</td> </tr> <tr style="height: 12pt;" height="16"> <td class="xl71" style="height: 12pt;" height="16">7</td> <td class="xl69">
</td> <td class="xl69">
</td> <td class="xl69">
</td> <td class="xl69">
</td> <td class="xl69">
</td> <td class="xl69">
</td> <td class="xl69">
</td> <td class="xl69">
</td> </tr> <tr style="height: 12pt;" height="16"> <td class="xl71" style="height: 12pt;" height="16">8</td> <td class="xl70">Name</td> <td class="xl69">
</td> <td class="xl69" align="right">1981</td> <td class="xl69" align="right">1982</td> <td class="xl69" align="right">1983</td> <td class="xl69" align="right">1984</td> <td class="xl69" align="right">1985</td> <td class="xl69" align="right">0</td> </tr> <tr style="height: 12pt;" height="16"> <td class="xl71" style="height: 12pt;" height="16">9</td> <td class="xl69">AAPL
</td> <td class="xl69">
</td> <td class="xl69" align="right">1</td> <td class="xl69" align="right">3</td> <td class="xl69" align="right">5</td> <td class="xl69" align="right">7</td> <td class="xl69" align="right">9</td> <td class="xl69" align="right">N/A</td> </tr> <tr style="height: 12pt;" height="16"> <td class="xl71" style="height: 12pt;" height="16">10</td> <td class="xl69">APP.OS
</td> <td class="xl69">
</td> <td class="xl69" align="right">2</td> <td class="xl69" align="right">4</td> <td class="xl69" align="right">6</td> <td class="xl69" align="right">8</td> <td class="xl69" align="right">10</td> <td class="xl69" align="right">N/A</td></tr></tbody></table>
This is what it would be if the B5 said "Actual (1981-1986)":

<table style="border-collapse: collapse; width: 407pt;" border="0" cellpadding="0" cellspacing="0" width="542"><tbody><tr style="height: 12pt;" height="16"><td class="xl70" style="height: 12pt; width: 48pt;" width="64" height="16">
</td> <td class="xl71" style="width: 48pt;" width="64">A</td> <td class="xl71" style="width: 23pt;" width="30">B</td> <td class="xl71" style="width: 48pt;" width="64">C</td> <td class="xl71" style="width: 48pt;" width="64">D</td> <td class="xl71" style="width: 48pt;" width="64">E</td> <td class="xl71" style="width: 48pt;" width="64">F</td> <td class="xl71" style="width: 48pt;" width="64">G</td> <td class="xl71" style="width: 48pt;" width="64">H</td> </tr> <tr style="height: 12pt;" height="16"> <td class="xl71" style="height: 12pt;" height="16">5</td> <td class="xl70">Act/Comp:</td> <td class="xl69" colspan="3" style="">Actual (1981-1986)</td> <td class="xl69">
</td> <td class="xl69">
</td> <td class="xl69">
</td> <td class="xl69">
</td> </tr> <tr style="height: 12pt;" height="16"> <td class="xl71" style="height: 12pt;" height="16">6</td> <td class="xl70">Acct Type:</td> <td class="xl69" colspan="2" style="">EBITDA</td> <td class="xl69">
</td> <td class="xl69">
</td> <td class="xl69">
</td> <td class="xl69">
</td> <td class="xl69">
</td> </tr> <tr style="height: 12pt;" height="16"> <td class="xl71" style="height: 12pt;" height="16">7</td> <td class="xl69">
</td> <td class="xl69">
</td> <td class="xl69">
</td> <td class="xl69">
</td> <td class="xl69">
</td> <td class="xl69">
</td> <td class="xl69">
</td> <td class="xl69">
</td> </tr> <tr style="height: 12pt;" height="16"> <td class="xl71" style="height: 12pt;" height="16">8</td> <td class="xl70">Name</td> <td class="xl69">
</td> <td class="xl69" align="right">1981</td> <td class="xl69" align="right">1982</td> <td class="xl69" align="right">1983</td> <td class="xl69" align="right">1984</td> <td class="xl69" align="right">1985</td> <td class="xl69" align="right">1986</td> </tr> <tr style="height: 12pt;" height="16"> <td class="xl71" style="height: 12pt;" height="16">9</td> <td class="xl69">AAPL
</td> <td class="xl69">
</td> <td class="xl69" align="right">1</td> <td class="xl69" align="right">3</td> <td class="xl69" align="right">5</td> <td class="xl69" align="right">7</td> <td class="xl69" align="right">9</td> <td class="xl69" align="right">11</td> </tr> <tr style="height: 12pt;" height="16"> <td class="xl71" style="height: 12pt;" height="16">10</td> <td class="xl69">APP.OS
</td> <td class="xl69">
</td> <td class="xl69" align="right">2</td> <td class="xl69" align="right">4</td> <td class="xl69" align="right">6</td> <td class="xl69" align="right">8</td> <td class="xl69" align="right">10</td> <td class="xl69" align="right">12</td></tr></tbody></table>
How can I create a dynamic chart that knows to read 1981 to 1985 when B5 says that and for it to read 1981-1986 when B5 says that?

Edited to say that I don't think it's necessary for me to have 1986 numbers be zeroed out - my main thing is to ask the chart to read 1981 to 1985 when I want to see those intervals and then 1981-1986 when I want to see those intervals.
 
Last edited:
Upvote 0
Create the following named formulas.

EndYr =0+MID(Sheet2!$B$5,FIND("-",Sheet2!$B$5)+1,FIND(")",Sheet2!$B$5)-FIND("-",Sheet2!$B$5)-1)
StartYr =0+MID(Sheet2!$B$5,FIND("(",Sheet2!$B$5)+1,FIND("-",Sheet2!$B$5)-FIND("(",Sheet2!$B$5)-1)
XVals =Sheet2!$C$8:INDEX(Sheet2!$8:$8,MATCH(EndYr,Sheet2!$8:$8,0))
YAAPL =OFFSET(XVals,1,0)



Use XVals in the chart series for the x values and YAAPL for the 1st y values.
Unfortunately, that doesn't tell me at all where to start. I know how to define names and formulas. However, incorporating them into a multiple series chart is where I am getting lost. So once more, I shall try explaining. I've added ticker symbols to simply differentiate between the different series.

My table:

<table style="border-collapse: collapse; width: 407pt;" border="0" cellpadding="0" cellspacing="0" width="542"><tbody><tr style="height: 12pt;" height="16"><td class="xl70" style="height: 12pt; width: 48pt;" width="64" height="16">
</td> <td class="xl71" style="width: 48pt;" width="64">A</td> <td class="xl71" style="width: 23pt;" width="30">B</td> <td class="xl71" style="width: 48pt;" width="64">C</td> <td class="xl71" style="width: 48pt;" width="64">D</td> <td class="xl71" style="width: 48pt;" width="64">E</td> <td class="xl71" style="width: 48pt;" width="64">F</td> <td class="xl71" style="width: 48pt;" width="64">G</td> <td class="xl71" style="width: 48pt;" width="64">H</td> </tr> <tr style="height: 12pt;" height="16"> <td class="xl71" style="height: 12pt;" height="16">5</td> <td class="xl70">Act/Comp:</td> <td class="xl69" colspan="3" style="">Actual (1981-1985)</td> <td class="xl69">
</td> <td class="xl69">
</td> <td class="xl69">
</td> <td class="xl69">
</td> </tr> <tr style="height: 12pt;" height="16"> <td class="xl71" style="height: 12pt;" height="16">6</td> <td class="xl70">Acct Type:</td> <td class="xl69" colspan="2" style="">EBITDA</td> <td class="xl69">
</td> <td class="xl69">
</td> <td class="xl69">
</td> <td class="xl69">
</td> <td class="xl69">
</td> </tr> <tr style="height: 12pt;" height="16"> <td class="xl71" style="height: 12pt;" height="16">7</td> <td class="xl69">
</td> <td class="xl69">
</td> <td class="xl69">
</td> <td class="xl69">
</td> <td class="xl69">
</td> <td class="xl69">
</td> <td class="xl69">
</td> <td class="xl69">
</td> </tr> <tr style="height: 12pt;" height="16"> <td class="xl71" style="height: 12pt;" height="16">8</td> <td class="xl70">Name</td> <td class="xl69">
</td> <td class="xl69" align="right">1981</td> <td class="xl69" align="right">1982</td> <td class="xl69" align="right">1983</td> <td class="xl69" align="right">1984</td> <td class="xl69" align="right">1985</td> <td class="xl69" align="right">0</td> </tr> <tr style="height: 12pt;" height="16"> <td class="xl71" style="height: 12pt;" height="16">9</td> <td class="xl69">AAPL
</td> <td class="xl69">
</td> <td class="xl69" align="right">1</td> <td class="xl69" align="right">3</td> <td class="xl69" align="right">5</td> <td class="xl69" align="right">7</td> <td class="xl69" align="right">9</td> <td class="xl69" align="right">N/A</td> </tr> <tr style="height: 12pt;" height="16"> <td class="xl71" style="height: 12pt;" height="16">10</td> <td class="xl69">APP.OS
</td> <td class="xl69">
</td> <td class="xl69" align="right">2</td> <td class="xl69" align="right">4</td> <td class="xl69" align="right">6</td> <td class="xl69" align="right">8</td> <td class="xl69" align="right">10</td> <td class="xl69" align="right">N/A</td></tr></tbody></table>
This is what it would be if the B5 said "Actual (1981-1986)":

<table style="border-collapse: collapse; width: 407pt;" border="0" cellpadding="0" cellspacing="0" width="542"><tbody><tr style="height: 12pt;" height="16"><td class="xl70" style="height: 12pt; width: 48pt;" width="64" height="16">
</td> <td class="xl71" style="width: 48pt;" width="64">A</td> <td class="xl71" style="width: 23pt;" width="30">B</td> <td class="xl71" style="width: 48pt;" width="64">C</td> <td class="xl71" style="width: 48pt;" width="64">D</td> <td class="xl71" style="width: 48pt;" width="64">E</td> <td class="xl71" style="width: 48pt;" width="64">F</td> <td class="xl71" style="width: 48pt;" width="64">G</td> <td class="xl71" style="width: 48pt;" width="64">H</td> </tr> <tr style="height: 12pt;" height="16"> <td class="xl71" style="height: 12pt;" height="16">5</td> <td class="xl70">Act/Comp:</td> <td class="xl69" colspan="3" style="">Actual (1981-1986)</td> <td class="xl69">
</td> <td class="xl69">
</td> <td class="xl69">
</td> <td class="xl69">
</td> </tr> <tr style="height: 12pt;" height="16"> <td class="xl71" style="height: 12pt;" height="16">6</td> <td class="xl70">Acct Type:</td> <td class="xl69" colspan="2" style="">EBITDA</td> <td class="xl69">
</td> <td class="xl69">
</td> <td class="xl69">
</td> <td class="xl69">
</td> <td class="xl69">
</td> </tr> <tr style="height: 12pt;" height="16"> <td class="xl71" style="height: 12pt;" height="16">7</td> <td class="xl69">
</td> <td class="xl69">
</td> <td class="xl69">
</td> <td class="xl69">
</td> <td class="xl69">
</td> <td class="xl69">
</td> <td class="xl69">
</td> <td class="xl69">
</td> </tr> <tr style="height: 12pt;" height="16"> <td class="xl71" style="height: 12pt;" height="16">8</td> <td class="xl70">Name</td> <td class="xl69">
</td> <td class="xl69" align="right">1981</td> <td class="xl69" align="right">1982</td> <td class="xl69" align="right">1983</td> <td class="xl69" align="right">1984</td> <td class="xl69" align="right">1985</td> <td class="xl69" align="right">1986</td> </tr> <tr style="height: 12pt;" height="16"> <td class="xl71" style="height: 12pt;" height="16">9</td> <td class="xl69">AAPL
</td> <td class="xl69">
</td> <td class="xl69" align="right">1</td> <td class="xl69" align="right">3</td> <td class="xl69" align="right">5</td> <td class="xl69" align="right">7</td> <td class="xl69" align="right">9</td> <td class="xl69" align="right">11</td> </tr> <tr style="height: 12pt;" height="16"> <td class="xl71" style="height: 12pt;" height="16">10</td> <td class="xl69">APP.OS
</td> <td class="xl69">
</td> <td class="xl69" align="right">2</td> <td class="xl69" align="right">4</td> <td class="xl69" align="right">6</td> <td class="xl69" align="right">8</td> <td class="xl69" align="right">10</td> <td class="xl69" align="right">12</td></tr></tbody></table>
How can I create a dynamic chart that knows to read 1981 to 1985 when B5 says that and for it to read 1981-1986 when B5 says that?

Edited to say that I don't think it's necessary for me to have 1986 numbers be zeroed out - my main thing is to ask the chart to read 1981 to 1985 when I want to see those intervals and then 1981-1986 when I want to see those intervals.
 
Upvote 0

Forum statistics

Threads
1,216,077
Messages
6,128,684
Members
449,463
Latest member
Jojomen56

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top