473,806 Members | 2,582 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Excel VBA Problem : UserForm1.show issue (shows up twice)

1 New Member
hi all,

i've created an Excel VBA script that shows UserForm1 when clicking on close (the "X" at the top right) without saving. the form simply prompts the user whether to save the file or not (with few more options).
the problem that occurs is: after clicking the submit button, the appears again and only after the second time i pressed the submit button it disappears.
in short: the form appears twice instead of just once (like i want).
my code:

Private Sub Workbook_Before Close(Cancel As Boolean)
If ThisWorkbook.Sa ved = False Then
If ThisWorkbook.Fi leFormat = xlTemplate Then
UserForm1.Show
End If
End If
End Sub



' The submit code:


Private Sub cmdSubmit_Click ()
If UserForm1.NoSav ePrint = True Then
UserForm1.Hide
ThisWorkbook.Cl ose False
End If
If UserForm1.NoSav ePrint = False Then
UserForm1.Hide
Application.Dis playAlerts = False
Application.Scr eenUpdating = False
ThisWorkbook.Sa ve
ActiveWorkbook. SaveAs Filename:="\\sb s2003\OrdersTnu a\JetBackup\Ord erNo_" & Range("E8"), FileFormat:=xlN ormal, Password:="jet" , WriteResPasswor d:="", ReadOnlyRecomme nded:=False, CreateBackup:=F alse
Application.Dis playAlerts = True
Application.Scr eenUpdating = True
Call ThisWorkbook.Pr int2
ThisWorkbook.Cl ose
End If
End Sub



i tried and tried and could not find out where's the error. everything is great except the fact that the form repeats twice.

will be grateful for any ideas.
thanks
Dec 26 '07 #1
3 4922
daniel aristidou
491 Contributor
hi all,


Expand|Select|Wrap|Line Numbers
  1. Private Sub Workbook_BeforeClose(Cancel As Boolean)
  2. If ThisWorkbook.Saved = False Then
  3.    If ThisWorkbook.FileFormat = xlTemplate Then
  4.       UserForm1.Show
  5.    End If
  6. End If
  7. End Sub
' The submit code:


[
Expand|Select|Wrap|Line Numbers
  1. I]Private Sub cmdSubmit_Click()
  2. If UserForm1.NoSavePrint = True Then
  3.    UserForm1.Hide
  4.    ThisWorkbook.Close False
  5. End If
  6. If UserForm1.NoSavePrint = False Then
  7.    UserForm1.Hide
  8.    Application.DisplayAlerts = False
  9.    Application.ScreenUpdating = False
  10.    ThisWorkbook.Save
  11.    ActiveWorkbook.SaveAs Filename:="\\sbs2003\OrdersTnua\JetBackup\OrderNo_" & Range("E8"), FileFormat:=xlNormal, Password:="jet", WriteResPassword:="", ReadOnlyRecommended:=False, CreateBackup:=False
  12.     Application.DisplayAlerts = True
  13.     Application.ScreenUpdating = True
  14.     Call ThisWorkbook.Print2
  15.     ThisWorkbook.Close
  16. End If
  17. End Sub[/i]
i
Can you please submit comments into your code explaining what each line does. This will make it allot easier to solve your problem.
Reading comments help anormously when dealing with this type of problem

Daniel(~_~)
Jan 3 '08 #2
daniel aristidou
491 Contributor
instead of this code
Expand|Select|Wrap|Line Numbers
  1. Private Sub Workbook_BeforeClose(Cancel As Boolean)
  2. If ThisWorkbook.Saved = False Then
  3.    If ThisWorkbook.FileFormat = xlTemplate Then
  4.       UserForm1.Show
  5.    End If
  6. End If
  7. End Sub

Why not use
Expand|Select|Wrap|Line Numbers
  1. Private Sub Workbook_BeforeClose(Cancel As Boolean)
  2. If ThisWorkbook.Saved = False And ThisWorkbook.FileFormat = xlTemplate Then
  3.          UserForm1.Show
  4.  
  5. End If
  6. End Sub
Jan 3 '08 #3
Killer42
8,435 Recognized Expert Expert
Subscribing (please ignore this post).
Jan 4 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

6
12510
by: Matthew Wieder | last post by:
I have the following requirements: Build a stand-alone C# application that asks the user to click in a cell in an Excel spreadsheet, and then displays the address of that cell in the C# application. It seems simple enough, but the problem I'm encountering is as follows: In order for the user to select the cell from Excel, they must first click once on the Excel window to give it focus and then their second click is what changes the cell...
4
2228
by: Vishal | last post by:
Hello, I have a dataset with some content. I can convert this dataset in a csv format which is then stored in a stringwriter. Now I want this be open via excel, so I set the contenttype "application/vnd.ms-excel". To access this page the user requires to login, once the user is logged in he can access this file. Now when this file is opened in excel it shows me the login screen instead of the data. The user is already logged in, so why...
1
3174
by: Mustufa Baig | last post by:
I have an ASP.NET website where I am showing off crystal reports to users by exporting them to pdf format. Following is the code: ---------------- 1 Private Sub ExportReport() 2 Dim oStream As System.IO.MemoryStream = 3 myReport.ExportToStream( ExportFormatType.PortableDocFormat) 4 Response.Clear() 5 Response.Buffer() = True
6
1615
by: Yanto | last post by:
hi Can somebody help me with this issue I'm exporting my data from Analysis Service to excel files using ADOMD. I got hundreds of excel files that need to be inserted with cube data retrieved from ADOMD connection. Each of the files would have a pre-defined location where i should start inserting data, for example "R3C4"(each file would be in different cell) I need to open the existing file, adds data to it, save changes, and closes I've...
8
38170
by: SteveS | last post by:
I'm attempting to close EXCEL from within my VB.NET application. Using the excel object library to write data to my spreadsheet is working fine but when I try to quit application object it does not work. I know this because I can still see the Excel application running in Task Manager. How do I shut down EXCEL? -- Thank You
13
13259
by: Hemant Sipahimalani | last post by:
The following piece of code is being used to export HTML to excel. HttpContext.Current.Response.ContentType = "application/vnd.ms-excel" HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=ABC.xls") HttpContext.Current.Response.Write(strHTML) HttpContext.Current.Response.End() However when the user tries to save it the Default File Type is Web Page(*.htm; *.html)
1
5743
by: garry.oxnard | last post by:
Can anyone help me to solve a problem which involves switching from Access to Excel (then back to Access) programatically please? I have an Excel template which, on open, also opens an Access database - containing a list of addresses. Sequence of events is = (1) Excel template opens in its default XXX.xls filename. (2) Code runs to save the spreadsheet as XXX.xls. (3) User clicks a button to open an Access database containing an
3
4022
by: dan_roman | last post by:
Hi, I developed a script with a nice interface in Tkinter that allows me to edit some formulas and to generate an Excel worksheet with VBA macros within it. The script runs perfectlly in Office 2000, but in Office 2003 crash at line: "wbc = workbook.VBProject.VBComponents.Add(1)" Please help me :-( the code of the module that crash is (only in Excel 2003, in 2000 not): import os
4
14025
by: ronakinuk | last post by:
hello i need help in this please. i dont have much knowledge of VBA but can do very basic things by learning from different forums. i want to create one excel application where there will be few userforms. and on each there will be some text boxes. i want it to work like this. if user puts number 2 in textbox1 on userform1 and then PRESS ENTER, unload userform1 and show userform2, but if user puts 3 in textbox1 on userform1 and PRESS...
0
9719
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9597
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10620
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10372
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7650
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5546
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5682
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4329
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3008
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.