+ Reply to Thread
Results 1 to 3 of 3

Macro Project - Consistently breaks

  1. #1
    Registered User
    Join Date
    02-18-2015
    Location
    Hollywood, FL
    MS-Off Ver
    2010
    Posts
    2

    Macro Project - Consistently breaks

    Greetings all,

    I have a project that I am working on that involves the creation of several macros within one workbook. The goal is to provide a central workbook that can be accessed by any staff member and based upon the source file selected for a data matching task, the associated macro can be applied. The purpose of the macros is to provide preparatory formatting, move the contents of Sheet1 to Sheet2 and rename both sheets.

    I was able to create the macros using the Personal.xlsb method and all seemed fine when I ran the first macro. As soon as I either "save" or close and re-open, the workbook breaks with any of several errors. I have received "400 Visual Basic" errors that say nothing else but what is in quotes, another Microsoft Visual Basic error of "Run-time error '9': Subscript out of range". Below is the macro that is resulting in the run-time error 9 so if anyone can shed some light on whether or not I am going about this the right way or perhaps a better solution is available I would be very appreciative. FYI, the macro below pertains to a file containing 7881 rows.

    Moderator's note: Please take the time to review our rules. There aren't many, and they are all important. Rule #3 requires code tags. I have added them for you this time because you are a new member. --6StringJazzer

    Please Login or Register  to view this content.
    Last edited by 6StringJazzer; 02-23-2015 at 01:44 PM.

  2. #2
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS365 Family 64-bit
    Posts
    24,721

    Re: Macro Project - Consistently breaks

    When you put a macro in the PERSONAL.XLSB file, it will apply to whatever the active workbook is. That may work fine for you, or it could be part of the problem you are having. In either case, this will not provide macros in a central workbook that can be used by all staff. PERSONAL.XLSB is just yours.

    When you get a runtime error in VBA, the code window open and highlights the offending line of code in yellow. It will be very helpful if you tell us what line is showing up.

    A subscript error can be caused by a couple of different things. In this case it is almost certainly caused by a reference to a worksheet that doesn't exist, most likely in one of these lines of code:

    Please Login or Register  to view this content.
    The macro as written does not pertain to any particular workbook, but pertains to whatever workbook is active at the time. So either your workbook does not have sheets with these names, or maybe the file that you want to use isn't really the active workbook.

    The first thing I would do is to create a Module in the actual workbook that this code operates on and move this code in there.

    Secondly, your code has macro recorder bloat. This code can be streamlined quite a bit. For example, the scrolling is only for display purposes and not really needed for what you're doing. Also, you don't have to Select a range to operate on it.

    You are deleting columns B:H then H:M in two separate operations. That sounds like more than the formatting you described. Note that this is equivalent to deleting B:H and O:T in a single operation, since once you delete B:H, the data in column H is what started out in O. Hopefully that's really what you mean to do.


    I would probably write this code like this, but don't have your file so testing was cursory:
    Please Login or Register  to view this content.
    Jeff
    | | |會 |會 |會 |會 | |:| | |會 |會
    Read the rules
    Use code tags to [code]enclose your code![/code]

  3. #3
    Registered User
    Join Date
    02-18-2015
    Location
    Hollywood, FL
    MS-Off Ver
    2010
    Posts
    2

    Re: Macro Project - Consistently breaks

    @Jeff. Thank you so much for your speedy and extremely helpful reply. Being a noob to VBA, I am not surprised that there is room for improvement with the optimization of my code, however, thanks to your reply, I was able to get the functionality and that was the first big hurdle.

    As you most likely noticed in the sample code, each of the macros, will ultimately move all of the contents of Sheet1 to Sheet2 and rename both. Following this step, I need to bring the contents from Sheet1 in an external workbook into the one with the macros. I assume the syntax would be very similar to the move from sheet to sheet, however, can you, or anyone share the syntax needed to reference an external workbook?

    Peace and respect,

+ 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] Line breaks in a string not working consistently.
    By Arkadi in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 12-11-2014, 01:27 PM
  2. Clear all page breaks, then insert new breaks at change in data
    By Alex0929 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 09-09-2014, 03:12 PM
  3. Macro doesn't work consistently
    By mrbill11 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 06-14-2012, 04:35 PM
  4. Hard page breaks being replaced by soft breaks in the wrong place
    By JDavies in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 11-14-2008, 10:28 AM
  5. Hard breaks in text to soft breaks in Excel
    By tbailey in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-26-2005, 12:05 AM

Tags for this Thread

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