Results 1 to 8 of 8

Compare ranging part of a string in two columns

Threaded View

  1. #1
    Registered User
    Join Date
    11-03-2011
    Location
    Amsterdam, The Netherlands
    MS-Off Ver
    Excel 2007
    Posts
    4

    Compare ranging part of a string in two columns

    Hi I'm new to all of this but I'm trying to compare the last part of two strings. In column A and B there are two lists of data all build-up in the same manner 'ID-code [department] description".

    example
    In column A : "TEST-79 [BA] This is a use case of a function"
    In column B: "TEST-1889 [TEST] This is a use case of a function"

    I want the example data to be matched on the last part of the string (This is a use case of a function) but the problem is the last part can have a length of 5 to 50 characters. The last character I want to be ignored is always "]".

    I already have some code but I can imagine it cant be used for the function I want, also the selection method of column A and B can be different but its ok for me as it is.

    Sub ComparTwoColumns()
    
    Dim Column1 As Range
    Dim Column2 As Range
      
      'Prompt user for the first column range to compare...
      '----------------------------------------------------
        Set Column1 = Application.InputBox("Select First Column to Compare", Type:=8)
        
        'Check that the range they have provided consists of only 1 column...
        If Column1.Columns.Count > 1 Then
        
          Do Until Column1.Columns.Count = 1
          
            MsgBox "You can only select 1 column"
            Set Column1 = Application.InputBox("Select First Column to Compare", Type:=8)
            
          Loop
          
        End If
       
      'Prompt user for the second column range to compare...
      '----------------------------------------------------
        Set Column2 = Application.InputBox("Select Second Column to Compare", Type:=8)
        
        'Check that the range they have provided consists of only 1 column...
        If Column2.Columns.Count > 1 Then
        
          Do Until Column2.Columns.Count = 1
          
            MsgBox "You can only select 1 column"
            Set Column2 = Application.InputBox("Select Second Column to Compare", Type:=8)
            
          Loop
          
        End If
        
      
      'Check both column ranges are the same size...
      '---------------------------------------------
      If Column2.Rows.Count <> Column1.Rows.Count Then
      
        Do Until Column2.Rows.Count = Column1.Rows.Count
        
          MsgBox "The second column must be the same size as the first"
          Set Column2 = Application.InputBox("Select Second Column to Compare", Type:=8)
          
        Loop
        
      End If
      
      'If entire columns have been selected (e.g. $AA), limit the range sizes to the
      'UsedRange of the active sheet. This stops the routine checking the entire sheet
      'unnecessarily.
      '-------------------------------------------------------------------------------
      If Column1.Rows.Count = 65536 Then
    
    
        Set Column1 = Range(Column1.Cells(1), Column1.Cells(ActiveSheet.UsedRange.Rows.Count))
        Set Column2 = Range(Column2.Cells(1), Column2.Cells(ActiveSheet.UsedRange.Rows.Count))
    
    
      End If
      
      
      'Perform the comparison and copy cells
      '----------------------------------------------------------------
      Dim intCell_1 As Long
      Dim intCell_2 As Long
      
      For intCell_1 = 1 To Column1.Rows.Count
        
        For intCell_2 = 1 To Column2.Rows.Count
            
            'working code to compare the same values
            If Column1.Cells(intCell_1) = Column2.Cells(intCell_2) Then
              
              Column1.Cells(intCell_1).Select
                Selection.Copy
                Range("C65536").End(xlUp).Offset(1).Select
                ActiveSheet.Paste
              
              Column2.Cells(intCell_2).Select
                Selection.Copy
                Range("D65536").End(xlUp).Offset(1).Select
                ActiveSheet.Paste
        
                Application.CutCopyMode = False
              
            End If
        Next
      Next
      
    End Sub
    Last edited by Jeroen79; 11-04-2011 at 09:54 AM.

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