+ Reply to Thread
Results 1 to 6 of 6

Macro for Replacing Text Produces Run-Time Error 91

  1. #1
    Registered User
    Join Date
    04-14-2014
    Location
    Atlanta, GA, USA
    MS-Off Ver
    Excel 2016
    Posts
    46

    Macro for Replacing Text Produces Run-Time Error 91

    Hi! I have a macro that works fine and with no errors if there is a single instance of a string--" Total"--found within a cell in column D. It also works until the string can no longer be found, but once all the values of " Total" have been found and replaced, the macro stops and the following error pops up: "Run-time error '91': Object variable or With block variable not set." The debug option pops up at the point in the code highlighted in blue below, and is obviously related to not finding " Total" in column D.

    I have searched and searched for code and keep trying to modify the macro to eliminate the error, but with no success. I'd like this this macro to run automatically on each worksheet as they are created rather than going to each worksheet and running the macro separately. Although this is a workaround that will produce the desired formatting of the worksheets, it is more time-consuming.

    Any help you can offer will be greatly appreciated after spending MANY hours trying to fix this! I am not locked into any of the code other than that needed for the formatting.

    Thanks in advance!
    Please Login or Register  to view this content.
    Last edited by bryanmarks; 04-30-2014 at 08:44 AM.

  2. #2
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    22,009

    Re: Macro for Replacing Text Produces Run-Time Error 91

    You need to take the same precaution as you did with the outer Find loop - i.e. test if the value is found by assigning it to a range object:
    Please Login or Register  to view this content.
    Everyone who confuses correlation and causation ends up dead.

  3. #3
    Registered User
    Join Date
    04-14-2014
    Location
    Atlanta, GA, USA
    MS-Off Ver
    Excel 2016
    Posts
    46

    Re: Macro for Replacing Text Produces Run-Time Error 91

    Hi, Rory! Thanks for the reply! I'm afraid I'm a bit confused by your response, however. Does the code you proposed replace the code I have at the top of my macro, or does this need to be inserted in place of the code in blue, or is there yet another alternative? I'm sorry, but although I know how to program in SAS and SPSS, VBA is still brand new to me.

  4. #4
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    22,009

    Re: Macro for Replacing Text Produces Run-Time Error 91

    Untested, but perhaps something like this:
    Please Login or Register  to view this content.

  5. #5
    Registered User
    Join Date
    04-14-2014
    Location
    Atlanta, GA, USA
    MS-Off Ver
    Excel 2016
    Posts
    46

    Re: Macro for Replacing Text Produces Run-Time Error 91

    That did it! I only had to make two small modifications in red below to make it work properly. Thanks so much for your help!
    Please Login or Register  to view this content.

  6. #6
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    22,009

    Re: Macro for Replacing Text Produces Run-Time Error 91

    Glad to help.

+ 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] ComboList produces errors run-time error...
    By radgrad in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-13-2014, 09:24 PM
  2. [SOLVED] "Run-time error '13': type mismatch when running macro to add text to end of text
    By nrjordan in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-18-2012, 10:34 PM
  3. Calling named cell into macro calculation produces error
    By widodude in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-25-2011, 09:26 AM
  4. Run-time error '91' when replacing text in mulitple workbooks
    By kshimabukuro in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 12-06-2010, 11:11 PM
  5. Clicking Red X on UserForm produces run-time error '91'
    By ChrisMattock in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 12-10-2008, 04:38 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