+ Reply to Thread
Results 1 to 4 of 4

Inefficient code

  1. #1
    Forum Contributor
    Join Date
    08-20-2005
    Posts
    173

    Inefficient code

    Hi,

    The code below loops through a list of values, finds each one and assigns the column location to an associated variable. The code works but is hugely inefficient, would be grateful if someone could provide something more flexible and efficient, would help going fwd, thanks in advance.


    Please Login or Register  to view this content.

  2. #2
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689
    I use an enumeration to do this.

    As applications are developed, the positions of particular columns in tables may change as columns are added, deleted, or rearranged. Hard-coding the column positions in VBA makes it very difficult to accommodate such changes. An alternative is to use a private enumeration at the top of the module that operates on the sheet, like this
    Please Login or Register  to view this content.
    Then in the body of the code,
    Please Login or Register  to view this content.
    If not specified, the value of the first item in an enumeration is zero, so the xxx is just an unused placeholder so I don't have to explicitly set the value of the first column to 1 (in the example, Desc = 1). If the first column of the table were D, change the first item to xxx = 3.

    These good things happen:
    • IntelliSense works; type Col. and you see a list of column names after you type the period
    • If the column numbers get rearranged, just change the order of appearance in the enumeration to match
    • Because the scope of the enumeration is limited to the particular module, you can use the same enumeration name in different modules, with different item names (or some of the same item names in different positions, if appropriate)
    Entia non sunt multiplicanda sine necessitate

  3. #3
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,960
    What's the relationship between the array and your worksheet layout??...
    Ben Van Johnson

  4. #4
    Forum Contributor
    Join Date
    08-20-2005
    Posts
    173
    Hi,

    the values in the array relate to column headers per attached.

    Regards
    Attached Files Attached Files

+ 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