+ Reply to Thread
Results 1 to 19 of 19

Need help tidying up code

  1. #1
    Registered User
    Join Date
    03-21-2019
    Location
    Barnsley, England
    MS-Off Ver
    2016
    Posts
    9

    Need help tidying up code

    Hi Can anyone help me tidy up some code, the current code works but is difficult to maintain, I'm sure this can be simplified but being a novice I am unsure, any help would be appreciated.


    The current code is only 3 blocks, I have around 20 blocks of the same code in total.
    Please Login or Register  to view this content.
    Moderator's note: Please take the time to review our rules. There aren't many, and they are all important. Rule #2 requires code tags. I have added them for you this time because you are a new member. --6StringJazzer



    Kindest regards

    Rob
    Last edited by 6StringJazzer; 05-03-2019 at 06:52 AM.

  2. #2
    Forum Expert Greg M's Avatar
    Join Date
    08-16-2007
    Location
    Dublin. Ireland
    MS-Off Ver
    Office 2016
    Posts
    4,481

    Re: Need help tidying up code

    Hi there,

    It's difficult to suggest/test a proposed solution without access to your workbook, but it looks as if you are performing multiple Copy And Paste operations on blocks of data which are spread uniformly across your worksheet - if that is the case, the following code might do what you need:

    Please Login or Register  to view this content.
    The highlighted values may be changed to suit the layout of your worksheet.

    These rows have been added to the original post.


    Hope this helps - please let me know how you get on.

    Regards,

    Greg M
    Last edited by Greg M; 05-03-2019 at 10:04 AM. Reason: Lines added to VBA code

  3. #3
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 2013
    Posts
    7,846

    Re: Need help tidying up code

    Try: (Untested)
    Please Login or Register  to view this content.
    You can say "THANK YOU" for help received by clicking the Star symbol at the bottom left of the helper's post.
    Practice makes perfect. I'm very far from perfect so I'm still practising.

  4. #4
    Registered User
    Join Date
    03-21-2019
    Location
    Barnsley, England
    MS-Off Ver
    2016
    Posts
    9

    Re: Need help tidying up code

    Many thanks Greg, this does appear to work, can I just ask what the offset is there for? i.e.

    Please Login or Register  to view this content.
    I had to set this to zero as opposed to -1 to get it to work.

    Thanks
    Rob

  5. #5
    Registered User
    Join Date
    03-21-2019
    Location
    Barnsley, England
    MS-Off Ver
    2016
    Posts
    9

    Re: Need help tidying up code

    Hi Mumps1

    This also works for me, as does Gregs, I had almost 30 blocks of my own code to maintain, this has made it much easier, I knew a loop would be the answer but still getting to grips with VBA.

    Thank you both so much for your help here

    Rob

  6. #6
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 2013
    Posts
    7,846

    Re: Need help tidying up code

    Glad to help.

  7. #7
    Registered User
    Join Date
    03-21-2019
    Location
    Barnsley, England
    MS-Off Ver
    2016
    Posts
    9

    Re: Need help tidying up code

    Just getting my head around loops, I have some error checking code in the same program, there are 3 text boxes that if one has a value then all 3 must have a value, not sure if this can be optimised with a loop?

    Please Login or Register  to view this content.
    Rob

  8. #8
    Forum Expert Greg M's Avatar
    Join Date
    08-16-2007
    Location
    Dublin. Ireland
    MS-Off Ver
    Office 2016
    Posts
    4,481

    Re: Need help tidying up code

    Hi again Rob,

    Many thanks for your very prompt feedback and also for the Reputation increase - much appreciated!

    You're quite right - my bad The line should of course read:

    Please Login or Register  to view this content.
    In my defence I'll say that I'd probably have spotted that error if I'd had a copy of your workbook to test it on!

    Just heading out now but will try to take a look at your latest query when I'm back.

    Best regards,

    Greg M

  9. #9
    Forum Expert Greg M's Avatar
    Join Date
    08-16-2007
    Location
    Dublin. Ireland
    MS-Off Ver
    Office 2016
    Posts
    4,481

    Re: Need help tidying up code

    Hi again Rob,

    See if the following code does what you need:

    Please Login or Register  to view this content.
    The highlighted values may be altered if the number of windows or the names of your controls are ever changed.


    Hope this helps - as before, please let me know how you get on.

    Regards,

    Greg M

  10. #10
    Registered User
    Join Date
    03-21-2019
    Location
    Barnsley, England
    MS-Off Ver
    2016
    Posts
    9

    Re: Need help tidying up code

    Thanks for your help once again Greg,

    I will give this a try when I'm back in work on Tuesday, will let you know how I get on.

    Rob

  11. #11
    Forum Expert Greg M's Avatar
    Join Date
    08-16-2007
    Location
    Dublin. Ireland
    MS-Off Ver
    Office 2016
    Posts
    4,481

    Re: Need help tidying up code

    Thanks for that - I look forward to hearing from you next week.

    Greg M

  12. #12
    Registered User
    Join Date
    03-21-2019
    Location
    Barnsley, England
    MS-Off Ver
    2016
    Posts
    9

    Re: Need help tidying up code

    Hi Greg

    I have tested the code you kindly provided, check on sash 1 as an example, the code doesn't appear to check for the qty when data is entered into Txt_Sash1_Width And Txt_Sash1_Height, also I need the code to stop running when conditions are not met, so the user can correct the error, it may be worth noting that my current (untidy) code allows for Txt_Sash1_Width And Txt_Sash1_Height and Combo_Sash1_Qty to be empty.

    Each block i.e. sash1, sash2 and sash3 need to be checked independently.

    I realise you only have a small snippet of the program and appreciate your help.

    Kindest regards
    Rob

    EDIT:

    I have an error check for the quantity which I believe was missing from the original post

    Please Login or Register  to view this content.
    Last edited by robhall1966; 05-07-2019 at 05:52 AM.

  13. #13
    Forum Expert Greg M's Avatar
    Join Date
    08-16-2007
    Location
    Dublin. Ireland
    MS-Off Ver
    Office 2016
    Posts
    4,481

    Re: Need help tidying up code

    Hi again Rob,

    Thanks for your latest feedback - sorry about missing the Quantity check.

    See if the attached workbook does what you need - it uses the following routine for data validation:

    Please Login or Register  to view this content.

    Hope this helps - as always, please let me know how you get on.

    Regards,

    Greg M
    Attached Files Attached Files

  14. #14
    Registered User
    Join Date
    03-21-2019
    Location
    Barnsley, England
    MS-Off Ver
    2016
    Posts
    9

    Re: Need help tidying up code

    Thanks Greg

    The sample workbook you very kindly attached works great, however I am unable to get this to work within my code, I have attached my form which will give you a better overview, the error message should only appear if there has been some text entered into sash1,2 or 3, these all work independently of each other, as per your sample form, the code should also stop after clicking ok on error.

    Thank you for all your help on this, it is much appreciated.

    Kindest regards
    Rob
    Attached Files Attached Files

  15. #15
    Forum Expert Greg M's Avatar
    Join Date
    08-16-2007
    Location
    Dublin. Ireland
    MS-Off Ver
    Office 2016
    Posts
    4,481

    Re: Need help tidying up code

    Hi Rob,

    Many thanks for posting your workbook.

    With luck, I hope to be able to post something here tomorrow.

    Regards,

    Greg M

  16. #16
    Registered User
    Join Date
    03-21-2019
    Location
    Barnsley, England
    MS-Off Ver
    2016
    Posts
    9

    Re: Need help tidying up code

    Much appreciated Greg,

    Rob

  17. #17
    Forum Expert Greg M's Avatar
    Join Date
    08-16-2007
    Location
    Dublin. Ireland
    MS-Off Ver
    Office 2016
    Posts
    4,481

    Re: Need help tidying up code

    Hi again Rob,

    Sorry I didn't manage to post this yesterday - busier day than anticipated!

    Anyway, I think the following is what you need for the Cmd_NewWindow_Click routine:

    Please Login or Register  to view this content.

    I've been playing around with the workbook you posted and have taken the liberty of rewriting parts of it. I hope you'll agree that the attached version of your workbook is a bit simpler and a bit more structured than the original. You can see how adopting a consistent style for naming the controls on your UserForms makes it easier to keep track of them, and also significantly simplifies processing controls when using VBA Code.

    Some of the items you might care to look at in the DataWF UserForm code include populating the dropdown lists of the ComboBoxes, handling the CheckBoxes in the Add-On frame, allowing only numeric input in the dimensions TextBoxes, and how the use of the CounterCell property of the UserForm decouples the UserForm VBA Code from the workbook/worksheets - i.e. the UserForm does not need to "know" either the worksheet or the address of the Counter cell on the Dashboard worksheet.

    Your project was an interesting one for me (which is why I enjoyed playing around with it), and if you feel that I might be able to give you some more assistance with it you can send me a private message and we can continue via email - if not, please feel free to ask me here about any aspect of the code which you do not understand.


    Hope the above helps - as always, please let me know how you get on.

    Regards,

    Greg M
    Attached Files Attached Files

  18. #18
    Forum Expert Greg M's Avatar
    Join Date
    08-16-2007
    Location
    Dublin. Ireland
    MS-Off Ver
    Office 2016
    Posts
    4,481

    Re: Need help tidying up code

    Hi Roy,

    Many thanks for your private message regarding the above.

    I look forward to working further with you on this project.

    Regards,

    Greg M

  19. #19
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2406 (Windows 11 23H2 64-bit)
    Posts
    81,483

    Re: Need help tidying up code

    Administrative Note:

    All questions and answers will benefit other posters when discussed in public threads. The point of having a public forum is to share solutions to common (and sometimes uncommon) problems with all members.

    For this reason, please do not take any of this discussion to private messages. Everything should be shared here. Thanks.
    Ali


    Enthusiastic self-taught user of MS Excel who's always learning!
    Don't forget to say "thank you" in your thread to anyone who has offered you help.
    You can reward them by clicking on * Add Reputation below their user name on the left, if you wish.

    Forum Rules (updated August 2023): please read them here.

+ 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] Tidying up code
    By trobb in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 05-08-2017, 12:49 AM
  2. Tidying up VBA code
    By MrBibby in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-31-2013, 07:29 PM
  3. [SOLVED] Can anyone advise on tidying up code?
    By whitieklf in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 09-30-2013, 09:55 AM
  4. tidying up code
    By tryer in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 12-14-2011, 10:03 AM
  5. code tidying
    By stevesunfold in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 12-26-2008, 05:23 PM
  6. Tidying/ Simplifying Code
    By bomberchia in forum Excel General
    Replies: 6
    Last Post: 11-29-2008, 06:16 AM
  7. Help with tidying up code please
    By Andy in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 11-10-2005, 01:00 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