+ Reply to Thread
Results 1 to 10 of 10

Shorten UserForm_Initialize code

  1. #1
    Forum Contributor
    Join Date
    04-04-2014
    Location
    Tetbury, England
    MS-Off Ver
    Excel 2010
    Posts
    254

    Shorten UserForm_Initialize code

    Hello

    I was wondering if this code could be shortened as it does take a tad too long to open up. The code works perfect atm.

    Please Login or Register  to view this content.

  2. #2
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    A1
    MS-Off Ver
    Most
    Posts
    12,302

    Re: Shorten UserForm_Initialize code

    Just making it shorter won't make it faster. You're doing quite a lot of processing there - I suggest you start by working out where the most time is spent (how much time are we talking anyway?) and then seeing if you can optimise that section of the code - perhaps by loading the data into an array rather than reading one cell at a time.
    Remember what the dormouse said
    Feed your head

  3. #3
    Forum Contributor
    Join Date
    04-04-2014
    Location
    Tetbury, England
    MS-Off Ver
    Excel 2010
    Posts
    254

    Re: Shorten UserForm_Initialize code

    Quick question... do I need all these letters as Integers/Longs? Or can I use one but make the value different each time e.g.

    Please Login or Register  to view this content.

  4. #4
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Shorten UserForm_Initialize code

    Hi,

    Personally I'd examine the overall process and see if there's a different way of achieving your aim. Can't really comment unless we know what that is. Must admit though that it's the longest Initialise procedure I've ever seen.

    First set Application.ScreenUpdating to False and then back to true at the end of the code
    Similarly switch off autocalculation with Application.Calculation = Manual ' switching it back to Automatic at the end

    See if Dimming your Integer Variables as Long makes a difference. I believe in the background VBA has to translate an Integer to a Long anyway so you may as well declare as a Long in the first place.

    Then to shorten the code (but maybe not the time so much) and untested in the absence of the workbook but something like;

    'For all the
    Please Login or Register  to view this content.
    stuff

    Please Login or Register  to view this content.
    Richard Buttrey

    RIP - d. 06/10/2022

    If any of the responses have helped then please consider rating them by clicking the small star icon below the post.

  5. #5
    Forum Contributor
    Join Date
    04-04-2014
    Location
    Tetbury, England
    MS-Off Ver
    Excel 2010
    Posts
    254

    Re: Shorten UserForm_Initialize code

    Thanks Richard, I'll give it a go. But could you tell me the difference between Long and Integer?

  6. #6
    Forum Expert Olly's Avatar
    Join Date
    09-10-2013
    Location
    Darlington, UK
    MS-Off Ver
    Excel 2016, 2019, 365
    Posts
    6,284

    Re: Shorten UserForm_Initialize code

    Each of these sections:
    Please Login or Register  to view this content.
    Can be replaced like:
    Please Login or Register  to view this content.
    But it will be the loops nearer the bottom of the routine which are taking the time to process.
    let Source = #table({"Question","Thread", "User"},{{"Answered","Mark Solved", "Add Reputation"}}) in Source

    If I give you Power Query (Get & Transform Data) code, and you don't know what to do with it, then CLICK HERE

    Walking the tightrope between genius and eejit...

  7. #7
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    A1
    MS-Off Ver
    Most
    Posts
    12,302

    Re: Shorten UserForm_Initialize code

    An Integer can hold a value between -32768 and 32767. A Long (short for Long Integer) can hold between -2,147,483,648 and 2,147,483,647.

  8. #8
    Forum Contributor
    Join Date
    04-04-2014
    Location
    Tetbury, England
    MS-Off Ver
    Excel 2010
    Posts
    254

    Re: Shorten UserForm_Initialize code

    Thanks guys. This is noticeably quicker:

    Please Login or Register  to view this content.

  9. #9
    Forum Expert Olly's Avatar
    Join Date
    09-10-2013
    Location
    Darlington, UK
    MS-Off Ver
    Excel 2016, 2019, 365
    Posts
    6,284

    Re: Shorten UserForm_Initialize code

    Just a quick "caution" on your variable declarations:
    Please Login or Register  to view this content.
    As you have written this, only s is declared as a Long, and ws8 as a Worksheet. All your other variables are variants, by default.

    You need to define the variable type of each variable individually, to apply that variable type - so your declaration lines would become:
    Please Login or Register  to view this content.

  10. #10
    Forum Contributor
    Join Date
    04-04-2014
    Location
    Tetbury, England
    MS-Off Ver
    Excel 2010
    Posts
    254

    Re: Shorten UserForm_Initialize code

    That's annoying! But thanks for the heads up

+ 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. please help shorten this code
    By newbiewexcel in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 11-22-2013, 11:38 PM
  2. [SOLVED] Very inefficient code because of different sizes of arrays, how do i shorten my code?
    By Brammer88 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 12-28-2012, 04:49 PM
  3. How can I shorten this VBA code?
    By hemi_fan in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 01-22-2010, 11:53 AM
  4. How to shorten the code?
    By joyce_88 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 10-29-2008, 06:37 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