+ Reply to Thread
Results 1 to 2 of 2

Problematic VBA Code for Formatting

  1. #1
    Registered User
    Join Date
    05-03-2019
    Location
    Tampa, Florida
    MS-Off Ver
    Office 365
    Posts
    3

    Problematic VBA Code for Formatting

    Hello all!

    Over 2 years ago, I developed a macro for my firm in order to format raw data output from our inventory database into a formatted and polished file we could send externally to clients. It always ran a bit slow (around 5 minutes), but other than that we had no issues. We recently changed from Office 2016 to Office 365, and around the same time people started noticing spikes in the macro run time of over 10 minutes. No changes had been made to the code itself, but all of a sudden the runtime became astronomical. I went back in to the file and cleaned up the macro in an attempt to improve speeds. It seemed to work well, with run times for smaller files down around 1 minute, but larger files are still taking 10 minutes or more. I believe the bulk of the time is spent performing the last function of the macro which is an If, Then function. When re-writing I tried to limit the number of cells being checked using a LastRow function, but I'm not sure I got it quite right. I'm going to post the code below. Maybe someone will see something that I'm not. I'm not an expert with VBA, so any input or feedback would be extremely helpful!

    Please Login or Register  to view this content.
    Last edited by alfrank.1012; 05-03-2019 at 06:46 PM.

  2. #2
    Forum Expert
    Join Date
    06-26-2010
    Location
    Austin, TX
    MS-Off Ver
    Excel 2010
    Posts
    1,673

    Re: Problematic VBA Code for Formatting

    Not sure how much it slows things down, but all of those 'single line' with statements can be combined into a single statement. For example:
    Please Login or Register  to view this content.
    can become
    Please Login or Register  to view this content.
    I think you have a bug here:
    Please Login or Register  to view this content.
    If Lastrow is 5 then the string above becomes "A2:J25" and I think you want "A2:J5". Just change the J2 to just J. But this should not be a big time killer since it is just formatting.

    Need to look for something that really adds time as it gets bigger...
    Ah - same type of bug here in your For loop:
    Please Login or Register  to view this content.
    A couple of things here.
    1) Similar bug as before. If Lastrow is 5, then you are doing the range D4:J25. And as Lastrow becomes bigger that just goes up orders of magnitude (e.g. Lastrow=100, you are now going through D4:J4100).
    2) That loop goes by cells - your c is a cell. Yet, the operations are row based. So, the first loop uses D4 and adjusts row 4. The second loop is E4, which also adjusts row 4. The third loop is F4, which also adjusts row 4. You are adjusting the same row 7 times when you only need to do it once.
    To fix both those issues:
    Please Login or Register  to view this content.
    Pauley
    --------
    If I helped with your issue, I'd appreciate a rep bump (hit the '*' icon to the bottom left of this post).

+ 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. Problematic ‘Edit’ code in Multiple Page Userform
    By Buddy8 in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 07-16-2016, 05:37 PM
  2. Problematic ‘Edit’ code in Multiple Page Userform
    By Buddy8 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-12-2016, 10:46 PM
  3. Problematic { symbols in formulas
    By Helloworld13 in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 04-30-2014, 09:53 PM
  4. [SOLVED] Problematic Auto-sort
    By uttuck in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 12-21-2012, 07:49 PM
  5. Problematic If clause
    By gloria in forum Excel Formulas & Functions
    Replies: 7
    Last Post: 12-16-2009, 04:37 PM
  6. problematic problem
    By cjjoo in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 10-10-2005, 01:22 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