Welcome to the Excel Forum

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed.

Please Register to Remove these Ads

Please Register to Remove these Ads



Reply
  #1  
Old 03-11-2005, 06:19 PM
VBA newbie VBA newbie is offline
Registered User
 
Join Date: 26 Feb 2005
Posts: 3
VBA newbie is becoming part of the community
Refering to close or cancel buttons on xlDialogOpen

Please Register to Remove these Ads

Hi,

I have the following code that allows a user to click a command button in my current workbook and then choose a file to open and then copy some data from into the current workbook. This works fine, but if the user doesn't select a file and presses either the close or cancel button instead then the code still runs and this causes a couple of problems. I want to use an if then statement but am unsure how to refer to the close or cancel buttons on the open dialog box in the code? Can anyone help?


Private Sub Cmd_file_select_Click()
Dim oldfile As String, newfile As String, x As String

oldfile = ActiveWindow.Caption

Application.Dialogs.Item(xlDialogOpen).Show

newfile = ActiveWindow.Caption

x = MsgBox("Have you opened the correct file, and is the data to be imported in columns A to C with the word END in the last cell of the firm name column? If so select Ok to import the file, or Cancel to return to Investor Solutions without importing", vbOKCancel)

Select Case x

Case vbOK
Windows(oldfile).Activate
Macro_undo_copy_original
Sheet3.Cmd_undo.Enabled = True

Sheet3.Activate
Range("A1").Select
Sheet3.Unprotect ("Code")
Columns("A:C").Select
Selection.Clear
Range("A1").Select

Windows(newfile).Activate
Columns("A:C").Select
Selection.Copy

Windows(oldfile).Activate
Range("A1").Select
ActiveSheet.Paste
Range("A1").Select
Sheet3.Cmd_update.Enabled = True
Macro_validation
Sheet3.Protect ("Code")

Windows(newfile).Close

Case vbCancel
Windows(oldfile).Activate
Windows(newfile).Close

End Select

End Sub
Reply With Quote
  #2  
Old 03-11-2005, 07:10 PM
crispbd crispbd is offline
Forum Contributor
 
Join Date: 21 Jun 2004
Location: Phoenix, Az
Posts: 30
crispbd is becoming part of the community
Lightbulb

If no file is returned (hits the close or cancel buttons)
the function returns a string: "False"


instead of:
Application.Dialogs.Item(xlDialogOpen).Show

try:

UserResponse = Application.Dialogs.Item(xlDialogOpen).Show

If UserResponse = "False" then Exit Sub

FileName = ActiveWindow.Caption




You might also check in to using a different open dialog such as:

FileName = Application.GetOpenFileName (....)

that way you can return the file full path more easily

Last edited by crispbd; 03-11-2005 at 07:14 PM.
Reply With Quote


Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Forum Jump