+ Reply to Thread
Results 1 to 8 of 8

RunTime Error 9 when using "With(strName)" to find sheet "strName"

  1. #1
    Valued Forum Contributor
    Join Date
    01-16-2012
    Location
    England
    MS-Off Ver
    MS 365
    Posts
    1,397

    RunTime Error 9 when using "With(strName)" to find sheet "strName"

    Following code should find the worksheet matching the name in Col AA, find whatever value is in the last cell of Col D in that sheet, and put it in Col AF of the Active sheet

    Tried the following logic:

    Set the number of sheets to be checked as the number of entries in Col AA
    Set the name in AA (row) as a string, and call the sheet with that name
    Find the last row in Col D of that sheet
    Set value in row AF in the original sheet as that Col D value
    Repeat cycle for other rows in AA

    Code recognises "strName" as the name in Cell AA (whatever) but then generates a "Runtime Error 9 - Subscript Out Of Range" at "With Sheets(strName)"


    Please Login or Register  to view this content.
    All suggestions, solutions and advice welcome as ever

    Ochimus
    Last edited by Ochimus; 05-28-2016 at 09:20 AM.

  2. #2
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,446

    Re: RunTime Error 9 when using "With(strName)" to find sheet "strName"

    Please post a sample workbook with some typical data.
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  3. #3
    Valued Forum Contributor
    Join Date
    01-16-2012
    Location
    England
    MS-Off Ver
    MS 365
    Posts
    1,397

    Re: RunTime Error 9 when using "With(strName)" to find sheet "strName"

    TMS,

    Thanks for prompt interest.

    I've shifted the Sheet1 data to Cols A and B (saves everyone trekking pointlessly to Col AA - AD)

    Sample has three sheets each showing just the "total" in Col D, each of which are on different rows.

    Cols F and G in Sheet1 show what the result should be - they show the fomulae to clarify what I want the Code to do, but obviously the Code will paste the actual hard numbers.

    Please Login or Register  to view this content.
    Hope that helps

    Ochimus

    EDITED: Sorry, please amend Macro on attachment. I left it posting the value into Col A, not Col B, which I have corrected on this Code extract.
    Attached Files Attached Files
    Last edited by Ochimus; 05-28-2016 at 12:18 PM.

  4. #4
    Forum Contributor
    Join Date
    01-03-2013
    Location
    Aberdeen, Scotland
    MS-Off Ver
    Excel 2007
    Posts
    163

    Re: RunTime Error 9 when using "With(strName)" to find sheet "strName"

    Hi
    What i noticed your code is trying to use as a worksheet name an empty string (for x=5 strName="").
    Assuming, that you want to have an empty row between worksheets names and row with CHECKSUM, I suggest to add a code line to check if strName is not empty.
    Maybe something like this

    Please Login or Register  to view this content.
    Hope this help you a bit...

  5. #5
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,446

    Re: RunTime Error 9 when using "With(strName)" to find sheet "strName"

    Do it this way:

    Please Login or Register  to view this content.

  6. #6
    Valued Forum Contributor
    Join Date
    01-16-2012
    Location
    England
    MS-Off Ver
    MS 365
    Posts
    1,397

    Re: RunTime Error 9 when using "With(strName)" to find sheet "strName"

    TMS,

    Many thanks for sorting this out, but I'm baffled.

    How did changing the means of identifying "c" overcome the problem of linking to the file? Because the original code found "c" and the file name quite happily, but them fell over!

    Ochimus

  7. #7
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,446

    Re: RunTime Error 9 when using "With(strName)" to find sheet "strName"

    You find the last row using xlUP which gives the Total row. So, when you loop through the cells, you get to the blank entry before the Total. And that's what fails; you don't have a worksheet with a blank worksheet name.

    By using xlDown, it just goes down to the last data entry. And doesn't try to process the blank cell.

    You could, of course, use xlUP - 2

  8. #8
    Valued Forum Contributor
    Join Date
    01-16-2012
    Location
    England
    MS-Off Ver
    MS 365
    Posts
    1,397

    Re: RunTime Error 9 when using "With(strName)" to find sheet "strName"

    TMS,

    Slight delay in Closing because two of the instances in the "real" file still fell over using your "Down not Up" approach, which was bizarre,

    For some reason, they only worked if I entered "ActiveSheet.Cells.SpecialCells(xlCellTypeLastCell).Row".

    As an aside, I'm wondering if I ought to use a "fuller" file path in future, and stipulate "ThisWorkbook.Worksheets!Sheet(whatever).Cells. . . ."

    Fortunately, I've not had an End User activate another workbook before or during my code execution up to now - but there's bound to be a first time!

    I'll mark this as "solved", but value your thoughts on that?

    Ochimus

+ 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. Replies: 35
    Last Post: 01-13-2016, 02:16 AM
  2. = EMBED("Forms.ComboBox.1","") error "Reference not valid."
    By CatharinaCatharina in forum Excel General
    Replies: 2
    Last Post: 12-11-2014, 09:58 AM
  3. Replies: 4
    Last Post: 11-17-2013, 12:05 PM
  4. [SOLVED] How to USE """"" cells count """"" change font color
    By austin123456 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 10-09-2013, 06:14 AM
  5. [SOLVED] Excel VBA Runtime error '1004' "If WorksheetFunction.Find" function
    By miguelitoven in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-11-2013, 04:34 PM
  6. [SOLVED] How to Count number of "Error" and "OK" after the word "Instrument" found in table row
    By eltonlaw in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 05-17-2012, 06:26 AM
  7. Replies: 5
    Last Post: 06-26-2006, 09:23 PM

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