+ Reply to Thread
Results 1 to 12 of 12

worksheets function not working as expected

  1. #1
    Registered User
    Join Date
    10-03-2015
    Location
    west coast is the best coast
    MS-Off Ver
    excel 2010
    Posts
    113

    worksheets function not working as expected

    Hello. I've used the code below and it gives me an error sometimes. If I put the value at 1 it doesn't work, but if I put it at 3 it runs through. If I put it at 1 it says select method of range class failed. Isn't this supposed to select the 1st sheet if the value in h is 1?


    Sub tst()
    h = 3
    Worksheets(h).Range("A1").Select


    End Sub

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

    Re: worksheets function not working as expected

    Hi elmasguapo,

    You can't select a cell apart from the active sheet (i.e. the sheet you're on). This is how you'd have to do it:

    Please Login or Register  to view this content.
    Note I've wrapped my code in the appropriate tags as you need to

    Regards,

    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

  3. #3
    Registered User
    Join Date
    10-03-2015
    Location
    west coast is the best coast
    MS-Off Ver
    excel 2010
    Posts
    113

    Re: worksheets function not working as expected

    @Trebor. Thanks, I didn't know that. I thought that the worksheet(variable) would automatically reference the worksheet and select the range.

    What would I do if I had multiple workbooks in the same statement? So, h= 3 (intWsIndex) and i = 2 (or intWsIndex 2)?

  4. #4
    Registered User
    Join Date
    10-03-2015
    Location
    west coast is the best coast
    MS-Off Ver
    excel 2010
    Posts
    113

    Re: worksheets function not working as expected

    Also, what if you aren't selecting the range but just referencing it. Do you still need the with? I tested it and it doesn't look it.
    Last edited by elmasguapo; 10-14-2015 at 06:30 PM.

  5. #5
    Registered User
    Join Date
    10-03-2015
    Location
    west coast is the best coast
    MS-Off Ver
    excel 2010
    Posts
    113

    Re: worksheets function not working as expected

    This is some code that I'm trying to piece together but it's not working and I'm not sure why. It seems like it should work since it's not actively selecting the sheets but rather just referencing them.

    Please Login or Register  to view this content.

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

    Re: worksheets function not working as expected

    What are you trying to do? You may need to attach a workbook (desensitised) with before and after scenarios so I can try and provide a solution.

  7. #7
    Registered User
    Join Date
    10-03-2015
    Location
    west coast is the best coast
    MS-Off Ver
    excel 2010
    Posts
    113

    Re: worksheets function not working as expected

    I think it's pretty straightforward. H and I evaluate to 1 and 2. On the next loop they evaluate to 4 and 5, then 7 and 8, etc. I'd like to use them as variables to reference sheets in the consolidate range function but it's not working. Thanks for your help.

    Please Login or Register  to view this content.

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

    Re: worksheets function not working as expected

    I think it's pretty straightforward.
    Not for me as I'm having trouble understanding your code.

    H and I evaluate to 1 and 2
    No, h is 2 and i is 1 etc

    "Sheets(h)!R1C1:R3C3"
    This isn't referencing the a sheet by its position in the workbook but a tab called "Sheet(2)" (in the first loop). Is that correct?

  9. #9
    Registered User
    Join Date
    10-03-2015
    Location
    west coast is the best coast
    MS-Off Ver
    excel 2010
    Posts
    113

    Re: worksheets function not working as expected

    Yeah, sorry, I got the order of H/I switched. AH!!! I thought it was referencing the sheet order. How can I get it to order the 1st/2nd sheet from the left (as opposed to the sheet name)? Thanks!!

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

    Re: worksheets function not working as expected

    Is this what you're after:

    Please Login or Register  to view this content.
    Regards,

    Robert

  11. #11
    Registered User
    Join Date
    10-03-2015
    Location
    west coast is the best coast
    MS-Off Ver
    excel 2010
    Posts
    113

    Re: worksheets function not working as expected

    @ Trebor. Thank you!! Perfect!! A couple of questions: What's the difference between usings sheets/worksheets in this case? Why do I need to add the ".name"? And lastly, why do I need to place the !R1C1 i n""? Muchas gracias.

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

    Re: worksheets function not working as expected

    I have a question for you actually - why have you changed your name from ammartino44??

+ 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] IF/ELSEIF not working as expected
    By amphinomos in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-12-2015, 10:00 AM
  2. [SOLVED] CountIfS not working as expected
    By rschoenb in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 11-12-2014, 11:45 PM
  3. Networkdays function not working as expected
    By Rem0ram in forum Excel Formulas & Functions
    Replies: 20
    Last Post: 07-02-2014, 09:29 AM
  4. Replies: 6
    Last Post: 05-07-2012, 10:49 AM
  5. ISNA not working as expected
    By micco in forum Excel General
    Replies: 2
    Last Post: 06-30-2011, 01:54 PM
  6. Lookup Not working as expected
    By trumpy81 in forum Excel - New Users/Basics
    Replies: 5
    Last Post: 06-30-2005, 03:05 PM
  7. Hyperlink Function not working as expected
    By Hari Prasadh in forum Excel General
    Replies: 2
    Last Post: 04-16-2005, 09:08 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