How do I return to a particular worksheet

azizrasul

Well-known Member
Joined
Jul 7, 2003
Messages
1,304
Office Version
  1. 365
  2. 2019
  3. 2016
Platform
  1. Windows
I know this is an easy question and I hang my head in shame but I just can't get the answer.

I want to return to the first worksheet (Cover) in the following code.

Code:
    Set objExcelApp = CreateObject("Excel.Application")
    Set objWorkBook = objExcelApp.Workbooks.Open(strFilePathAMCorpQueries)
    
    objExcelApp.Visible = True

    For Each objWorkSheet In objWorkBook.Worksheets
        If objWorkSheet.Name = "Summary" Then
            objWorkSheet.Select
            objWorkSheet.Range("K14").Select
            objExcelApp.ActiveCell.FormulaR1C1 = [Forms]![Form1]![txtQuery_Date]
            objWorkSheet.Range("A1").Select
            objWorkSheet("Cover").Select         'Need this line to work
        End If
    Next objWorkSheet
     
    With objExcelApp
        .DisplayAlerts = False
        .ActiveWorkbook.SaveAs strFilePathAMCorpQueries
        .DisplayAlerts = True
        .Quit
    End With
    
    Set objWorkBook = Nothing

Thanks in advance.
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
If you didn't select other sheets you wouldn't need to do this.

Since your code doesn't need to select anything you could just remove the line of code that's selecting each sheet as you loop through them.

If you do want to move focus to a specific worksheet you can use this.
Code:
objExcelApp.Goto objWorkbook-Worksheets("Cover").Range("A1"), True
 
Upvote 0
aziz

It's actually a very good idea to avoid selecting because it really isn't needed and can cause problems - even in Excel VBA.
 
Upvote 0
OK. How would I alter my original code in that case?
 
Upvote 0

Forum statistics

Threads
1,224,508
Messages
6,179,188
Members
452,893
Latest member
denay

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