Logo 
Search:

MS Office Answers

Ask Question   UnAnswered
Home » Forum » MS Office       RSS Feeds
  Question Asked By: Lloyd Alvarez   on Oct 17 In MS Office Category.

  
Question Answered By: Lonnie Rogers   on Oct 17

Try this: Put the string  with dashes in cell  B2, and the string
without dashes in cell C2.

Then paste the following code into the code window for ThisWorkbook:
:::::::::::::::::::::::::::::
Option Explicit

Public Sub compareTwoStrings()
'Compare the string in cell B2 with the string in cell B3.
'The first two dashes in cell B2 are to be ignored.
'Assume cell B2 is a consistent pattern of 3 substrings
' of length 4, 3 and 3 separated by two dashes.
'Thus the string in cell B2 has length = 12.
'Likewise cell B3 has length = 10.
Dim strFirst As String, strSecond As String
strFirst = Left(Range("$B$2"), 12)
strSecond = Left(Range("$C$2"), 10)
'The split will return an array of the first 3 substrings.
Dim arrSplit
arrSplit = Split(strFirst, Chr(45), 3, vbTextCompare)
Dim ctr As Integer
're-initialize strFirst since arrSplit is storing the substrings.
strFirst = ""
For ctr = 0 To 2 'arrSplit is always zero-based.
'This line concatenates the 3 substrings without the dashes.
strFirst = strFirst & arrSplit(ctr)
Next
'Now compare  the two strings.
Dim intResult As Integer
intResult = StrComp(strFirst, strSecond, vbTextCompare)
If intResult = 0 Then
MsgBox "Comparison exact."
Else
MsgBox "They don't match."
End If
End Sub

Share: 

 

This Question has 5 more answer(s). View Complete Question Thread

 
Didn't find what you were looking for? Find more on checking strings Or get search suggestion and latest updates.


Tagged: