+ Reply to Thread
Results 1 to 41 of 41

More efficient way to loop through cells and assign values

  1. #1
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,712

    More efficient way to loop through cells and assign values

    Hi,

    I need a more efficient way to loop through cells on several worksheets and return results to summary worksheet. I am attaching the code in module1 that i am using and i dont know how long it takes......all i know is that it takes longer than 20min.

    I have 4 worksheets. I am trying to return values contained on the "TDY", "TCD", and "FullList" TO the summary sheet. To do this for example:
    Between the Summary and TDY: Match the Subj_DS value between the two sheets (if there is one) and return the "days" value to the summary sheet.
    Between the Summary and TCD: Match the Subj_DS value between the two sheets (if there is one) and return the "ReadStatus" value to the summary sheet.
    Between the Summary and FullList: Match the Subj value (Note: Subj value only) between the two sheets (if there is one) and return the "StatusB" value to the summary sheet.


    i am attaching the code and excel file.

    ************Note: The file i had was too big to attach so i had to reduce the number of rows to make the file smaller..........


    the code i am using is also shown below:

    Please Login or Register  to view this content.
    Attached Files Attached Files

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

    Re: More efficient way to loop through cells and assign values

    This is not a code, but a formula. I have deleted most of the rows on the summary sheet in order to reduce the size of the file.
    Attached Files Attached Files

  3. #3
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,712

    Re: More efficient way to loop through cells and assign values

    thanks AB33 but i am looking for a VBA type soln. Is there an efficient vba soln out there? Mine certainly is not going to work.

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

    Re: More efficient way to loop through cells and assign values

    Try the attached code.
    Attached Files Attached Files

  5. #5
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,712

    Re: More efficient way to loop through cells and assign values

    AB33 thanks for your help. How did you come up with that code so quick?

    I took your formulas and some vba code and was able to get it to complete in 1.18 secs......which is way better than i thought it would.

    I ran your code (again using the same dataset as above but usingthe FULL data set not the reduced one enclosed in this thread) and it took ~ 20 sec.

    So i guess a combination of inserting formulas and using VBA is the most efficient way?

    If anyone has a better way just let me know.

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

    Re: More efficient way to loop through cells and assign values

    Generally speaking, Excel's built functions are normally faster than a macro, and looping through arrays are faster than looping through a range, but using dictionary can further increase the speed. My code is not entirely looping through arrays as it has got an element of looping through a collection of ranges.

    You can get faster than my code if all the looping is carried out using arrays, but the tricky bit is matching the summary rows with each sheet rows, hence I had to use looping through a range . If all the sheets have had the same layout , there was no need for the last bit of the code. This probably would have saved another 5 seconds of the clock.

    I think 20 seconds for over 10k rows across 4 sheets is not that bad.
    Last edited by AB33; 08-27-2013 at 10:25 AM.

  7. #7
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,712

    Re: More efficient way to loop through cells and assign values

    AB33,

    I guess i spoke too soon...........so now the my approach is taking ~ 83sec..................

    Why my test code took ~ 1.3sec and now when i put it into program it takes ~83sec...........i am guessing i have a formula some where that is attempting to recalculate but i am not sure.

    Also, you said if you used arrays you could make it faster.........can you show me more about this not sure i understand.

  8. #8
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,712

    Re: More efficient way to loop through cells and assign values

    i am trying to go through your code now..........

    can you tell me what this is doing

    Please Login or Register  to view this content.

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

    Re: More efficient way to loop through cells and assign values

    First, assign(or upload) the data in to array and then loop through the array

    For e.g, the range of summary sheet is assigned to x variant

    Please Login or Register  to view this content.
    You then column 4 is added to dic
    .Item(x(i, 3)) = x(i, 4)

    When you run the code on your actual data, how long does it take to run it? Forget about a formula.
    Last edited by AB33; 08-27-2013 at 02:51 PM.

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

    Re: More efficient way to loop through cells and assign values

    Try the attached. It might be faster than the previous code, but as I said, formula is much faster than a code.
    Attached Files Attached Files
    Last edited by AB33; 08-27-2013 at 03:17 PM.

  11. #11
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,712

    Re: More efficient way to loop through cells and assign values

    hi,

    so this is adding col-4 to dictionary

    Please Login or Register  to view this content.
    but what i dont get is why the x(i,3)

    dont know what this means...........what would happen if its x(i,2) instead?

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

    Re: More efficient way to loop through cells and assign values

    You are testing column 3 for a match.
    The concept is s very similar to Index and match formula, Match two columns and return the corresponding row-index.
    You are matching column3 of sheet summary with the other sheets and then return the index of other sheets(column4) to summary sheet.
    .Item(x(i, 3)) = x(i, 4)

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

    Re: More efficient way to loop through cells and assign values

    By the way, have you tested the latest code and if so, did you see any change in a speed? The code is also shorter than the previous one.

  14. #14
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,712

    Re: More efficient way to loop through cells and assign values

    have not tested it yet......after dinner i will test it and get back to you. thanks.

    also, i have been meaning to learn about scripting.dictionary stuff......any good web sites or info you recommend because i cant find it in any books.....

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

    Re: More efficient way to loop through cells and assign values

    I know!
    You have asked similar question in the past. I remember Jindon had given you some info.
    I was in your position a year ago. I started on this site (See attached).
    IMO, it would be much easier to first master loading data in to array and copying the loaded array back in to range. It then becomes easier to learn dictionary.

    http://www.experts-exchange.com/Soft...ss-in-VBA.html

  16. #16
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,712

    Re: More efficient way to loop through cells and assign values

    i get a run time error at this step


    Please Login or Register  to view this content.

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

    Re: More efficient way to loop through cells and assign values

    Where? Is it in your actual code, or in the sample?

  18. #18
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,712

    Re: More efficient way to loop through cells and assign values

    its in the code example you sent ........the latest one.......but i used in my "FULL" data set.

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

    Re: More efficient way to loop through cells and assign values

    If the layout of the your actual data has not changed since you run the first code, I do not know why you are getting an error. The number of columns has remained the same-4.

  20. #20
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: More efficient way to loop through cells and assign values

    Please Login or Register  to view this content.

    NB. I altered the misspelled sheetname FullLlist into 'List'
    Last edited by snb; 08-28-2013 at 04:16 AM.



  21. #21
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,712

    Re: More efficient way to loop through cells and assign values

    trying to work through the code to understand whats going on.

    is there a way to display the dictionary.........it would be helpful to me if i could see whats actually in the dictionary

    is this even possible?

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

    Re: More efficient way to loop through cells and assign values

    The link I had sent will show you how the dic and arrays work.
    I do not think you may find many free material on dic. Whatever you find on-line is not specifically geared to-wards excel.The best way to learn is by "Doing"

  23. #23
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,712

    Re: More efficient way to loop through cells and assign values

    AB33, i double checked as far as i can tell i have not changed any of the worksheet formats or column designations. Still i dont know why i am getting a type mismatch error when i try to run your code. I am sure it is something simple.......just dont know what it is.


    snb, i had a question about your code (note: anyone can answer the question):
    In the code you have the following

    Please Login or Register  to view this content.
    can you describe what this is doing?
    What i think i know is this.......and please correct me if i am wrong.
    The key for the dictionary is the 3rd column of values (Subj_Ds).
    There are 5 values for each key: sn(j, 1), sn(j, 2), sn(j, 3), sn(j, 4), ""

    ?How would you access value 3 [sn(j,3)] for a certain key?...........


    Also in reviewing snb's code i dont understand the role of "st" and what it is....can someone help me understand whats going on below:
    Please Login or Register  to view this content.

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

    Re: More efficient way to loop through cells and assign values

    I really do not know what causes the error as you did not get an error on the first code.

  25. #25
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,712

    Re: More efficient way to loop through cells and assign values

    also if someone could explain this i would appreciate it..........

    Please Login or Register  to view this content.
    i know its pasting something onto the summary worksheet but not sure what?

  26. #26
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,712

    Re: More efficient way to loop through cells and assign values

    Lastly,

    if someone could explain this it would be helpful:
    Please Login or Register  to view this content.
    i know it says something about go to first column of the summary worksheet and find the value in sq array but then i dont get the .offset or why sq(j,2) is being assigned ..........just confused. please help.

  27. #27
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,712

    Re: More efficient way to loop through cells and assign values

    AB33,

    in the first code you did not have

    Please Login or Register  to view this content.

    you had this

    Please Login or Register  to view this content.

    i get the type mismatch at
    Please Login or Register  to view this content.

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

    Re: More efficient way to loop through cells and assign values

    Try to use this line

    Please Login or Register  to view this content.
    Instead of
    Please Login or Register  to view this content.
    Do you still have column 4 as x(i, 4) refers to column 4?

  29. #29
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: More efficient way to loop through cells and assign values

    Did you run the code ?

    Did you run the code step by step ?
    Did you make the window 'local' in the VBEditor visible ?

  30. #30
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,712

    Re: More efficient way to loop through cells and assign values

    AB33,

    I figured out what was causing the problem.......the layout was the same.
    The problem was my FULL-data set on the TDY worksheet in the column of "Days" there were "#Values"............i reassigned all the "#Values" to a value of 993 and the macro ran just fine.

    also, why did the #Values cause a problem?

    have not verified it yet.........but had a question

    can you explain what this is doing?
    Please Login or Register  to view this content.

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

    Re: More efficient way to loop through cells and assign values

    I have re-run the code on the sample, I did not get any error and the code completes in 3 seconds.

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

    Re: More efficient way to loop through cells and assign values

    Well,
    It is an error value and the code will show that.
    If you have got formulas in your data, please use iferror excel's function to trap the error.

    Please Login or Register  to view this content.

  33. #33
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,712

    Re: More efficient way to loop through cells and assign values

    snb,

    i have not "stepped" through the code. I have reviwed the code put hard breaks in it where i thought i should learn something.

    I may know that the following code puts results i want onto summary sheet but i dont understand why?
    Please Login or Register  to view this content.
    Last edited by welchs101; 08-28-2013 at 09:03 AM.

  34. #34
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,712

    Re: More efficient way to loop through cells and assign values

    snb, can you also explain a little this code?

    Please Login or Register  to view this content.
    i know whats happening i just dont know why.....hopin you could shed some light.

  35. #35
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,712

    Re: More efficient way to loop through cells and assign values

    any way to display key and items in dictionary so i can see whats "going on"

  36. #36
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,712

    Re: More efficient way to loop through cells and assign values

    AB33 and snb,

    I hope you're both still there............

    I took the examples you both gave and came up with this approach........trying to learn this as well. I think i have all the data into the dictionary but now i need to somehow get it out of the dictionary and into the correct col locations...........

    please help if you can?

    also, if there is something fundamentlly wrong with my approach or fundamentally better about yours please let me know.


    Please Login or Register  to view this content.

  37. #37
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,712

    Re: More efficient way to loop through cells and assign values

    anyone? so close to end.............can someone help?

  38. #38
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,835

    Re: More efficient way to loop through cells and assign values

    I haven't read all through the history, so worked with the file.

    See if this is what you are after.
    Attached Files Attached Files
    Last edited by jindon; 08-28-2013 at 10:41 AM.

  39. #39
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,712

    Re: More efficient way to loop through cells and assign values

    thanks jindon,

    i will take a look at your code as well.

    But i really spent a lot of time learning AB33 and snb's code.................i took parts from each and came up with my own version. Problem is i dont know how to get the data out of the dictionary............

    i will review your code thanks.

  40. #40
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,712

    Re: More efficient way to loop through cells and assign values

    i scrapped my previous attempt and went with something that resembles what AB33 did...........now keep in mind i am just starting to learn about these dictionary things and while jindon's code and snb's code work and are very fast...........there are things in the code i dont understand. I understand AB33 code........so i am using a variation of it.......

    enclosing code below:
    Please Login or Register  to view this content.

  41. #41
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: More efficient way to loop through cells and assign values

    Please Login or Register  to view this content.

    Writes any 2-dimensional array into a worksheet.
    See the explanation for 'index' in the Excel formulae.
    In this case VBA uses an Excel function.

    You will have to apply this method to 'understand' what it does.

+ 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. Help edit the code to loop, in addtion to being more efficient.
    By rocksan in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 08-28-2012, 01:44 AM
  2. Assign fixed values to array / For...Next loop
    By SunOffice in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 12-18-2011, 05:43 PM
  3. More efficient alternative to Find-Replace Loop?
    By Paul in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 02-03-2010, 01:49 AM
  4. Remove long running loop with someting more efficient
    By JP Romano in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 09-10-2009, 02:20 PM
  5. Assign cells values to an array
    By mikeburg in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 12-08-2006, 06:15 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