Close Code In vba

mattless1

Board Regular
Joined
Apr 1, 2011
Messages
102
Can someone help please??

I have a userform, which has a listbox and a transfer data button to a cell inside my workbook.
I want to have a button to close the userform after an item has been selected and transferred, but it must not close the userform until a item has been transferred to the workbook. this is to stop staff bypassing the userform.

Many thanks
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
You have an existing TransferData button, you want code for a Close button. Try
Code:
[code]Private Sub butClose_Click()
    Call butTransferData_Click
    Unload Me
End Sub

You may have to write a routine for the QueryClose event to insure that data has been transferred before the corner X is clicked.


Code:
Dim dataTransfered as Boolean

Private Sub butTransferData_Click()
    Rem some Code

    dataTransfered = True
End Sub
'...
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
    If Not(dataTransfered) Then
        MsgBox "Transfer data please"
        Cancel = True
    End If
End Sub
 
Upvote 0
Thanks i will try that. i have changed it a bit now i have a list of 10 shops i need the user to click one of the shops and when they click the Quit button it transfers the selected Shop to cell D3 & then closes the userform. but if they do not pick a shop it will ask them to pick one before it closes.

Do you think this can be done?
 
Upvote 0
I'm guessing that the shops are represented by Option Buttons rather than by CheckBoxes.
In each OptionButton change event, set a global Boolean variable (like dataTransfered above) and then test that variable before closing.
 
Upvote 0

Forum statistics

Threads
1,225,656
Messages
6,186,245
Members
453,343
Latest member
hacigultekin

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