Consulting

Results 1 to 4 of 4

Thread: help with select case

  1. #1
    VBAX Regular
    Joined
    Jul 2018
    Posts
    6
    Location

    help with select case

    I'm trying to make a code that traverses a number of rows and returns values from those rows.

    however, according to the attached worksheet, it does not work with Case the way I put it. I must be missing out on something. It works if I use If.

    in case, in cell d1, it should return the value of a16. d2 would return the value of a18, d3 would return the value of a20.
    on the first try i am giving a bad result, returning the value of a20 in d1.

    although with If seems to work, is a bit counterproductive.
    I attached the spreadsheet and the code.
    Attached Files Attached Files

  2. #2
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,728
    Location
    This will not work but is just a example of Case

    Based on the value of the Selector (in Select Case) on of the Case …. statements is selected and associated code executed


    Option Explicit
    
    Sub extrair()
        Dim i As Long
        
        For i = 1 To 20
            Select Case Worksheets("ddp").Cells(i, 1).Value
                
                Case "3.3190.03.01"
                    Worksheets("ddp").Cells(1, 4).Value = Worksheets("ddp").Cells(i, 1).Value
                
               Case "3.3190.03.03"
                    Worksheets("ddp").Cells(2, 4).Value = Worksheets("ddp").Cells(i, 1).Value
            
                Case "3.3190.03.86"
                    Worksheets("ddp").Cells(3, 4).Value = Worksheets("ddp").Cells(i, 1).Value
            
            End Select
        Next i
    End Sub


    I think you're looking for something like this


    Sub OptionalWay()
        Dim r As Range
        Dim i As Long
        Dim v As Variant
        
        
        With Worksheets("ddp")
            
            v = Application.WorksheetFunction.Transpose(.Cells(1, 1).CurrentRegion.Columns(1).Value)
        
            For Each r In .Cells(1, 3).CurrentRegion.Cells
                For i = LBound(v) To UBound(v)
                    If Len(v(i)) > 0 Then
                        If InStr(v(i), r.Value) > 0 Then
                            r.Offset(0, 1).Value = v(i)
                            Exit For
                        End If
                    End If
                Next i
            Next
        End With
    End Sub
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  3. #3
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,876
    I don't think Select Case is a good thing to use here. Try:
    Sub extrair()
    With Worksheets("ddp")
      For Each cll In .Range(.Cells(1, "C"), .Cells(.Rows.Count, "C").End(xlUp)).cells
        For Each celle In .Range(.Cells(1, "A"), .Cells(.Rows.Count, "A").End(xlUp)).cells
          If InStr(1, celle.Value, cll.Value) Then
            cll.Offset(, 1).Value = celle.Value
            Exit For
          End If
        Next celle
      Next cll
    End With
    End Sub
    edit post posting: Ha! just 4 minute after Paul's solution.
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  4. #4
    VBAX Regular
    Joined
    Jul 2018
    Posts
    6
    Location
    thank you my friends.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •