+ Reply to Thread
Results 1 to 13 of 13

Case Statements

Hybrid View

anazarian Case Statements 07-03-2008, 04:32 PM
shg Welcome to the forum. ... 07-03-2008, 04:38 PM
anazarian Conditions: Cells in Column... 07-07-2008, 01:07 AM
shg I meant a workbook ... 07-07-2008, 01:20 AM
broro183 hi, As Shg says, you're... 07-07-2008, 04:19 AM
shg You might use VBA's native... 07-07-2008, 11:04 AM
anazarian Sorry about not providing the... 07-07-2008, 11:48 AM
anazarian Here is the file attached... 07-07-2008, 02:04 PM
shg Select Case... 07-07-2008, 02:07 PM
  1. #1
    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 ...

  2. #2
    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:

    Sub EgOfCaseStatements()
    Dim cll As Range
    Dim DesiredLength As Long
    For Each cll In Selection
        With cll
            Select Case .Column
                Case 3
                    DesiredLength = 4
                Case 4
                    DesiredLength = 7
                Case Else
                    DesiredLength = Len(.Value)
            End Select
            '(needs tweaking) _
                .Value = .Value & Application.WorksheetFunction.Rept(".", DesiredLength & -Len(.Value))
            'seems to work
            .Value = .Value & Application.Evaluate("=REPT("".""," & DesiredLength & -Len(.Value) & ")")
        End With
    Next cll
    End Sub
    hth
    Rob
    Rob Brockett
    Kiwi in the UK
    Always learning & the best way to learn is to experience...

  3. #3
    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
    Application.WorksheetFunction.Rept(...)

  4. #4
    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

    Sub SkuBuilder()
    'SkuBuilder Macro
    'Keyboard Shortcut: ctrl+s
    
    Dim Design As String
    Dim Color As String
    Dim Size As String
    
    Design = Range("C1").Value
    Select Case Range("C1").Length
        Case 3
            Range("C1").Value = Range("C1").Value & "."
    End Select
    
    Color = Range("D1").Value
    Select Case Range("D1").Length
        Case 2
            Range("D1").Value = Range("D1").Value & "....."
        Case 3
            Range("D1").Value = Range("D1").Value & "...."
        Case 4
            Range("D1").Value = Range("D1").Value & "..."
        Case 5
            Range("D1").Value = Range("D1").Value & ".."
        Case 6
            Range("D1").Value = Range("D1").Value & "."
        Case 7
            Range("D1").Value = Range("D1").Value & ""
    End Select
    
    Size = Range("E1").Value
    
    Do While Not IsEmpty(ActiveCell.Offset(0, 1))
    Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
    ActiveCell.Offset(1, 0).Range("F1").Select
    ActiveCell.Value = "Design" & "Color" & "Size"
    Loop
    End Sub
    Last edited by anazarian; 07-07-2008 at 11:51 AM.

  5. #5
    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

  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
    Select Case Range("D1").Length
    There is no Length property for a range. Did you try Rob's code?

  7. #7
    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.

    Dim Design As String
    Dim Color As String
    Dim Size As String
    Dim SKUDesign As String
    Dim SKUColor As String
    Dim CharcountD As Integer
    Dim CharcountC As Integer
    
    CharcountD = Len(Range("C1"))
    Design = CharcountD
    
    Select Case Design
        Case 3
            SKUDesign = Range("C1").Value & "."
        Case Else
            SKUDesign = Range("C1").Value
    End Select
    
    
    CharcountC = Len(Range("D1"))
    Color = CharcountC
    Select Case Color
        Case 2
            SKUColor = Range("D1").Value & "....."
        Case 3
            SKUColor = Range("D1").Value & "...."
        Case 4
            SKUColor = Range("D1").Value & "..."
        Case 5
            SKUColor = Range("D1").Value & ".."
        Case 6
            SKUColor = Range("D1").Value & "."
        Case Else
            SKUColor = Range("D1").Value
    End Select
    
    Size = Range("E1").Value
    
    ActiveCell.Range("F1").Select
    ActiveCell.Value = SKUDesign & SKUColor & Size
    
    End Sub
    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?

+ 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