+ Reply to Thread
Results 1 to 3 of 3

Streamlining settings...by array?

  1. #1
    Forum Contributor beeawwb's Avatar
    Join Date
    01-04-2004
    Location
    Perth, WA, Australia
    MS-Off Ver
    Microsoft Office Excel 2003 (11.8146.8221) SP2
    Posts
    105

    Streamlining settings...by array?

    Hi all,

    Just interested in a bit of discussion at the moment. I've got a portion of my code which reads settings (Variable = Cell) to store them for later use.

    Problem is, I've defined each variable 1 at a time. Now, that's all well and good, but I'm sure there must be a better, more streamlined way of doing things.

    The flow on effect is, if I'm able to streamline this section of code, I can likely turn some other sections into arrays as well.

    The thought that did occur to me would be to store the cell rows / columns in an array, and then use that to get the cell positions...

    E.g.

    Please Login or Register  to view this content.
    The actual code I'm currently using is...

    Please Login or Register  to view this content.
    Any thoughts / comments are greatly appreciated.

    -Bob

  2. #2
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,437
    First a common gotcha in your code.
    Only the last item, DISCODE , is a string variable the rest are variant
    For them all to be string varaibles you would need to add As String to each one.

    Please Login or Register  to view this content.
    You could load your range directly into a variant array, like this,
    Although the locations of your data would mean an array where only certain elements are actually used.

    [code]
    Sub x()

    Dim vntData As Variant
    Dim lngIndex As Long

    vntData = Range("A1:A7")
    For lngIndex = 1 To 7
    Debug.Print vntData(lngIndex, 1)
    Next

    End Sub
    [/vba]

    In order to have a compact array you would still need to have a line of code for each element assignment. Plus the variable name does not help you understand what the code is doing. Make's it very hard to understand and amend at a later date.

    Either use a Type variable or a Class object

    Both will allow you to name the elements with meaningful names.
    They will still need to be loaded line by line but that can be done in a separate routine in order to reduce code clutter in your main entry routine.
    Cheers
    Andy
    www.andypope.info

  3. #3
    Forum Contributor beeawwb's Avatar
    Join Date
    01-04-2004
    Location
    Perth, WA, Australia
    MS-Off Ver
    Microsoft Office Excel 2003 (11.8146.8221) SP2
    Posts
    105
    Hi Andy,

    First, thank you for pointing out the "Gotcha" in my variables. I never realised that they needed to be defined seperate! Looks like I may need to revisit some past projects - they may not be behaving in exactly the ways I expect.

    I'll look into the Type variables and Class objects, but I guess you've hit the nail on the head. At the end of the day, I still need to load each line seperately, so as far as reducing clutter goes, I would make more progress moving things to another module / routine.

    Thanks again for your input, I really appreciate it.

    -Bob

+ 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. Remove duplicates from 1 dimensional array in VBA
    By zaphodb2003 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 11-08-2010, 04:18 AM
  2. Using Vlookup to lookup array, return array
    By jcampbell in forum Excel General
    Replies: 5
    Last Post: 11-16-2007, 01:13 PM
  3. Getting Data From User And Storing Into Array
    By Dan_Dollar in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-13-2007, 06:25 PM
  4. Searching an array
    By hoopz in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-18-2006, 05:46 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