user form X button

cocopops2001

Board Regular
Joined
Apr 18, 2011
Messages
112
i am having a slight problem with my userform. the form allows the user to either select an existing workbook or create a new work book into which a sheet is to be copied.

its probably something really simple but i want the form to run a macro when a button is pushed but when the X button on the form is clicked the 'sheetcopy' macro still runs but i dont want it to. ie it adds sheets to my master workbook instead of a new one, obviously because the new one is not created. i know i need a line to tell it to abort the 'sheetcopy' but not sure what. Im getting on quite well at this but not used to the VBA commands yet.

i did get this button code from a forum and modified it to suit my needs so maybe deleted/added something i shouldnt have. any help would be great, Thanks

also if you think there is a better way to do this then i open to suggestions

Code:
Private Sub CommandButton2_Click()
    
    Workbooks.Add
    
    sheetcopy
    
End Sub

Private Sub UserForm_Click()

End Sub
Private Sub CommandButton1_Click()
        
    OpenCalc
    
    sheetcopy
    
End Sub

Private Sub CommonDialog1_AfterUpdate()

End Sub

Private Sub CommonDialog1_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)

End Sub

Private Sub CommonDialog1_Enter()

End Sub

Private Sub CommonDialog1_Exit(ByVal Cancel As MSForms.ReturnBoolean)

End Sub
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
I do not see anything in the UF code that should do that. Nor does the dialog get dismissed. Maybe show the code that calls the userform?
 
Upvote 0
Code:
Sub form()
    
    ActiveSheet.Range("a11:n70").Copy
    
    UserForm1.Show
End Sub

pretty simple, it copies the info first then opens the form which asks the user where they want to paste the sheet, ie existing file or new excel book
 
Upvote 0
also the opencalc macro

Code:
Sub OpenCalc()

Dim fn
fn = Application.GetOpenFilename(FileFilter:="Excel Files (*.xls),*.xls", _
        Title:="Open File(s)", MultiSelect:=False)
If fn = False Then
MsgBox "Nothing Chosen, Create New Book"
UserForm2.Show
Else
MsgBox "You chose " & fn
Workbooks.Open fn
End If

End Sub
 
Upvote 0
and my second button code
Code:
Private Sub CommandButton1_Click()

    Workbooks.Add
    
    sheetcopy
    
End Sub

Private Sub CommonDialog1_Exit(ByVal Cancel As MSForms.ReturnBoolean)

End Sub

Private Sub CommonDialog1_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)

End Sub

Private Sub CommonDialog1_AfterUpdate()

End Sub

sorry for multiple posts here, im just a noob
 
Upvote 0
Presuming the code shown in post#1 is for Userform1:
Rich (BB code):
Option Explicit
    
Private Sub CommandButton2_Click()
    
    Workbooks.Add
    sheetcopy
End Sub
    
Private Sub CommandButton1_Click()
        
    OpenCalc
    sheetcopy
End Sub
...I am sorry, but I am not seeing it. I think you will find it easier to read if you delete all the currently unused procedures (the empty ones).

Also, in Post#5, you have CommandButton1 again, but with different code. Is that in Userform2?
 
Upvote 0
yes the code shown in post one is for userform1 it works fine apart from when i click X in top right of box it still copies the sheet to my master workbook which i dont want it to do. im very new to VBA and not certain of the commands.

how do you define the X button? all i want is for the process to be aborted when the X is clicked on either form.
 
Upvote 0
fixed this problem, mod can close thread if they wish. i used this code in the userform

Code:
Private Sub UserForm_QueryClose _
  (Cancel As Integer, CloseMode As Integer)
'   Prevents use of the Close button
    If CloseMode = vbFormControlMenu Then
        UserForm1.Show
    End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,527
Messages
6,179,351
Members
452,907
Latest member
Roland Deschain

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