Consulting

Results 1 to 13 of 13

Thread: How to compare two Strings and do some find-copy-paste operation

  1. #1

    How to compare two Strings and do some find-copy-paste operation

    Hi,

    I want to compare the names on Sheet1 (listed vertically) with the names on Sheet2 (listed horizontally). On Sheet2 every Name has numbers listed below. If there is a match then the 2nd smallest number should be copied next to the Name on Sheet1.

    Here is my code so far:

    Sub Match()
    Dim wb As Workbook
    Dim i As Integer
    Dim number As Integer
    Dim find As String
    Dim name As String
    Dim a As Long
    Set wb = ThisWorkbook
    number = Sheets(Sheet2).Cells(1, Columns.Count).End(xlToLeft).Column 
    For i = 1 To number
    such = Sheets(Sheet2).Cells(6 + i, 4).Value
    wert = Sheets(Sheet1).Cells(1, 1 + i).Value
    
    If find = name Then
     
    '--> go to Cell with the Header "name" and copy the 2nd smallest number in the column and paste it on Sheet1 next to "find" 
    
    End If
    Next i
    
        
        
        Application.ScreenUpdating = False
        Application.Calculation = xlCalculationManual
        
       
     
    End Sub

  2. #2
    Remark: "such" should be "find" and "wert" should be "name"

  3. #3

  4. #4
    Oder
    "wert" should be "value"

  5. #5
    Maybe
    Sub So_Etwas()
        Dim sh2 As Worksheet, lr As Long, c As Range, x As Long, y As Long
        Set sh2 = Sheets("Sheet2")
        lr = Cells(Rows.Count, 1).End(xlUp).Row
        For Each c In Range("A1:A" & lr)
            On Error Resume Next
            x = sh2.Rows(1).Find(c.Value, , , 1, xlByColumns, xlPrevious).Column
            With sh2
                y = WorksheetFunction.Small(.Range(.Cells(2, x), .Cells(.Cells(.Rows.Count, x).End(xlUp).Row, x)), 2)
            End With
            On Error GoTo 0
            c.Offset(, 1).Value = y
        Next c
    End Sub

  6. #6
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    For your consideration with absolutely required changes
    Sub Match() 
        Dim i As Long 'Integer :Row and column variables should be Long
        Dim number As Long 'Integer 
        Dim Found As String 'find As String :Find is a VBA and Excel Function
        Dim Nomen As String 'name As String :Name is an Excel Object and Property
        Dim a As Long 
    
        number = Sheets(Sheet2).Cells(1, Columns.Count).End(xlToLeft).Column 
    
    'Add before code
        Application.ScreenUpdating = False 
        Application.Calculation = xlCalculationManual 
    
        For i = 1 To number 
            such = Sheets(Sheet2).Cells(6 + i, 4).Value 
            wert = Sheets(Sheet1).Cells(1, 1 + i).Value 
             
            If found = nomen Then              
                 '--> go to Cell with the Header "nomen" and copy the 2nd smallest number in the column and paste it on Sheet1 next to "find"
            End If 
        Next i 
    
    'Reset after code
        Application.ScreenUpdating = True 'False 
        Application.Calculation = xlCalculationAutomatic 'xlCalculationManual 
    End Sub
    What are these lines for?
           such = Sheets(Sheet2).Cells(6 + i, 4).Value 
            wert = Sheets(Sheet1).Cells(1, 1 + i).Value
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  7. #7
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    All my help files are on a broken computer, this is as far as I can get.
    Sub Match_Code_Started()
    Dim Cel As Range
    Dim Found As Range
    Dim Rw As Long
    
    Application.ScreenUpdating = False
    Application.Calculation = xlCalculationManual
     
    With Sheets("Sheet2")
                 
      For Each Cel In Sheets("Sheet1").Range(Cells(1, "A"), Cells(Rows.Count, "A").End(xlUp)) 'Uses Column a
       Set Found = .Range("1:1").Find(Cel) 'uses Row 1
                 
         If Not Found Is Nothing Then
          With .Columns(Found.Column)
                 '--> Cel.Offset(, 1) = Second smallest number
                 
          
          End With 'Found.Column
         End If
      Next Cel
    End With 'Sheet2
         
    Application.ScreenUpdating = True '
    Application.Calculation = xlCalculationAutomatic
    End Sub
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  8. #8
    Hi jolivanes,

    that's it. you are great thank you.

  9. #9
    Hi SamT,

    thank you for your help and advice. I will Combine your help with jolivanes code. Thank you !

  10. #10
    Thank you for letting us know that you were helped with it.
    Also, I am sure, from SamT

  11. #11
    Hi jolivanes,

    what does actually the command y = WorksheetFunction.Small(.Range(.Cells(2, x), .Cells(.Cells(.Rows.Count, x).End(xlUp).Row, x)), 2) does? Why do we have Range(.Cells(2 , x)) ?

  12. #12
    That line finds the 2nd lowest number in the used range below the found value.

  13. #13
    thank you

Posting Permissions

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