+ Reply to Thread
Results 1 to 13 of 13

Case Statements

  1. #1
    Registered User
    Join Date
    07-03-2008
    Location
    Cresskill, NJ
    Posts
    7

    Case Statements

    Hi, I was wondering if anybody could help me with macro I am trying to build. I know the concept of Case statements sounds simple, but trying to write it in my scenario seems a lot more difficult than I thought it would be. I have three columns with an unspecified number of rows (which are generated from another macro). Lets say those colums are C, D, E, and they should be strings because my criteria are based on the number of digits contained in each cell. I want to add a certain number of periods to a given cell, then add that string onto two other cell strings, and form one combined string in a separate cell.

    Basically I want to sort of Concatenate the cells into a given cell in column F for each row, but there are many conditions. If the cells in column C contain 3 digits, I want to add one period "." onto the original string. If it has 4 digits, then I add nothing, and then, add that cell to the string in cell D, and then Cell E. [It is like the function Concatenate(C1&".",D1&"..",E1)]. Cells in Column D have 6 criteria, and cells in Column E have zero criteria.

    And I need this to loop down until it reaches an empty row or cells.

    I would sooo much appreciate any help anyone has. Thank you so much Excel Forum. You have helped me enormously in general.
    -Arthur

  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
    Welcome to the forum.

    Posting an example and explaining in context would help ...

  3. #3
    Registered User
    Join Date
    07-03-2008
    Location
    Cresskill, NJ
    Posts
    7
    Conditions:
    Cells in Column C
    if len(selection) = 3, do cell.selection & "."
    if len(selection) = 4, do cell selection & ""
    Cells in Column D
    if len(selection) = 2, do cell.selection & "....."
    if len(selection) = 3, do cell.selection & "...."
    if len(selection) = 4, do cell.selection & "..."
    if len(selection) = 5, do cell selection & ".."
    if len(selection) = 6, do cell.selection & "."
    if len(selection) = 7, do cell.selection & ""
    Cells in Column E
    '(no conditions)
    I need a macro to run through Column F that will concatenate the cells in columns C,D,E based on those conditions. The only problem is, I dont know how to write the code and only know the concatenate function and limited VBA. Any help? What commands do I use to a) combine the cell strings and b) do so based on these conditions?

    I would appreciate any knowledge anyone might have. Thank you.
    -Arthur Nazarian

  4. #4
    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 meant a workbook ...

  5. #5
    Forum Expert
    Join Date
    01-03-2006
    Location
    Waikato, New Zealand
    MS-Off Ver
    2010 @ work & 2007 @ home
    Posts
    2,243
    hi,

    As Shg says, you're more likely to get useful answers when some context is provided in the form of a sample workbook with sample data & expected outcomes. Here's a rough attempt without this context:

    Please Login or Register  to view this content.
    hth
    Rob
    Rob Brockett
    Kiwi in the UK
    Always learning & the best way to learn is to experience...

  6. #6
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689
    You might use VBA's native String function in lieu of
    Please Login or Register  to view this content.

  7. #7
    Registered User
    Join Date
    07-03-2008
    Location
    Cresskill, NJ
    Posts
    7
    Sorry about not providing the example. My workbook was on my computer at work and I was at home for the weekend. This is what I thought would work, but it doesnt. Going to look at what Broro and shg just suggested. I am a novice and all this stuff is new to me so...
    -Arthur

    Please Login or Register  to view this content.
    Last edited by anazarian; 07-07-2008 at 11:51 AM.

  8. #8
    Registered User
    Join Date
    07-03-2008
    Location
    Cresskill, NJ
    Posts
    7
    Here is the file attached showing the cells if anyone wants a better look of what I'm going for.
    Attached Files Attached Files

  9. #9
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689
    Please Login or Register  to view this content.
    There is no Length property for a range. Did you try Rob's code?

  10. #10
    Registered User
    Join Date
    07-03-2008
    Location
    Cresskill, NJ
    Posts
    7
    I tried Rob's code and it wasnt what I was looking for. BUT, I did find it. I had to create three variables, but it works great! This is the first real VBA code that I've written (i.e. without the help of the recorder or other peoples' code). I have never been so happy to see a period appear like magic. I feel like a magician. Now I need to try to figure out how to make the cells referential and to add a loop.

    Please Login or Register  to view this content.
    I don't know why this inserting the values into Cell F1. I think I am going to put a DoWhileNot isEmpt(Activecell.offset(0,1)), loop.

    When using ActiveCell and Offset selections, how do you define where the selection begins? If I want it to start and return values in cell F1 and down until there is an empty cell, how do write that?

  11. #11
    Registered User
    Join Date
    07-03-2008
    Location
    Cresskill, NJ
    Posts
    7
    Correction, it the loop needs to loop until the cells it refers to are empty

  12. #12
    Forum Expert
    Join Date
    01-03-2006
    Location
    Waikato, New Zealand
    MS-Off Ver
    2010 @ work & 2007 @ home
    Posts
    2,243
    hi Arthur,

    Yay! feeling like a magician is something we all like

    Unfortunately, I only have Excel 2003 so I can't open your file. Also, I didn't explain how to use my code (or include the concatenation section for column F)). So here's a modified version, to let me feel like a magician ...
    This version relies on you selecting the range C?:F? before you run the macro.

    Please Login or Register  to view this content.
    This can be modified to run for all rows of your data (rather than a selection), assuming your data is continuous with no blank rows, by changing it to:
    (also contains some additional coding)

    Please Login or Register  to view this content.
    Right, that's my magic trick (hopefully;-))

    Now to answer your questions & help you learn:

    Now I need to try to figure out how to make the cells referential and to add a loop.
    Define the current (for want of a better word) cell at the start of the loop (I used a "for each" statement), & then use a "with" statement to link the subsequent references to the object/range etc by removing the initial expression & beginning the them with a dot. For example:
    Please Login or Register  to view this content.
    I don't know why this inserting the values into Cell F1. I think I am going to put a DoWhileNot isEmpt(Activecell.offset(0,1)), loop.
    This is inserting the info into cell F1 because of:
    Please Login or Register  to view this content.
    I may be wrong but I think that "dowhilenot" code can be slower than loops using "for i = 1 to lastrow" or "for each cell in rangetocheck". My reasoning is that the latter codes only perform a single calculation whereas the former performs a calculation each time it loops.


    When using ActiveCell and Offset selections, how do you define where the selection begins?
    Rather than using
    Please Login or Register  to view this content.
    try using either
    Please Login or Register  to view this content.
    or
    Please Login or Register  to view this content.
    to define where it starts.

    If I want it to start and return values in cell F1 and down until there is an empty cell (in col c), how do write that?
    Check out the use of "Lastrow" in my above code. This chooses the last row of column C & then goes upwards until it finds a value & uses that row number.


    hth
    Rob

  13. #13
    Forum Expert
    Join Date
    01-03-2006
    Location
    Waikato, New Zealand
    MS-Off Ver
    2010 @ work & 2007 @ home
    Posts
    2,243
    Ooopps!

    Thanks Shg for pointing out the String function - I knew there must be a tidier way but I couldn't find it at a quick glance in the ol' Help files :-)

    Rob

+ 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