+ Reply to Thread
Results 1 to 9 of 9

Find expression causing "Subscript out of range" error

  1. #1
    Registered User
    Join Date
    06-21-2013
    Location
    Sydney, Australia
    MS-Off Ver
    Excel 2010
    Posts
    3

    Find expression causing "Subscript out of range" error

    Hi everyone,

    I've been trying for hours to get the following macro working, but it keeps giving me a "Subscript out of range" error. The error is at the Find expression, but I've been using the exact format I found from an example online, so I'm not sure what's going on.

    As far as I can tell, it's not the sheet name, since I've tried it using the code name (used below) and the actual name (100% spelled correctly). I've also tried running it with different sheets and it's still the same issue. I also thought it might be related to the variables, but I've declared them according to what was in the example.


    Please Login or Register  to view this content.

    Just as a brief explanation of what I'm trying to achieve:

    On the front page (Sheet2), the user selects a week number (e.g. "30") from a dropdown box in cell B5. They click an "Update" button which runs the macro.

    The macro goes to a different worksheet (Sheet5 in this case) and searches for the week number as a string (e.g., "Week 30") in row 18 (from columns E to BD only). Note that Row 18 uses formulas to display the week numbers, which are called from a database. Then, once it finds the column for the appropriate week (e.g. column AB), it should copy cells E19 to E310 and paste it in that column in the same position (e.g. AB19 to AB310). Cells E19 to E310 contain formulas.

    This process then needs to be completed across 4 other worksheets. The macro then ends.


    This macro will be used on a weekly basis, so any help would be greatly appreciated!

    Thanks,
    aseret

  2. #2
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 2019 on Win10 (desktop), 2019 on Win11 (notebook)
    Posts
    8,197

    Re: Find expression causing "Subscript out of range" error

    Hi, aseret ,

    maybe try it like this:
    Please Login or Register  to view this content.
    Ciao,
    Holger
    Use Code-Tags for showing your code: [code] Your Code here [/code]
    Please mark your question Solved if there has been offered a solution that works fine for you

  3. #3
    Registered User
    Join Date
    06-21-2013
    Location
    Sydney, Australia
    MS-Off Ver
    Excel 2010
    Posts
    3

    Re: Find expression causing "Subscript out of range" error

    Hi Holger,

    Thanks, the code looks simpler and produces no errors. However, it also does not copy the formulas across

    I'm not familiar with Match, but does it look for values, rather than formulas? The weeks are the result of formulas rather than just plain text.

    I'm stumped once again! Any further clarification would be appreciated

    Thanks,
    aseret

  4. #4
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 2019 on Win10 (desktop), 2019 on Win11 (notebook)
    Posts
    8,197

    Re: Find expression causing "Subscript out of range" error

    Hi, aseret,

    Match will deliver the number of the Column if found. So you would not set a range first (which should be evaluated before going on) and get the column from the range but either have it or get an error.

    However, it also does not copy the formulas across
    Iīm afraid it does in the sample I created for testing (I just used the formula "=Row()*Column()" for testing). As I donīt know how your formulas look like I would ask you to either show any formula or maybe attach a sample workbook to have a look at.

    Ciao,
    Holger

  5. #5
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 2019 on Win10 (desktop), 2019 on Win11 (notebook)
    Posts
    8,197

    Re: Find expression causing "Subscript out of range" error

    Hi, aseret,,

    as I just realized maybe we should adjust the range in which to find because the column for match is in the range we looked in or alter the columns we copy to. So either
    Please Login or Register  to view this content.
    or
    Please Login or Register  to view this content.
    should copy over to the correct column.

    Ciao,
    Holger

  6. #6
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Find expression causing "Subscript out of range" error

    Your code works, provide that this line is correct

    Please Login or Register  to view this content.
    Please Login or Register  to view this content.

  7. #7
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 2019 on Win10 (desktop), 2019 on Win11 (notebook)
    Posts
    8,197

    Re: Find expression causing "Subscript out of range" error

    Hi, aseret,

    Do you want to copy over formulas or values?

    In case of values
    Please Login or Register  to view this content.
    Ciao,
    Holger

  8. #8
    Registered User
    Join Date
    06-21-2013
    Location
    Sydney, Australia
    MS-Off Ver
    Excel 2010
    Posts
    3

    Re: Find expression causing "Subscript out of range" error

    Holger, you are a genius! That amendment definitely works. I amended the columns to copy over towards.

    Just two questions:
    1. If I amend the range that is copied to F19:F310 or to G19:G310, etc. does the number I increase var by increase too? i.e. to 5, then 6, etc.
    2. How do I go about giving you some sort of forum pat on the back?

    AB33, thanks so much, at least I know I was on the right track! I'll probably abandon my original code because it wasn't as elegant.

  9. #9
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 2019 on Win10 (desktop), 2019 on Win11 (notebook)
    Posts
    8,197

    Re: Find expression causing "Subscript out of range" error

    Hi, aseret,

    the search range starts with Column E (the fifth column) and will deliver the column number starting with E in the given range. If you consider to change the column you must either adapt that and change 4 (four columns skipped for the begin of the search range) accordingly.

    Iīd prefer in that case to alter the range for the Match to start in Column A as that will always deliver the correct column to copy to no matter from which column the data is to be copied.

    2. How do I go about giving you some sort of forum pat on the back?
    So thatīs where the pain in the back comes from this morning? You may consider the small star (Add Reputation) at the left bottom of the post for that.

    Ciao,
    Holger

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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