+ Reply to Thread
Results 1 to 8 of 8

assign sheet to variable by code name in another workbook

  1. #1
    Registered User
    Join Date
    10-09-2012
    Location
    Los Angeles
    MS-Off Ver
    Excel 2010
    Posts
    13

    assign sheet to variable by code name in another workbook

    I have been unable to get this to work. It errors out telling me I have not defined the variable, which is actually the sheet code name. The workbook sets fine. It's the sheet's code name.

    I have tried:

    Please Login or Register  to view this content.
    and I've tried:

    Please Login or Register  to view this content.
    I appreciate your time and help.

    Thanks

  2. #2
    Forum Expert
    Join Date
    01-23-2013
    Location
    USA
    MS-Off Ver
    Microsoft 365 aka Office 365
    Posts
    3,863

    Re: assign sheet to variable by code name in another workbook

    Hi karen53,

    Try one of the following (both tested and working). You will get a runtime error if a Workbook Name or Worksheet name is spelled incorrectly and/or doesn't exist:
    Please Login or Register  to view this content.
    There are several additional ways to do the same thing.

    To prevent typos from ruining days and weeks of work 'Option Explicit' is NEEDED at the top of each code module. This prevents errors caused by missspellings and FORCES every variable to be DECLARED (e.g. dim i as Integer). http://www.cpearson.com/excel/DeclaringVariables.aspx

    Lewis

  3. #3
    Registered User
    Join Date
    10-09-2012
    Location
    Los Angeles
    MS-Off Ver
    Excel 2010
    Posts
    13

    Re: assign sheet to variable by code name in another workbook

    HI LJMetzger,

    Thank you for your response.

    Yes, I have Option Explicit at the top of all of my modules.

    Setting the workbooks is not a problem. The workbooks set fine.

    Set FromWs = fromwb.activesheet will not work either as it is not the activesheet, nor is it even in the current workbook.

    I have tried activating the workbook before I set the worksheet but I still get the same error.

    How do I address the sheet code name located in a different workbook to set the worksheet in the current workbook?

    Thanks,

  4. #4
    Forum Expert
    Join Date
    01-23-2013
    Location
    USA
    MS-Off Ver
    Microsoft 365 aka Office 365
    Posts
    3,863

    Re: assign sheet to variable by code name in another workbook

    It' good that you're using 'Option Explicit'.

    Try single stepping through the following Macro and observing how 'Book2.xls' changes.
    Please Login or Register  to view this content.
    If this doesn't point you in the correct direction, you're going to give us some details about what you are trying to do. For example, putting a copy of Sheet1 in the Source Workbook in the Destination Workbook, or copying values only, or copying formulas only, or copying formats only.

    Lewis

  5. #5
    Registered User
    Join Date
    08-27-2013
    Location
    Texas
    MS-Off Ver
    Excel 2010
    Posts
    30

    Re: assign sheet to variable by code name in another workbook

    Somewhere in your code I hope there is something that assigns a workbook name to FromWbk
    Like set FromWbk = Workbook("C:\stuff.xlms")

    If you only plan to have this one workbook open you should be able to get away with
    Set FromWs = shtrpt


    I also relooked at your response that the workbook set was not a problem:
    I'm pretty sure the workbook you are referring to has to be open.
    Last edited by briguin; 10-17-2014 at 05:05 PM.

  6. #6
    Forum Expert
    Join Date
    12-10-2006
    Location
    Sydney
    MS-Off Ver
    Office 365
    Posts
    3,565

    Re: assign sheet to variable by code name in another workbook

    Hi Karen53,

    You can only codenames from the active workbook.

    Have a look at this thread and this thread about this, and see if the UDF romperstomper suggests in the second thread helps.

    Robert
    ____________________________________________
    Please ensure you mark your thread as Solved once it is. Click here to see how
    If this post helps, please don't forget to say thanks by clicking the star icon in the bottom left-hand corner of my post

  7. #7
    Registered User
    Join Date
    10-09-2012
    Location
    Los Angeles
    MS-Off Ver
    Excel 2010
    Posts
    13

    Smile Re: assign sheet to variable by code name in another workbook

    Thank you Trebor! I was going crazy!

  8. #8
    Forum Expert
    Join Date
    12-10-2006
    Location
    Sydney
    MS-Off Ver
    Office 365
    Posts
    3,565

    Re: assign sheet to variable by code name in another workbook

    Thank you Trebor! I was going crazy!
    You're welcome and thanks for marking the thread as solved (and the reputation)

+ 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. [SOLVED] Assign entire row to variable, how do you paste variable values in a new workbook?
    By tv69 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 12-18-2013, 08:01 PM
  2. assign variable name to the workbook
    By yjmmay34 in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 08-17-2010, 02:18 AM
  3. code to assign value from a file to variable
    By kichu in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 10-22-2009, 11:08 AM
  4. Assign the name of the workbook to a variable?
    By syntex in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-04-2008, 07:26 AM
  5. Variable VB to assign new sheet name
    By Kevin M in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 09-06-2005, 02:05 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