Focus on opened Filename before Dialog Box

Drofidnas

Board Regular
Joined
Jun 30, 2004
Messages
125
Hi

I have the following macro which opens an external file.
I want the user to be able to edit it and save it and then in the parent Excel file again be given the choice to re-import those changes.

As it stands, the file opens but I get the dialog up first before focusing on the opened file. How can I force the focus or delay the dialog box?

Thanks for any help

Chris

Sub Open_Nat_Dem_331()

On Error Resume Next

Application.ScreenUpdating = False
Application.DisplayAlerts = False

'Define Source Workbook
DMRLSource = "\Nat Dem 331 01-05-14.xlsx"
SheetSource = "Template"

'Open Source Workbook and correct tab
Workbooks.Open Filename:=DMRLSource
Workbooks(DMRLSetup).Activate
Sheets(SheetSource).Select

Ans = MsgBox("Do you want to re-import the DMRL data for " & DMRLSource & "?", vbQuestion + vbYesNo)

Select Case Ans
  Case vbYes
  Call Data_Import_Nat_Dem_331
  Case vbNo
  Exit Sub
End Select

End Sub
 
Last edited:

Some videos you may like

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.

AlphaFrog

MrExcel MVP
Joined
Sep 2, 2009
Messages
16,278
One possibility; instead of using a MsgBox is to use a UserForm with a Label and two command buttons (Yes and No). The UserForm remains visible and allows the user to edit the worksheet. When done editing, they click Yes or No.

Code:
[COLOR=darkblue]Sub[/COLOR] Open_Nat_Dem_331()

[COLOR=darkblue]On[/COLOR] [COLOR=darkblue]Error[/COLOR] [COLOR=darkblue]Resume[/COLOR] [COLOR=darkblue]Next[/COLOR]

Application.ScreenUpdating = [COLOR=darkblue]False[/COLOR]
Application.DisplayAlerts = [COLOR=darkblue]False[/COLOR]

[COLOR=green]'Define Source Workbook[/COLOR]
DMRLSource = "\Nat Dem 331 01-05-14.xlsx"
SheetSource = "Template"

[COLOR=green]'Open Source Workbook and correct tab[/COLOR]
Workbooks.Open Filename:=DMRLSource
Workbooks(DMRLSetup).Activate
Sheets(SheetSource).Select
 
[COLOR=#ff0000]UserForm1.Show vbModeless[/COLOR]

[COLOR=green]'Ans = MsgBox("Do you want to re-import the DMRL data for " & DMRLSource & "?", vbQuestion + vbYesNo)[/COLOR]
[COLOR=green]'[/COLOR]
[COLOR=green]'Select Case Ans[/COLOR]
[COLOR=green]'  Case vbYes[/COLOR]
[COLOR=green]'  Call Data_Import_Nat_Dem_331[/COLOR]
[COLOR=green]'  Case vbNo[/COLOR]
[COLOR=green]'  Exit Sub[/COLOR]
[COLOR=green]'End Select[/COLOR]

End [COLOR=darkblue]Sub[/COLOR]

UserForm1 code module.
Code:
[COLOR=darkblue]Private[/COLOR] [COLOR=darkblue]Sub[/COLOR] CommandButton1_Click()
    [COLOR=green]'Yes[/COLOR]
    [COLOR=darkblue]Call[/COLOR] Data_Import_Nat_Dem_331
    Unload Me   [COLOR=green]'Exit userform[/COLOR]
[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]Sub[/COLOR]
    
[COLOR=darkblue]Private[/COLOR] [COLOR=darkblue]Sub[/COLOR] CommandButton2_Click()
    [COLOR=green]'No[/COLOR]
    Unload Me   [COLOR=green]'Exit userform[/COLOR]
[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]Sub[/COLOR]
 

Drofidnas

Board Regular
Joined
Jun 30, 2004
Messages
125
Thanks, that sort of works, but the form is visible over the top of the editing.
The dialog was really for the parent worksheet, so when the editing in the opened file had finished, you could choose whether to update the parent file with this new info.
 

Drofidnas

Board Regular
Joined
Jun 30, 2004
Messages
125
It's not.
My idea, but maybe flawed(!) was this:

Parent file runs Macro, dialog opens in it while other file is opened.
Nat Dem 331 file is opened and focused.
Editing happens and file is saved.
Parent file now has a dialog box present.

Clearly the dialog box takes precedent over all open Excel windows and focus is on this at all times.

I'm going to have to rethink.

Thanks for your help.
 

Watch MrExcel Video

Forum statistics

Threads
1,109,437
Messages
5,528,761
Members
409,834
Latest member
vexceled

This Week's Hot Topics

  • Change military grades into rank
    Afternoon all Need help with formula that will change military rank (i.e. 1, 2, 3 into Amn, A1C, SrA). Running IF formula that does not work...
  • VBA COUNTIF SOLUTION
    Hi The following are the errors spread across the several columns from E to Q ie. 13 columns across several sheets with more than 500 rows per...
  • INSERT ROW WITH SPECIFIS TEXT IN A COLUMN
    Hi All! How can identify that that the row to be inserted has to be inserted before 1st row with specific text in column F. If I record the...
  • Auto-Create a monthly Sign in sheet for preschool students
    The image below is what each page looks like. Above is space for the "Child Name" "Month" "Class" School days are obviously Monday-Friday but...
  • VBA vlookup multiple results
    Hi folks, Hopefully someone out there can help. I have a list to vlookup which works (ish). the lookup only picks up the first instance of the...
  • Extract values for earliest/latest times
    I am trying to put together a formula to get the earliest start time, the latest end time from column A for each person in Column B-F without the...
Top