Can i launch userform from a button in a Sheet - vba

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

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,

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:
Put a Button on Sheet1 and associate to it a macro, let's say popUpChart;
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.

Related

Get value from command button VBA

I have created A userform with few command buttons.
I can't seem to figure out how do I get the information from them.
I want to open this userform from another one and then I want the user to choose an option from one of the buttons which will change a specific cell in the table itself
the userform I created
I did not write anything on this userform therefor there is no code from it to show only the design.
how do get the information from the buttons to be written in A specific cell on a specific worksheet?
double click on one of the buttons, in the code menu a new sub should appear. this looks something like:
Sub CommandButton1_onClick()
End sub
Alongside this event method, it also has a few properties. The one that is usefull here is the CommandButton1.Value, this represents the state of the button (either true or false iirc).
So build for each button (I strongly advice giving them custom names to prevent getting lost in the trouble) as sub like this:
Sub CommandButton1_onClick()
if CommandButton1.Value then
ThisWorkBook.WorkSheets("WorksheetName").Range("YourRange").Value = "Some value"
else
ThisWorkBook.WorkSheets("WorksheetName").Range("YourRange").Value = ""
end if
End sub

Excel Form pops automatically when sheet opens

I have a specific worksheet with 2 sheets(Sheet1 & Sheet2). For Sheet2 I have implemented a form for the table (Using the basic Excel Form from the top bar).
My problem is that I have to make the form appear automatically every time I open Sheet1 (even if the data from the form will be completed in Sheet2).
Is this possible? Or how can I do it? (I can also use VBA)
To show the DataForm associated with a Worksheet, you use the command Worksheet.ShowDataForm (MSDN Article)
To show the DataForm for Sheet1 whenever you go to Sheet2, you can use the Worksheet_Activate event in Sheet2, like so:
Option Explicit
Private Sub Worksheet_Activate()
Sheet1.ShowDataForm
End Sub
A quick way to figure things like this out is use the "Record Macro" button, carry out the action you want, and then hit "Stop Recording" and look at the macro

Presenter-only pop-up in Powerpoint

Is it possible to show a pop-up message (using VBA in Powerpoint) to the presenter which won't be visible to the "audience"?
Edit: Of course, it would be nice if you could tell me "how".
Thanks in advance!
Something like this, perhaps:
Add a userform with a label to contain the text you want to display.
Add this to the vba project:
Sub PopUp()
With UserForm1
.Show (vbModeless)
.Left = 100
End With
End Sub
Add a shape to any slide where you want to display the popup and give it an action setting of Run Macro: PopUp
This assumes that you've accepted the default userform name; change the subroutine to reflect any name changes if you like.
It also assumes that the main monitor is on the left side; if not, you'll need to change the .Left value.

How to use VBA to add a button to a sheet and assign its click event upon another button press

I am trying to automate a quarterly patient report for a local pharmacy and in doing so I have transferred it to excel. One portion of the automation is an add patient button on the cover page of the report that goes to a form for relevent information. The ok button on the form takes the information and formats it in a new sheet named according to the patient's name. The button also adds two newly created buttons to the patient sheet, a delete and edit button. I can create the buttons and place them, but I can not find any way to assign a click event to the buttons, since they are considered new objects on each page.
I have moved the button's main code to the workbook itself, so all I really need to put in the button's click event is a call to that method, but I can't find any way to access the new buttons' click events through vba, and since I need to call a method in VBA itself, I'm not sure I can use a macro either (fair note, I am not all that familiar with excel macros, so if the solution lies in them, I can use that too).
Here is the code that instantiates and places/sizes the delete button on the new sheet:
Dim btn As OLEObject
Set btn = ActiveSheet.OLEObjects.Add(ClassType:="Forms.CommandButton.1", Link:=False, DisplayAsIcon:=False)
With btn
.Name = "deletePatientButton"
.Object.Caption = "Delete Patient"
.Top = 5.25
.Left = 290.25
.Width = 75
.Height = 24.75
.PrintObject = False
End With
Here is the main method of the delete button placed in the workbook code itself (note it only really calls another verification form, so this may be redundant, but I wanted to put it in the workbook section for testing since I assumed it would have the largest scope):
Public Sub mainDeleteButton(sheet As Worksheet)
Dim confirmer As New deleteConfirmationForm
sheet.Activate
confirmer.Show
End Sub
Finally, here is an example of the click event I am hoping to be able to place, or replace with another solution:
Private Sub deletePatientButton_Click()
Call ThisWorkbook.mainDeleteButton(Me)
End Sub
Any help is more than appreciated!
It is possible to add the event code programmatically to the worksheet module (see this post). However, it may be easier to keep your buttons on template worksheets that already have the event code in them. Just copy your template to a new sheet, rename it, and add your patient data.

