1

Pop up charts in VBA Excel

I was very curious with the answer in the above link.

My question is, Can I pop up a graph with click of button(The button is on Sheet1) and when i go bach to
Sheet thr graph is gone

Community
  • 1
  • 1
Vijay Kumar
  • 13
  • 1
  • 5

2 Answers2

2

Pertinent to your question as it is worded in the Title (i.e. 'Can i launch userform from a button in a Sheet'), you can implement this functionality in two simple steps:

1). From Excel 'Developer' tab select Insert -> Button. By default, it will create a new Button1 and corresponding Click event handle will be added to the Module1

Sub Button1_Click()

End Sub

2). Provided that you have a User Form named UserForm1, add a single statement in that event handle that will open the UserForm1 on Button click:

Sub Button1_Click()
  UserForm1.Show
End Sub

In case you are trying to implement additional functionality, please include your code snippet highlighting the problematic part.

Hope this will help. Best regards,

Alexander Bell
  • 7,842
  • 3
  • 26
  • 42
  • 1
    I'm just going to add this here. Right Click on the UserForm and select "View Code". From there you will have 'Private Sub UserForm_Activate()' with whatever code you want to fire off when the form opens as well as any click events that occur ON that form. I'm just mentioning do to the nature of the question. In most cases, the click for the button need only show the userform. – peege Dec 21 '14 at 04:37
  • 1
    This was a clearly worded and well explained answer! – Alfa Bravo Sep 18 '17 at 23:29
1

Let's do it by point and basing on the answer you have posted.

Can I pop up a graph with click of button(The button is on Sheet1)?

Yes, you can. You need to:

  1. Put a Button on Sheet1 and associate to it a macro, let's say popUpChart;
  2. Create and show the chart:

    Sub popUpChart()
        Dim ch As UserForm1
        Set ch = New UserForm1
        'ch.CommandButton1_Click()
        'a) Uncomment the line above if you want to invoke the button press before to show the chart
        'b) If you decide to uncomment, remember to change the sub from "Private" to "Public"
        ch.Show
    End Sub
    

when i go bach to Sheet thr graph is gone

I don't understand well what you mean here. Do you mean "I want the graph to be gone when I go back to the sheet" or "I would like the chart to stay here but actually it's gone?

a) In the first case, it's enough to remove the image from the Image control of the form; b) In the second case, it's enough to remove the Set statement from the button's macro.

Matteo NNZ
  • 11,930
  • 12
  • 52
  • 89