+ Reply to Thread
Results 1 to 10 of 10

Help to streamline my code.

  1. #1
    Registered User
    Join Date
    02-15-2011
    Location
    Tri-Cities, Wa
    MS-Off Ver
    Excel 2010
    Posts
    3

    Help to streamline my code.

    I'm new to the forum and scripting/Macro Excel. I have a calculation sheet that the user inputs weight by pounds into a cell (G5), then in the cells following (H5, I5, J5, L5, M5, N5), the user inputs ounces. With the following code this allows me to have the user input ounces in one cell (H5) and then a percentage is calculated and outputted into the same cell (H5). Basically the formula is simple, oz. x 0.0625 / lbs. * 10. I found this code on the web, modified it and it worked great. Now I've added a little more to the formula and I get "Procedure too large". Not surprising I guess. HELP!!

    Here's my code:
    Please Login or Register  to view this content.


    Every time I try to simplify I get errors. I would appreciate help, a litle frustrated at the moment.

    Thank you
    Last edited by Leith Ross; 02-15-2011 at 06:07 PM. Reason: Added Code Tags

  2. #2
    Forum Expert
    Join Date
    11-29-2010
    Location
    Ukraine
    MS-Off Ver
    Excel 2019
    Posts
    4,168

    Re: Help to streamline my code.

    try this in a woksheet module:

    Please Login or Register  to view this content.

  3. #3
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,169

    Re: Help to streamline my code.

    Hi gpwaters and welcome to the forum,
    Here would be the first Improvement on your code, if I was your teacher.

    Please Login or Register  to view this content.
    One test is worth a thousand opinions.
    Click the * Add Reputation below to say thanks.

  4. #4
    Registered User
    Join Date
    02-15-2011
    Location
    Tri-Cities, Wa
    MS-Off Ver
    Excel 2010
    Posts
    3

    Re: Help to streamline my code.

    Quote Originally Posted by watersev View Post
    try this in a woksheet module:

    Please Login or Register  to view this content.
    Wow, thanks for the amazingly fast reply. And it's working perfectly. I inserted your code into my "If Range ("XX").Value <> "" to keep a run-time error from occuring when a value is deleted in one of the cells and its working great.

    Thanks again.

  5. #5
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,169

    Re: Help to streamline my code.

    Looking at all the Case statement repitition I'd suggest something like this after you thought about it a while.
    Please Login or Register  to view this content.
    Hope all this helps - there are a lot of ways to write code that doesn't work, a few less that work and fewer yet that you can read and understand a few months from now.

  6. #6
    Valued Forum Contributor
    Join Date
    09-21-2003
    Location
    British Columbia , Canada
    MS-Off Ver
    03,07,10,13
    Posts
    727

    Re: Help to streamline my code.

    A couple of error checks to do is that the source cell is actually a number and that the value in G5 or G6 is also a number.
    Also good idea to trap for errors and EnableEvents even if there is an error/crash
    This solution does an update for a change in any cell in the range ("I5:U6"),
    To increase the range of cells, i.e. row or columns, you only need to change one thing '("I5:U6"), .. and your done

    Please Login or Register  to view this content.
    Last edited by nimrod; 02-15-2011 at 07:54 PM.

  7. #7
    Valued Forum Contributor
    Join Date
    09-21-2003
    Location
    British Columbia , Canada
    MS-Off Ver
    03,07,10,13
    Posts
    727

    Re: Help to streamline my code.

    This sol'n will put error statement in source if you enter invalid information ie. zeros , blanks , text ... etc
    This solution does an update for a change in any cell in the range ("I5:U6"),
    To increase the range of cells, i.e. row or columns, you only need to change one thing '("I5:U6"), .. and your done

    Please Login or Register  to view this content.
    Last edited by nimrod; 02-15-2011 at 08:19 PM.

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

    Re: Help to streamline my code.

    or
    Please Login or Register  to view this content.



  9. #9
    Registered User
    Join Date
    02-15-2011
    Location
    Tri-Cities, Wa
    MS-Off Ver
    Excel 2010
    Posts
    3

    Re: Help to streamline my code.

    Thank you nimrod.

    Althought watersev's answer worked very well, I still had a few issues. Your answer has been exactly what I've been looking for. I knew with such repetition in my code there had to be a way to narrow it to a very small block, which yours did perfectly.

    Thanks again


    Quote Originally Posted by nimrod View Post
    A couple of error checks to do is that the source cell is actually a number and that the value in G5 or G6 is also a number.
    Also good idea to trap for errors and EnableEvents even if there is an error/crash
    This solution does an update for a change in any cell in the range ("I5:U6"),
    To increase the range of cells, i.e. row or columns, you only need to change one thing '("I5:U6"), .. and your done

    Please Login or Register  to view this content.
    Last edited by gpwaters; 02-21-2011 at 03:02 PM.

  10. #10
    Valued Forum Contributor
    Join Date
    09-21-2003
    Location
    British Columbia , Canada
    MS-Off Ver
    03,07,10,13
    Posts
    727

    Re: Help to streamline my code.

    HI gpwaters:
    Glad you liked the sol'n , thx for the feedback.

    Cheers

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

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