How to tell a UserForm button has been clicked (XL365)?

EdNerd

Active Member
Joined
May 19, 2011
Messages
456
Several years ago, I designed an macro-laden Excel file for my work section. I then created a "toolkit" workbook with multiple macros to enable the users to fix these files, rather than piling them up in my lap. %-/

The macro I'm currently working on requires the user to close all Excel files (except the toolkit) before the macro continues. Unfortunately, I can't guarantee that will be done prior to initiating the macro. So I thought to open a UserForm with instructions to close all other workbooks, then click a button on the form when this is accomplished. I'm using a form because that would allow the users to interact with the other XL files, whereas a Message Box would not. I also need to include a Cancel button in case they are not ready to close a certain file.

My problem: I am drawing a blank on how to detect when and which form button is clicked to allow the rest of the macro to continue to run. Everything I can think of seems very convoluted -- which tells me I've got the wrong answer! If someone could drop-kick me in the right direction, I would be very grateful.
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
Each button on a UserForm has a series of events that code can detect, including the click event. For a button called CommandButton1 you can write a Sub called

VBA Code:
Private Sub CommandButton1_Click()

that will be called automatically when the button is clicked. Implement this for each button with the desired code.
 
Upvote 0
Thank you for responding. I know about the Click events, but I need to keep the actual code inside the workbook. There are several sub-routines that are already there that I will need to call in the course of running this macro.

What I'm looking for is an easy way to use the Click event to communicate back to the workbook-based code that called the user form, and tell that code which way to proceed??
 
Upvote 0
I think I see what you are getting at.

Typically this is done with a public variable at the module level of the UserForm code:

VBA Code:
Public Result As Long
Public Button1Click As Long

Private Function UserForm_Initialize()
   Button1Click = 1
End Sub

Private Function Button1_Click()
   Result = Button1Click
End If

And used like this:

VBA Code:
UserForm1.Show
If Result = UserForm1.Button1Click Then
    ' Do something here
 
Upvote 0
Trying to work this out, but coming up with a weird response. (Or maybe it's just been so long since I've worked with Forms that I forgot this "feature"!)

I open the form using .Show vbModeless - that should put the form on top and allow me to interact with other workbooks -- yes??
But when I close a different workbook (not the one with the user form), the original wkb and the form close - but the second file (the one I wanted to close) stays open??

Head scratching .....
 
Upvote 0
No way to diagnose without at least having all your code. Entire file would be better.

BTW my solution was for a modal form. If you Show a modal form, the code does not continue execution until the form is closed. That when my code checks the button status. If you Show a modeless form, the code keeps executing. You would need a completely different solution to detect what button was pressed. I would need to understand more about the code that needs to determine this
 
Upvote 0
Sorry for the delay in replying. Had to take an emergency day off yesterday.
I decided (mostly due to my inexperience in dealing with forms and passing info) to go another route. Using the form got too confusing, when all I really wanted to do was give the user an opportunity to say "No, I'm not ready to have all my other files closed!" A MsgBox was much easier! 😱

Thank you for trying to help me out. My bad for not being more up to speed.
 
Upvote 0

Forum statistics

Threads
1,214,583
Messages
6,120,383
Members
448,956
Latest member
JPav

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