+ Reply to Thread
Results 1 to 6 of 6

VBA to say use sheet B if sheet A doesn't exist

  1. #1
    Registered User
    Join Date
    07-05-2017
    Location
    chicago
    MS-Off Ver
    Office 2013
    Posts
    44

    VBA to say use sheet B if sheet A doesn't exist

    Hello,

    I have a macro issue that another person who left my company wrote. In the current macro, data is being pulled from a tab called "Summary Sheet"

    However, in the set of files (this is pulling from) the sheet has been renamed to "Report"


    I do not want to rename several hundred tabs in different workbooks, so how would I tell the VBA code to pull the data from "Report" if it cannot locate tab "Summary Sheet" ?

    Here is the code that references "Summary Sheet"


    wbk2.Activate
    Sheets("Report").Activate
    Range("d30").Select
    Application.CutCopyMode = False
    Selection.Copy

  2. #2
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    MS365 Apps for enterprise
    Posts
    5,913

    Re: VBA to say use sheet B if sheet A doesn't exist

    Use IsObject(wbk2.Sheets("Summary Sheet")) to check if the sheet exists, if it does use "Summary Sheet" if not, use "Report" sheet.
    ?Progress isn't made by early risers. It's made by lazy men trying to find easier ways to do something.?
    ― Robert A. Heinlein

  3. #3
    Registered User
    Join Date
    07-05-2017
    Location
    chicago
    MS-Off Ver
    Office 2013
    Posts
    44

    Re: VBA to say use sheet B if sheet A doesn't exist

    thank you, how do I write this in code:

    "if it does use "Summary Sheet" if not, use "Report" sheet."

  4. #4
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    MS365 Apps for enterprise
    Posts
    5,913

    Re: VBA to say use sheet B if sheet A doesn't exist

    Please Login or Register  to view this content.
    Or declare worksheet object variable and set that variable... I prefer this method as it can reduce code line.
    Please Login or Register  to view this content.

  5. #5
    Registered User
    Join Date
    07-05-2017
    Location
    chicago
    MS-Off Ver
    Office 2013
    Posts
    44

    Re: VBA to say use sheet B if sheet A doesn't exist

    sorry that still isn't working:

    Dim ws As Worksheet

    If IsObject(wbk2.Sheets("Summary Sheet")) Then
    Set ws = wbk2.Sheets("Summary Sheet")
    Else
    Set ws = wbk2.Sheets("Report")
    End If



    wbk2.Activate
    Sheets("Report").Activate
    Range("d30").Select
    Application.CutCopyMode = False
    Selection.Copy


    the error is on the sheets ("report"). Activate line

  6. #6
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    MS365 Apps for enterprise
    Posts
    5,913

    Re: VBA to say use sheet B if sheet A doesn't exist

    .... so ws is set in if statement.

    You'd just use ws.Activate instead of Sheets("Report").Activate.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Referencing a sheet that doesn't exist yet
    By jameskey in forum Excel General
    Replies: 1
    Last Post: 06-07-2012, 11:51 AM
  2. How do I reference a sheet that doesn't exist yet?
    By vin200 in forum Excel General
    Replies: 2
    Last Post: 01-11-2012, 03:38 AM
  3. Create new directory if it doesn't exist and save sheet
    By rkjudy in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-01-2011, 01:47 PM
  4. Referencing a sheet that doesn't exist - yet
    By kosciosco in forum Excel General
    Replies: 1
    Last Post: 12-13-2007, 03:22 PM
  5. Create a new Sheet if it doesn't exist?
    By nbaj2k in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 10-28-2006, 06:49 PM
  6. [SOLVED] VBA: Make a new sheet if it doesn't exist
    By PaulW in forum Excel General
    Replies: 2
    Last Post: 05-08-2006, 10:50 AM
  7. VBA, Make a new sheet if it doesn't exist
    By PaulW in forum Excel General
    Replies: 1
    Last Post: 05-05-2006, 12:30 PM
  8. error when sheet doesn't exist...
    By Ctech in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 11-11-2005, 09:15 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1