+ Reply to Thread
Results 1 to 19 of 19

Speeding up vba macro multiple for loops

  1. #1
    Registered User
    Join Date
    11-10-2019
    Location
    Australia
    MS-Off Ver
    365
    Posts
    15

    Question Speeding up vba macro multiple for loops

    Hi guys i have a code that loops through the columns, makes new sheets for each one and then does some formatting depending on the acronoym in the matrix underneath. Anyway to speed it up? On a laptop its taking a few minutes to run.

    Please Login or Register  to view this content.
    Last edited by kpayney1; 04-03-2022 at 06:34 PM.

  2. #2
    Forum Expert nankw83's Avatar
    Join Date
    08-31-2015
    Location
    Kuwait
    MS-Off Ver
    365
    Posts
    1,712

    Re: Speeding up vba macro multiple for loops

    Hi kpayney1 & welcome to the forum

    Try changing the below part of your code ...

    Please Login or Register  to view this content.
    with this
    Please Login or Register  to view this content.
    If I was able to help, you can thank me by clicking the * Add Reputation under my user name

  3. #3
    Registered User
    Join Date
    11-10-2019
    Location
    Australia
    MS-Off Ver
    365
    Posts
    15

    Re: Speeding up vba macro multiple for loops

    Hi Nankw83, Thanks for the quick response. That code is bringing up a 1004 error?

  4. #4
    Forum Expert nankw83's Avatar
    Join Date
    08-31-2015
    Location
    Kuwait
    MS-Off Ver
    365
    Posts
    1,712

    Re: Speeding up vba macro multiple for loops

    It is compiling with no errors at my end. Can you attach a sample file so we could test & identify the issue

  5. #5
    Registered User
    Join Date
    11-10-2019
    Location
    Australia
    MS-Off Ver
    365
    Posts
    15

    Re: Speeding up vba macro multiple for loops

    File Attached
    Attached Files Attached Files

  6. #6
    Registered User
    Join Date
    11-10-2019
    Location
    Australia
    MS-Off Ver
    365
    Posts
    15

    Re: Speeding up vba macro multiple for loops

    Hi Nankw83, attached file above. Appears to be hitting an error on the autofilter line. Can you explain the .offset= evaluate line please?

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

    Re: Speeding up vba macro multiple for loops

    nankw83 Code is faster and leaner, but it errors as .Offset(, 3) = Evaluate("if(isnumber(--(left(" & .Address & ",1))),""x"","""")") returns null, hence for error on filter.
    I have removed two loops and merged them.
    The code will error on newSh.Name = "GCID" if sheet GCID already existed.

    Please Login or Register  to view this content.

  8. #8
    Forum Expert nankw83's Avatar
    Join Date
    08-31-2015
    Location
    Kuwait
    MS-Off Ver
    365
    Posts
    1,712

    Re: Speeding up vba macro multiple for loops

    I made few changes to the code ... Also, I moved it to a module & rename macro name. Please check attached file which seems to work
    Please Login or Register  to view this content.
    Attached Files Attached Files

  9. #9
    Registered User
    Join Date
    11-10-2019
    Location
    Australia
    MS-Off Ver
    365
    Posts
    15
    [QUOTE=AB33;5658235]nankw83 Code is faster and leaner, but it errors as .Offset(, 3) = Evaluate("if(isnumber(--(left(" & .Address & ",1))),""x"","""")") returns null, hence for error on filter.

    Awesome I'll give yours a try. Can you please explain the .offset code line? Assuming .offset is the object column 3 and the if statement looks for numbers but not understanding the excessive "" and & comments

  10. #10
    Registered User
    Join Date
    11-10-2019
    Location
    Australia
    MS-Off Ver
    365
    Posts
    15
    Hi Nankw83 can you explain the offset = evaluate line? Looks like a good piece of code to keep in the memory bank.

  11. #11
    Forum Expert
    Join Date
    10-06-2017
    Location
    drevni ruchadlo
    MS-Off Ver
    old
    Posts
    2,151

    Re: Speeding up vba macro multiple for loops

    The "Trial" file is inadequate to the presented code.
    Last edited by mjr veverka; 04-02-2022 at 08:45 AM.

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

    Re: Speeding up vba macro multiple for loops

    .Offset(, 2) = Evaluate("if(isnumber(--(left(" & .Address & ",1))),""x"","""")")
    Means 2 columns to the right of column A, which is column C.
    Evaluate extracts left function for any cell that has a number.

  13. #13
    Forum Expert sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,239

    Re: Speeding up vba macro multiple for loops

    GCID = RemoveInvalidCharacters(newSh.Range("B2"))
    Not sure what characters you are wanting to replace as B2 has no values...
    Also, what shall new tabs be named...
    Will amend once I understand the above...

    This, however, will be much faster...
    Please Login or Register  to view this content.
    Attached Files Attached Files
    Last edited by sintek; 04-02-2022 at 09:53 AM.
    Good Luck
    I don't presume to know what I am doing, however, just like you, I too started somewhere...
    One-day, One-problem at a time!!!
    If you feel I have helped, please click on the star to left of post [Add Reputation]
    Also....add a comment if you like!!!!
    And remember...Mark Thread as Solved.
    Excel Forum Rocks!!!

  14. #14
    Registered User
    Join Date
    11-10-2019
    Location
    Australia
    MS-Off Ver
    365
    Posts
    15

    Re: Speeding up vba macro multiple for loops

    Hi Sintek, Originally on each new sheet the B2 was pulled from the job role name row in the entry sheet and that row had special characters in it so i was using regex to strip the characters then check if it was under 32 char so that it could be used for the sheet name. Your code works perfectly, I think the B2 issue is from my end as the file ive got has an extra row as below. Once i replace the entry sheet with the full version and comment out the merge line it works perfectly. Would you be able to comment your code so i may learn the differences?
    Last edited by kpayney1; 04-03-2022 at 01:15 AM.

  15. #15
    Registered User
    Join Date
    11-10-2019
    Location
    Australia
    MS-Off Ver
    365
    Posts
    15

    Re: Speeding up vba macro multiple for loops

    Also sintek attached is the correct sheet with row layout. The GCID code is used to strip D4 to AJ4 of special characters then uses those cells as names for each sheet in the loops.
    Attached Files Attached Files

  16. #16
    Forum Expert sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,239

    Re: Speeding up vba macro multiple for loops

    I only see 2 special characters...And ideally a space should remain between sheet name strings...
    This should cover all...I will comment code if this all works as it should...
    Attached Files Attached Files
    Last edited by sintek; 04-03-2022 at 09:51 AM.

  17. #17
    Registered User
    Join Date
    11-10-2019
    Location
    Australia
    MS-Off Ver
    365
    Posts
    15
    Quote Originally Posted by sintek View Post
    I only see 2 special characters...And ideally a space should remain between sheet name strings...
    This should cover all...I will comment code if this all works as it should...
    Originally the role names had a few more / and - but where stripped down since I made the code. It all works fine on my end! Thanks alot I'd love to see the comments to learn how you made it so fast.

  18. #18
    Forum Expert sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,239

    Re: Speeding up vba macro multiple for loops

    Post 16 file has been updated with comments...Securi Firewall not allowing code posting...

    Thanks for rep +
    Last edited by sintek; 04-03-2022 at 09:52 AM.

  19. #19
    Registered User
    Join Date
    11-10-2019
    Location
    Australia
    MS-Off Ver
    365
    Posts
    15

    Re: Speeding up vba macro multiple for loops

    Quote Originally Posted by sintek View Post
    Post 16 file has been updated with comments...Securi Firewall not allowing code posting...

    Thanks for rep +
    Thanks Sintek! and no problems. Between you and nankw83 I have picked up a few new ways of speeding my code up.

+ 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] Speeding up Macro
    By booney440 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 03-09-2017, 10:41 PM
  2. Speeding up For Each Loops
    By deeppurple247 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-06-2015, 07:26 AM
  3. [SOLVED] Speeding Up Macro
    By ScabbyDog in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 01-03-2015, 11:15 AM
  4. Speeding up For loops
    By Gingermuppet in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 03-18-2013, 06:15 AM
  5. multiple loops in one macro
    By jeskit in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-28-2011, 08:43 AM
  6. Speeding up a macro
    By Mister P in forum Excel Programming / VBA / Macros
    Replies: 17
    Last Post: 12-20-2010, 02:50 AM
  7. Speeding Up Excel Performance-VBA Loops
    By okelly in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-17-2006, 10:45 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