+ Reply to Thread
Results 1 to 7 of 7

VBA - Read Values of Many Cells

  1. #1
    Registered User
    Join Date
    11-23-2005
    Posts
    61

    VBA - Read Values of Many Cells

    Excel 2003 > I am developing a comprehensive VBA function that needs to read the value of many cells in a worksheet. I don't want to Name all those cells because then I will need to pass too many variables to the function. I can use the following set of objects and properties ...
    Please Login or Register  to view this content.
    ... but that's not desirable either because the cell locations are then hard coded. Can someone suggest another solution?
    Last edited by Cecil; 02-25-2009 at 10:04 PM.

  2. #2
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: VBA - Read Values of Many Cells

    There's probably 29 answers to that, depending on what you're trying to do, and you haven't given us a hint.

    A little detail?
    Entia non sunt multiplicanda sine necessitate

  3. #3
    Registered User
    Join Date
    11-23-2005
    Posts
    61

    VBA - Read Values of Many Cells

    Yes, I will try. Here is a code sample. These are just some of the variables I need in this VBA function. I don't want to use Defined Cell Names ( too many variables to pass ) and the sample below shows too many hard coded value locations.

    ' Retrieve Baseline, depending on Cat and Dog
    Select Case Cat
    Case Is = 1
    Select Case Dog
    Case Is = "BARK"
    Baseline = ThisWorkbook.Worksheets("WS_Name").Range("B19").Value
    Case Is = "SCRATCH"
    Baseline = ThisWorkbook.Worksheets("WS_Name").Range("B20").Value
    Case Is = "RUN"
    Baseline = ThisWorkbook.Worksheets("WS_Name").Range("B21").Value
    Case Is = "JUMP"
    Baseline = ThisWorkbook.Worksheets("WS_Name").Range("B22").Value
    Case Else
    MsgBox "Dog Invalid " & Dog, vbOKOnly
    End Select
    Case Is = 2
    Select Case Dog
    Case Is = "BARK"
    Baseline = ThisWorkbook.Worksheets("WS_Name").Range("C19").Value
    Case Is = "SCRATCH"
    Baseline = ThisWorkbook.Worksheets("WS_Name").Range("C20").Value
    Case Is = "RUN"
    Baseline = ThisWorkbook.Worksheets("WS_Name").Range("C21").Value
    Case Is = "JUMP"
    Baseline = ThisWorkbook.Worksheets("WS_Name").Range("C22").Value
    Case Else
    MsgBox "Dog Invalid " & Dog, vbOKOnly
    End Select
    Case Is = 3
    Select Case Dog
    Case Is = "BARK"
    Baseline = ThisWorkbook.Worksheets("WS_Name").Range("D19").Value
    Case Is = "SCRATCH"
    Baseline = ThisWorkbook.Worksheets("WS_Name").Range("D20").Value
    Case Is = "RUN"
    Baseline = ThisWorkbook.Worksheets("WS_Name").Range("D21").Value
    Case Is = "JUMP"
    Baseline = ThisWorkbook.Worksheets("WS_Name").Range("D22").Value
    Case Else
    MsgBox "Dog Invalid " & Dog, vbOKOnly
    End Select
    Case Else
    MsgBox "Cat Invalid " & Cat, vbOKOnly
    End Select

  4. #4
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: VBA - Read Values of Many Cells

    Case Is you need to read the forum rules and add code tags to your code, please.

    I still don't know what you want -- just how to use a range variable from which you can read the contents of various offsets?
    Last edited by shg; 02-25-2009 at 07:10 PM.

  5. #5
    Registered User
    Join Date
    11-23-2005
    Posts
    61

    VBA - Read Values of Many Cells

    Sorry, this is the first time I have inserted a considerable amount of code in one of my threads. I will study the rules to see how it should be done. Next time better.
    ================
    Two things >
    1) Yes, I would like to know how to use a range variable from which I can read the contents of various offsets.
    2) Also, I will re-state the challenge to see if that clarifies things. I am writing a VBA function that needs to input about 20 variables that are stored in cells in a worksheet. Is there an optimum way of passing those variables to the function, other than Naming them all and passing them as individual parameters to the function?

  6. #6
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: VBA - Read Values of Many Cells

    You haven't whittled the possibilities much:
    Please Login or Register  to view this content.
    Last edited by shg; 02-25-2009 at 07:45 PM.

  7. #7
    Registered User
    Join Date
    11-23-2005
    Posts
    61

    Re: VBA - Read Values of Many Cells

    Yes, that is good. Thank you.

+ 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