+ Reply to Thread
Results 1 to 9 of 9

Need help writing a better loop

  1. #1
    Forum Contributor
    Join Date
    05-04-2013
    Location
    USA
    MS-Off Ver
    Excel 365
    Posts
    184

    Need help writing a better loop

    I need to find a better way to write the code below. The code below works great, but then it's looping through my whole data in order to accomplish what I want. The thing is my data can have 40,000 rows and it's taking forever. Is there a better way to write this?

    Basically, what the code below doing is looping through my data and evaluate the data in column D and G. If they are both "CONTRACT" and "ABC COMPANY". If that's the case, it will change the value in column D, which is "CONTRACT" to "CONTRACT ENG".


    I think another way to write the code is to do an autofilter and filer column D and G by "CONTRACT" and "ABC COMPANY". Once it's filtered, it would only be less than 20 rows of data that match that criteria. I can loop through those 20 rows, it's much faster than have the macros loop through 40k rows. I just don't know how to do it. Can someone help me?

    Please Login or Register  to view this content.
    Attached Files Attached Files
    Last edited by alexduy; 01-28-2021 at 07:31 PM.

  2. #2
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 insider Version 2505 Win 11
    Posts
    24,744

    Re: Need help writing a better loop

    If you provide a sample worksheet then I am sure we can provide you with a solution using Advanced Filter with or without VBA. Read the yellow banner at the top of this page.
    Alan עַם יִשְׂרָאֵל חַי


    Change an Ugly Report with Power Query
    Database Normalization
    Complete Guide to Power Query
    Man's Mind Stretched to New Dimensions Never Returns to Its Original Form

  3. #3
    Forum Expert
    Join Date
    05-29-2020
    Location
    NH USA
    MS-Off Ver
    365
    Posts
    2,103

    Re: Need help writing a better loop

    Try something like this

    Please Login or Register  to view this content.
    Last edited by maniacb; 01-28-2021 at 07:37 PM.

  4. #4
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 insider Version 2505 Win 11
    Posts
    24,744

    Re: Need help writing a better loop

    Administrative Note:

    @maniacb

    You need to include code tags around your code.

    Please take a moment to add the tags. Posting code between tags makes your code much easier to read and copy for testing, and it also maintains VBA formatting.

    Please see Forum Rule #2 about code tags and adjust accordingly. Click on Edit to open your post, then highlight your code and click the # icon at the top of your post window. More information about these and other tags can be found here

    (Note: this change is not optional.)

  5. #5
    Forum Contributor
    Join Date
    05-04-2013
    Location
    USA
    MS-Off Ver
    Excel 365
    Posts
    184

    Re: Need help writing a better loop

    Hi Alansidman,

    I've attached a workbook. Is it possible to write a script so that before the loop is run, the macro would check column D to see if the string "CONTRACT" exist. If it's not there, skip and move on with the macro.

    If it exists, then loop through column D and G to find CONTRACT in column D and ABC in column G. If both of those criteria match, then change CONTRACT to CONTRACT ENG.

    My script works well, it's just that sometime the string CONTRACT doesn't exist, the script still run and loop through 40 rows for nothing. Also, if the string exist, it still loop through 40k rows to make the change and usually I only have to change 3 or 4 rows.
    Last edited by alexduy; 01-28-2021 at 07:30 PM.

  6. #6
    Forum Contributor
    Join Date
    05-04-2013
    Location
    USA
    MS-Off Ver
    Excel 365
    Posts
    184

    Re: Need help writing a better loop

    maniacd - I tried your code and it did not work.

  7. #7
    Forum Expert
    Join Date
    05-29-2020
    Location
    NH USA
    MS-Off Ver
    365
    Posts
    2,103

    Re: Need help writing a better loop

    Try it this way

    Please Login or Register  to view this content.

  8. #8
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,653

    Re: Need help writing a better loop

    Please Login or Register  to view this content.
    Surround your VBA code with CODE tags e.g.;
    [CODE]your VBA code here[/CODE]
    The # button in the forum editor will apply CODE tags around your selected text.

  9. #9
    Forum Contributor
    Join Date
    05-04-2013
    Location
    USA
    MS-Off Ver
    Excel 365
    Posts
    184

    Re: Need help writing a better loop

    Thank you both very much. I was able to use both your codes and create my own code to get what I want. I really appreciate your help.

    Here is the code in case someone want to learn:

    Please Login or Register  to view this content.
    Last edited by alexduy; 01-29-2021 at 12:35 AM.

+ 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: 0
    Last Post: 08-11-2016, 09:28 PM
  2. Writing a loop to look up data
    By JesseV92 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-04-2016, 08:22 AM
  3. Writing to cell stops for loop
    By Amathanio in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 05-04-2016, 06:40 AM
  4. Problem writing a loop
    By themindseye in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 06-17-2014, 09:30 AM
  5. Need help writing Loop syntax
    By sslack88 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 06-26-2012, 11:02 PM
  6. Issue writing a loop
    By bg18461 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 05-30-2008, 08:27 AM
  7. help writing loop
    By robhargreaves in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-31-2005, 06:05 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