Advertisement
Bloodrat

Comprarátor

May 11th, 2023
650
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VBScript 1.55 KB | Source Code | 0 0
  1. Sub CompareAndWrite()
  2.  
  3.  
  4.     Dim rowNum As Integer
  5.     Dim strA As String, strB As String
  6.     Dim wordsA() As String, wordsB() As String
  7.     Dim matches As String
  8.     Dim foundMatch As Boolean
  9.    
  10.     rowNum = 1 ' Starting row number
  11.    
  12.     Do While Range("A" & rowNum).Value <> "" ' Loop until an empty cell is found in column A
  13.        ' Get the values from cells A(rowNum) and B(rowNum)
  14.        strA = Range("A" & rowNum).Value
  15.         strB = Range("B" & rowNum).Value
  16.        
  17.         ' Split the strings into an array of words
  18.        wordsA = Split(strA, " ")
  19.         wordsB = Split(strB, " ")
  20.        
  21.         ' Initialize the variables
  22.        matches = ""
  23.         foundMatch = False
  24.        
  25.         ' Loop through each word in the first string
  26.        For Each wordA In wordsA
  27.             ' Check if the word exists in the second string and is longer then 2 characters
  28.            If InStr(1, strB, wordA, vbTextCompare) > 0 And Len(wordA) > 2 Then
  29.                 ' Found a matching word/sentence
  30.                If matches <> "" Then
  31.                     matches = matches & " "
  32.                 End If
  33.                 matches = matches & wordA
  34.                 foundMatch = True
  35.             End If
  36.         Next wordA
  37.        
  38.         ' Write the matches into the corresponding cell in column C
  39.        If foundMatch Then
  40.             Range("C" & rowNum).Value = matches
  41.         Else
  42.             Range("C" & rowNum).Value = "výraz_nenalezen"
  43.         End If
  44.        
  45.         rowNum = rowNum + 1 ' Move to the next row
  46.    Loop
  47. End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement