+ Reply to Thread
Results 1 to 3 of 3

VBA switch statement with a twist?

  1. #1
    Registered User
    Join Date
    06-11-2012
    Location
    United Kingdom
    MS-Off Ver
    Excel 2010
    Posts
    2

    VBA switch statement with a twist?

    Hi guys,

    I have a question regarding the use of VBA switch statements, any help would be very appreciated, I'm still getting to know the language.

    Essentially, I have a column of integer values which I would like to iterate through. The integers in the column vary from 12 to -12 with the exception of 0, so 24 possible integer values here.
    For each possible value, I would like the activecell.offset(0,7) value added to a "total" variable.
    So if I iterate from cell A1 to cell A10, each time a value of "1" appears, I would like the cell 7 spaces to the right of the "1" cell added to a "total" variable for "1" e.g.: "totalOne".
    The same applies for every value within my range. So if there are three instances of the value "8" between A1 and A10, then each value 7 cells right of those "8" values will be added to a total value for the number 8.

    I started to use:

    select case range("u4:u150"). value

    case 1
    Dim value1 = value1 + ActiveCell.offset(0, 7).Value


    However, at this point I realised that a switch statement stores the range I have given it as an array, so my 'case 1' cannot work as it is trying to compare an integer with an array.
    Finally, if it stores an array, surely it wouldn't be able to get cell.offset values as it would have no references to those locations given that the array is just a group of integers, not actual cell references? Will I have to use lots of "for each" statements? I hope not!

    Thanks guys for your time. Any questions, please ask.
    Last edited by pwillia; 06-12-2012 at 09:43 AM.

  2. #2
    Registered User
    Join Date
    06-11-2012
    Location
    United Kingdom
    MS-Off Ver
    Excel 2010
    Posts
    2

    Re: VBA switch statement with a twist?

    can anybody give me a hint here? I'm really struggling to push forward, thanks again.

  3. #3
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: VBA switch statement with a twist?

    it seems to me that it would be as easy to use a SUMIF formula (or use the function code). If you want a loop you just need one for each statement to iterate through the cells once.
    Josie

    if at first you don't succeed try doing it the way your wife told you to

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

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