retval = Shell("excel.exe """ & filename & """", vbNormalFocus)

condorelli

New Member
Joined
Jul 27, 2017
Messages
12
Hi all!
Is there any way to choose a sheet in
retval = Shell("excel.exe """ & filename & """", vbNormalFocus)
????
I need to open another excel file with "shell" in a specific sheet.

Thanks
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
There's no command line switch that will activate a worksheet. You have to do one of the following:

-Save the workbook with the desired sheet active. Excel will open it active.

-Put a macro in the workbook to activate the sheet. This would go in the Workbook_Open event handler in the ThisWorkbook module.
 
Upvote 0
Or you could do this :

Code:
retval = Shell("excel.exe """ & filename & """", vbNormalFocus)

GetObject(filename).Worksheets("specific sheet").Activate
 
Upvote 0
Or you could do this :

Code:
retval = Shell("excel.exe """ & filename & """", vbNormalFocus)

GetObject(filename).Worksheets("specific sheet").Activate

Now i have in fileA.xlsm
Code:
Private Sub Workbook_Open()

    Dim strTestString
    strTestString = Left(ThisWorkbook.Name, (InStrRev(ThisWorkbook.Name, ".", -1, vbTextCompare) - 1))
    Dim retval As String
    Dim filename As Variant
    filename = "\\xxx\yyy\[B]fileB[/B].xlsm"
    retval = Shell("excel.exe """ & filename & """", vbNormalFocus)
    GetObject(filename).Worksheets(strTestString).Activate

End Sub

It doesn't show any error but the sheet activated is the saved one and not the "specific sheet".
 
Upvote 0
Try running a small loop to wait until the workbook is loaded in the shelled application ... Something along these lines :
Code:
retval = Shell("excel.exe """ & filename & """", vbNormalFocus)

Do
    DoEvents
Loop Until Not GetObject(filename) Is Nothing

GetObject(filename).Worksheets("specific sheet").Activate
 
Last edited:
Upvote 0
Try running a small loop to wait until the workbook is loaded in the shelled application ... Something along these lines :
Code:
retval = Shell("excel.exe """ & filename & """", vbNormalFocus)

Do
    DoEvents
Loop Until Not GetObject(filename) Is Nothing

GetObject(filename).Worksheets("specific sheet").Activate

Same as above...No errors but the sheet activated is the saved one
 
Upvote 0
Try this and see what you get on the Debug.print line :
Code:
retval = Shell("excel.exe """ & filename & """", vbNormalFocus)

Do
    DoEvents
Loop Until Not GetObject(filename) Is Nothing

Debug.Print GetObject(filename).Sheets(strTestString).Name

GetObject(filename).Sheets(strTestString).Select
 
Upvote 0
Try this and see what you get on the Debug.print line :
Code:
retval = Shell("excel.exe """ & filename & """", vbNormalFocus)

Do
    DoEvents
Loop Until Not GetObject(filename) Is Nothing

Debug.Print GetObject(filename).Sheets(strTestString).Name

GetObject(filename).Sheets(strTestString).Select

run-time '1004' error
 
Upvote 0

Forum statistics

Threads
1,216,730
Messages
6,132,398
Members
449,725
Latest member
Enero1

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