+ Reply to Thread
Results 1 to 12 of 12

Help reduce code

  1. #1
    Forum Contributor
    Join Date
    10-03-2015
    Location
    North Carolina, USA
    MS-Off Ver
    Office 365
    Posts
    167

    Help reduce code

    I inherited a template that I use frequently. It has several macros, but there is one that I would like to simplify/reduce if possible.

    My best understanding of what it does is, it selects and copies a cell on one sheet (POC Calc), and then uses that selection to choose an option from a dropdown list on another sheet (PM input). The option from the dropdown list populates another range of cells which then gets selected and copied. The data that was copied is then pasted into a cell back in the POC Calc sheet.

    It then goes back and does the same thing over and over again, until the list on the PM input sheet is exhausted. I currently have to manually adjust the number of times the code repeats based on how many items are in the list in the PM input sheet or the number of populated cells in the range on the POC Calc sheet (The list in the dropdown of the PM input sheet and the items in the range on the POC Calc are identical).

    Can the code be simplified by using some type of loop? This would be great so that I don't have to manually update the code each time I use the template.

    (Sample file also attached).

    The person who built the template placed a button at the top of the POC Calc sheet to run the macro in question.

    Thank you!
    Attached Files Attached Files
    Last edited by carlito2002wgn; 11-21-2022 at 05:19 PM.

  2. #2
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    49,655

    Re: Help reduce code

    Fast answers need visual help. Please read the yellow banner at the top of this page on how to attach a file and a mocked up solution.
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  3. #3
    Forum Contributor
    Join Date
    10-03-2015
    Location
    North Carolina, USA
    MS-Off Ver
    Office 365
    Posts
    167

    Re: Help reduce code

    Quote Originally Posted by TMS View Post
    Fast answers need visual help. Please read the yellow banner at the top of this page on how to attach a file and a mocked up solution.
    Thank you, Trevor. I hesitated to upload the file originally since I wasn't sure how to "shrink" the file and still have it do what it is supposed to do. The file is now attached to my original post (gutted as much as I could).

  4. #4
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    49,655

    Re: Help reduce code

    There's something very strange about the file you have uploaded. I have tried twice to open it and both times it has crashed Excel AND my machine.

    I have opened recently downloaded .xlsx and .xlsm files without issue so it is very definitely something to do with the file. It is unlikely to be the VBA as that is not enabled (by default). What did you do to the file when you "gutted it"?

    I'm not trying for third time lucky.

  5. #5
    Forum Contributor
    Join Date
    10-03-2015
    Location
    North Carolina, USA
    MS-Off Ver
    Office 365
    Posts
    167

    Re: Help reduce code

    Quote Originally Posted by TMS View Post
    There's something very strange about the file you have uploaded. I have tried twice to open it and both times it has crashed Excel AND my machine.

    I have opened recently downloaded .xlsx and .xlsm files without issue so it is very definitely something to do with the file. It is unlikely to be the VBA as that is not enabled (by default). What did you do to the file when you "gutted it"?

    I'm not trying for third time lucky.
    I am sorry about that, Trevor! I am just returning from Thanksgiving holiday here in the US, but will look into the issue. As far as how I "gutted it", I simply deleted some sheets (I do not remember doing anything else). I will send it to my wife to see what happens Thanks for trying!

  6. #6
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    49,655

    Re: Help reduce code

    No problem.

  7. #7
    Forum Guru
    Join Date
    09-10-2017
    Location
    Chippenham, England
    MS-Off Ver
    365
    Posts
    15,988

    Re: Help reduce code

    Opens quite happily for me.
    How about
    Please Login or Register  to view this content.

  8. #8
    Forum Contributor
    Join Date
    10-03-2015
    Location
    North Carolina, USA
    MS-Off Ver
    Office 365
    Posts
    167

    Re: Help reduce code

    Quote Originally Posted by Fluff13 View Post
    Opens quite happily for me.
    How about
    Please Login or Register  to view this content.
    This is awesome! It works perfectly (and FAST).

    I feel like an ungrateful child asking, but is there any way to alter the code to account for the fact that the list of resources will not always be 3 To 39? It will always be from 3, but might be shorter or longer depending on the project.

    As mentioned in my first post, the list of resources is manually pasted by me each time I use the template (I won't be so greedy as to ask for help automating that!). If it is difficult to alter the code to count the number of resources each time I use the template, would it be easier to just increase the length of that range be default, say, 3:60 instead of 3:39? Obviously, I would need to expand the range in the template as well as the code.

  9. #9
    Forum Guru
    Join Date
    09-10-2017
    Location
    Chippenham, England
    MS-Off Ver
    365
    Posts
    15,988

    Re: Help reduce code

    How about
    Please Login or Register  to view this content.
    This will look for the word Total in col A & run up to the row before.

  10. #10
    Forum Contributor
    Join Date
    10-03-2015
    Location
    North Carolina, USA
    MS-Off Ver
    Office 365
    Posts
    167

    Re: Help reduce code

    Quote Originally Posted by Fluff13 View Post
    How about
    Please Login or Register  to view this content.
    This will look for the word Total in col A & run up to the row before.
    Bravo!

    This is perfect. Thank you so much!

  11. #11
    Forum Guru
    Join Date
    09-10-2017
    Location
    Chippenham, England
    MS-Off Ver
    365
    Posts
    15,988

    Re: Help reduce code

    Glad to help & thanks for the feedback.

  12. #12
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    49,655

    Re: Help reduce code

    Glad you have a solution. Opens ok on my iPad but, for some reason, problems on my laptop. Who knows … ?

+ 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] Need Understanding How To Reduce Code
    By PosseJohn in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-13-2016, 10:04 AM
  2. Replies: 1
    Last Post: 12-17-2013, 03:18 PM
  3. how can i reduce this code
    By newapa in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 09-19-2012, 04:44 AM
  4. Reduce Redundant Code Help Please
    By Hummuna in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 06-11-2012, 09:09 AM
  5. Reduce Runtime For This Code
    By moo5003 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-19-2011, 04:56 PM
  6. Reduce lines in code
    By charlie.duke2 in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 01-14-2010, 09:14 AM
  7. Reduce block of code
    By khalid79m in forum Excel General
    Replies: 2
    Last Post: 03-16-2007, 10:08 AM

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