+ Reply to Thread
Results 1 to 8 of 8

Writing Concise VBA

  1. #1
    Registered User
    Join Date
    05-31-2020
    Location
    Tokyo
    MS-Off Ver
    Microsoft Office 365
    Posts
    11

    Writing Concise VBA

    Hi all,

    As a novice in VBA, I was hoping to get some tips on writing more concise code. I have uploaded a sample file with a clunky subroutine I wrote, which I believe must be able to be written more efficiently using loops and novel conventions.
    The code executes as I intend it to, but since I plan to continue adding functionality to this routine, I wanted to start down the 'right' path. Any suggestions would be greatly appreciated.

    The intention of the code is to make forecasts based on historical data.

    Thank you
    Attached Files Attached Files

  2. #2
    Valued Forum Contributor Eastw00d's Avatar
    Join Date
    02-29-2020
    Location
    Breda, NL
    MS-Off Ver
    2016, 2019
    Posts
    833

    Re: Writing Concise VBA

    Hi, I would set all the needed data into an array-table, in this way you can skip all the separate steps to make of every column a single array. Just use an index for the rows and a column in the table. This saves a bit of I/O.
    my two cents:
    Please Login or Register  to view this content.
    Cheers
    Erwin
    I started learning VBA because I was lazy ...
    Still developing.... being more lazy...

  3. #3
    Registered User
    Join Date
    05-31-2020
    Location
    Tokyo
    MS-Off Ver
    Microsoft Office 365
    Posts
    11

    Re: Writing Concise VBA

    Thank you Erwin, I will give your suggestion a go and see if I can implement!

  4. #4
    Valued Forum Contributor Eastw00d's Avatar
    Join Date
    02-29-2020
    Location
    Breda, NL
    MS-Off Ver
    2016, 2019
    Posts
    833

    Re: Writing Concise VBA

    I worked things a little bit out: you can skip the complete declaration of the ranges.
    and end up with something like:
    Please Login or Register  to view this content.
    furthermore, you have to declare this only once:
    Please Login or Register  to view this content.
    is also Year1
    Cheers
    Erwin
    Last edited by Eastw00d; 06-03-2020 at 12:24 AM. Reason: additional information

  5. #5
    Registered User
    Join Date
    05-31-2020
    Location
    Tokyo
    MS-Off Ver
    Microsoft Office 365
    Posts
    11

    Re: Writing Concise VBA

    Erwin, this has helped me immensely in my understanding. Thank you for your kindness!

  6. #6
    Registered User
    Join Date
    05-31-2020
    Location
    Tokyo
    MS-Off Ver
    Microsoft Office 365
    Posts
    11

    Re: Writing Concise VBA

    Quote Originally Posted by Eastw00d View Post
    furthermore, you have to declare this only once:
    Please Login or Register  to view this content.
    is also Year1
    The Forecast_ETS function that I am using requires datatype double. Could I use Year1 or would I need to first convert it to a double?

  7. #7
    Valued Forum Contributor Eastw00d's Avatar
    Join Date
    02-29-2020
    Location
    Breda, NL
    MS-Off Ver
    2016, 2019
    Posts
    833

    Re: Writing Concise VBA

    I would make no issue of it. And if you look in the Locals window of the VBE then you would see that Year1 is a Variant/Double. So there is no difference.
    If you need a Double, use a Double, but use the same variable for all your needs. So in stead of Year1 use dblYear, in the first declaration.

    Cheers
    Erwin

  8. #8
    Registered User
    Join Date
    05-31-2020
    Location
    Tokyo
    MS-Off Ver
    Microsoft Office 365
    Posts
    11

    Re: Writing Concise VBA

    That makes a lot of sense. The Locals window is very helpful too. Thanks for all the advice.

+ 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] Modify code vba is more concise
    By tax112 in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 05-11-2019, 09:46 AM
  2. [SOLVED] Concise way to match last value against any other value in range
    By TheN in forum Excel Formulas & Functions
    Replies: 9
    Last Post: 10-02-2016, 03:05 PM
  3. Cannot figure out concise what to write if-or formula
    By jam320 in forum Excel Formulas & Functions
    Replies: 8
    Last Post: 09-17-2014, 05:06 PM
  4. [SOLVED] Question: Concise List
    By sglick in forum Excel General
    Replies: 3
    Last Post: 05-09-2014, 06:26 AM
  5. making a lookup formula more concise
    By ammartino44 in forum Excel General
    Replies: 3
    Last Post: 02-21-2014, 08:40 PM
  6. making my data more concise
    By chedges in forum Excel General
    Replies: 2
    Last Post: 02-11-2009, 12:22 PM
  7. a more concise formula
    By jwongsf in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 01-15-2009, 10:20 AM

Tags for this Thread

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