Order of Events for the SheetFollowHyperlink Event

I have a workbook that lists people's names and overall performance for the past few weeks. I was asked to make it so that when their names are clicked, the details for that person would show. The solution I came up with at the time was to point all of the hyperlinks to a sheet that reminded them to turn on their macros.
In the WorkSheet_Activate event, I redirected them to another sheet that populated with the details for the person they had selected. This works fine. If the user doesn't have their macros turned on, they get a friendly reminder, but if they do, they immediately get redirected. There's just one problem. Because the hyperlink takes them somewhere else first, it causes the infamous "screen flicker".
On researching, I found the FollowHyperlink Events (I think the workbook level event would be most suitable for my purposes, though). However, before I get started rebuilding it, I wanted to make sure that this would solve the "screen flicker".
On MSDN, it states that the Event occurs when the user clicks the hyperlink. I can't seem to find anywhere that directly states whether this Event is triggered before or after the user is directed to the other sheet, though. If it gets triggered right after, that wouldn't really help me, but if it gets triggered before, I could put an Application.ScreenUpdates = False in the event, and it would solve my problem.
TL;DR (Get to the point already):
Does the Workbook_SheetFollowHyperlink Event happen before or after the user is directed to where ever the hyperlink is pointed?
To answer your question, the event is triggered after the hyperlink is followed. You can demonstrate this using the code below:
Private Sub Workbook_SheetFollowHyperlink(ByVal Sh As Object, ByVal Target As Hyperlink)
Debug.Print "Workbook level: " & ActiveSheet.Name
End Sub
When the event is triggered, the ActiveSheet is the sheet directed to by the hyperlink.
I'm not sure of a way to solve your problem directly, even the Click() event won't be raised when you click a hyperlink. However, the Worksheet level hyperlink event handler will be called before the Workbook level handler and this may speed the process up enough to not see the flicker.
You can prove this if you leave the Workbook level event as is and add the following code to the Worksheet containing the hyperlinks:
Private Sub Worksheet_FollowHyperlink(ByVal Target As Hyperlink)
Debug.Print "Worksheet level: " & ActiveSheet.Name
Sleep 1000
Debug.Print "Leaving worksheet level event"
End Sub
After a few experiments (see below) I concluded that:
Allow the user to use the hyperlink columns when macros are disabled.
When macros are enabled, hide the current hyperlink column and show another column that gives the user a different "hyperlink" that takes them directly to where you want them to go.
The 2nd hyperlink can easily be "simulated" such that it picks up the other columns hyperlink. (See below)
I hope his helps
Interesting, here's a few things I tried
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
' This does not fire if the user clicks directly on the hyperlink text
' it only fires if when click on the cell space that is not text
With ActiveCell
If Hyperlinks.Count Then
MsgBox "hi" & .Hyperlinks(1).Range
.Hyperlinks(1).Follow
End If
End With
End Sub
However you could make the text in the cells look like a hyperlink but actually just be blue underlined text. You could then use the Worksheet_SelectionChange to go to a related cell.
The question then become how to store the related cell.
You want to store it so that if row and column are inserted on the destination sheet, the reference will adjust. (eg NOT in comments, or the description of named ranges etc..)
Depending on how much data you have there's a variety of ways you could choose from.
I think I would favour this:
Having a hidden column next to the cell on display that has a hyperlink. The cell on display has a formula that is set to pick up the value from the hidden column (I quite like the sound of this as you have the hyperlink column already - so just modify the above code to get the hyperlink from the column next to the clicked cell using offset perhaps)
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
' This does not fire if the user clicks directly on the hyperlink text
With ActiveCell.offset(0,1)
If Hyperlinks.Count Then
.Hyperlinks(1).Follow
End If
End With
End Sub
You could mess around with named ranges, but it will be a pain.
I've just realised that what I tried won't help much as you need the hyperlink to work when macros have not been enabled!
So to get the above to work you could change the columns displayed when the user enables macros. ie the hyperlink column is shown when macros are disabled (and the column to the left of it is not).
When macros are enabled hide the hyperlink column and show the one to the left of it that will cause the SelectionChange event to run.
(You need to beware how other hyperlinks are used as any cell with a hyperlink next to it will respond to the event. You may need to use intersection to check the cell clicked is in a namedrange that contains "all the cells that need to respond to a user clicking them in the manner".
All the above sounds a bit mad, but it would appear the event model does not facilitate stopping the screen flickering.
I hope there is a better way of doing what you want, but for what it's worth, the above might help.
Harvey