Hi,
I'm writing a vba script where I insert a list of Strings into an Excel file. Then I need to search each of those strings for certain characters (ie. "&", "'"). When a match is found, it copies the string to the adjacent column and concatenates a tagged alternate spelling to is as well. For example, Column A had these two values:
Rock & Roll
Breakfast @ Night
Tiffany's
The output of the program should be this (where A and B are adjacent Columns):
A
Rock & Roll
Breakfast @ Night
Tiffany's
B
Rock & Roll <!Rock and Roll>
Breakfast @ Night <!Breakfast at Night>
Tiffany's <!Tiffanys>
I have developed a program to do this, but it's slow and doesn't always work right. Here are snippets of my code:
Sub Dupe()
Dim Ess As Integer
Dim X As Integer
Dim Y As Integer
Dim W As Integer
Dim Z As Integer
Dim A1 As Integer
Dim B1 As Integer
Dim C1 As Integer
... 'rest of variables taken out...over 100 of these
Dim AD As String
Dim AE As String
Dim NoTag(5000) As String
Dim Tag(5000) As String
Dim Duplicate(5000) As Integer
Dim Rows As Integer
Dim LastRow As Integer
A = "'"
B = "s "
C = "&"
D = "."
E = "-"
... 'Same as above. over 100 declatations
Worksheets("Output").Activate
For X = 2 To Rows
Ess = 0
Tag(X) = NoTag(X)
Z = Len(NoTag(X))
A1 = InStr(1, NoTag(X), A, vbBinaryCompare)
If A1 > 0 _
Then
Work1 = Left(NoTag(X), (A1 - 1))
Work2 = Right(NoTag(X), (Z - A1))
If InStr((A1 + 1), NoTag(X), "s", vbBinaryCompare) = A1 + 1 _
Then
Tag(X) = Tag(X) & " <!" & Work1 & Work2 & ">"
Ess = 1
End If
If Ess = 0 Then Tag(X) = Tag(X) & " <!" & Work1 & Work2 & ">" & " <!" & Work1 & " " & Work2 & ">"
End If
If A1 = 0 _
Then
B1 = InStr(1, NoTag(X), B, vbBinaryCompare)
If B1 > 0 _
Then
Work1 = Left(NoTag(X), (B1 - 1))
Work2 = Right(NoTag(X), (Z - B1 + 1))
Tag(X) = Tag(X) & " <!" & Work1 & "'" & Work2 & ">"
End If
End If
C1 = InStr(1, NoTag(X), C, vbBinaryCompare)
If C1 > 0 _
Then
... 'This continues until It has searched the cell for each variable.
Cells(X, 1) = Tag(X)
Next X
End Sub
Often, I have over 2,000 cells to search, and I know that there has to be a better way to do this. Any help you can give would be appreciated.
I'm writing a vba script where I insert a list of Strings into an Excel file. Then I need to search each of those strings for certain characters (ie. "&", "'"). When a match is found, it copies the string to the adjacent column and concatenates a tagged alternate spelling to is as well. For example, Column A had these two values:
Rock & Roll
Breakfast @ Night
Tiffany's
The output of the program should be this (where A and B are adjacent Columns):
A
Rock & Roll
Breakfast @ Night
Tiffany's
B
Rock & Roll <!Rock and Roll>
Breakfast @ Night <!Breakfast at Night>
Tiffany's <!Tiffanys>
I have developed a program to do this, but it's slow and doesn't always work right. Here are snippets of my code:
Sub Dupe()
Dim Ess As Integer
Dim X As Integer
Dim Y As Integer
Dim W As Integer
Dim Z As Integer
Dim A1 As Integer
Dim B1 As Integer
Dim C1 As Integer
... 'rest of variables taken out...over 100 of these
Dim AD As String
Dim AE As String
Dim NoTag(5000) As String
Dim Tag(5000) As String
Dim Duplicate(5000) As Integer
Dim Rows As Integer
Dim LastRow As Integer
A = "'"
B = "s "
C = "&"
D = "."
E = "-"
... 'Same as above. over 100 declatations
Worksheets("Output").Activate
For X = 2 To Rows
Ess = 0
Tag(X) = NoTag(X)
Z = Len(NoTag(X))
A1 = InStr(1, NoTag(X), A, vbBinaryCompare)
If A1 > 0 _
Then
Work1 = Left(NoTag(X), (A1 - 1))
Work2 = Right(NoTag(X), (Z - A1))
If InStr((A1 + 1), NoTag(X), "s", vbBinaryCompare) = A1 + 1 _
Then
Tag(X) = Tag(X) & " <!" & Work1 & Work2 & ">"
Ess = 1
End If
If Ess = 0 Then Tag(X) = Tag(X) & " <!" & Work1 & Work2 & ">" & " <!" & Work1 & " " & Work2 & ">"
End If
If A1 = 0 _
Then
B1 = InStr(1, NoTag(X), B, vbBinaryCompare)
If B1 > 0 _
Then
Work1 = Left(NoTag(X), (B1 - 1))
Work2 = Right(NoTag(X), (Z - B1 + 1))
Tag(X) = Tag(X) & " <!" & Work1 & "'" & Work2 & ">"
End If
End If
C1 = InStr(1, NoTag(X), C, vbBinaryCompare)
If C1 > 0 _
Then
... 'This continues until It has searched the cell for each variable.
Cells(X, 1) = Tag(X)
Next X
End Sub
Often, I have over 2,000 cells to search, and I know that there has to be a better way to do this. Any help you can give would be appreciated.