Я є початківцем початком в обох програмах, а також на цих форумах, і намагаюся скласти разом код, який дозволив би користувачеві оновити стовпчик текстових записів. В основному те, що я намагався зробити, це:
запропонувати користувачеві вибрати діапазон
починати з першої комірки в діапазоні та оновлювати текст на основі кодування.
замініть текст у цій комірці на "чистий" текст
перейти до наступної комірки в діапазоні, зробити те саме, що №3
зупинка в кінці вибраного діапазону.
Sub MultiFindNReplace()
Dim InputRng As Range, ReplaceRng As Range
Dim strOld As String
Dim intPosition As Integer
Dim c As Integer
Dim CountofRows As Integer
xtitleId = "Name Update"
Set InputRng = Application.Selection
Set InputRng = Application.InputBox("Labels to be updated ", xtitleId, InputRng.Address, Type:=8)
CountofRows = InputRng.Rows.Count
MsgBox CountofRows & " rows Selected"
For c = 1 To CountofRows
strOld = ActiveCell.Value
'Replace " .COM" with a space
For i = 1 To Len(strOld)
intPosition = InStr(1, strOld, " .COM", vbTextCompare)
If intPosition > 0 Then
strOld = Left(strOld, intPosition - 1) & Right(strOld, (Len(strOld) - (intPosition + 4)))
End If
Next i
'Replace ".COM" with a space
For i = 1 To Len(strOld)
intPosition = InStr(1, strOld, ".COM", vbTextCompare)
If intPosition > 0 Then
strOld = Left(strOld, intPosition - 1) & Right(strOld, (Len(strOld) - (intPosition + 3)))
End If
Next i
'Replace " INC." with a space
For i = 1 To Len(strOld)
intPosition = InStr(1, strOld, " INC.", vbTextCompare)
If intPosition > 0 Then
strOld = Left(strOld, intPosition - 1) & Right(strOld, (Len(strOld) - (intPosition + 4)))
End If
Next i
'Replace " LTD " with a space
For i = 1 To Len(strOld)
intPosition = InStr(1, strOld, " LTD ", vbTextCompare)
If intPosition > 0 Then
strOld = Left(strOld, intPosition - 1) & " " & Right(strOld, (Len(strOld) - (intPosition + 4)))
End If
Next i
'Replace "INC." with a space
'For i = 1 To Len(strOld)
' intPosition = InStr(1, strOld, ".COM", vbTextCompare)
' If intPosition > 0 Then
' strOld = Left(strOld, intPosition - 1) & Right(strOld, (Len(strOld) - (intPosition + 3)))
' End If
'Next i
'Remove trailing ", LA"
If Right(strOld, 4) = ", LA" Then strOld = Replace(strOld, ", LA", "")
'Remove trailing ",LA"
If Right(strOld, 3) = ",LA" Then strOld = Replace(strOld, ",LA", "")
'Remove "," (comma)
For i = 1 To Len(strOld)
intPosition = InStr(1, strOld, ",", vbTextCompare)
If intPosition > 0 Then
strOld = Left(strOld, intPosition - 1) & Right(strOld, (Len(strOld) - (intPosition)))
End If
Next i
'Remove trailing " LTÉE"
If Right(strOld, 5) = " LTÉE" Then strOld = Replace(strOld, " LTÉE", "")
'Remove trailing " LTÉE."
If Right(strOld, 6) = " LTÉE." Then strOld = Replace(strOld, " LTÉE.", "")
'Remove trailing " LIMITÉE"
If Right(strOld, 8) = " LIMITÉE" Then strOld = Replace(strOld, " LIMITÉE", "")
'Remove trailing " LTD."
If Right(strOld, 5) = " LTD." Then strOld = Replace(strOld, " LTD.", "")
'Remove trailing " CORP."
If Right(strOld, 6) = " CORP." Then strOld = Replace(strOld, " CORP.", "")
'Remove trailing " CO."
If Right(strOld, 4) = " CO." Then strOld = Replace(strOld, " CO.", "")
'Remove trailing " INCORPORATION"
If Right(strOld, 14) = " & CO" Then strOld = Replace(strOld, " INCORPORATION", "")
'Remove trailing " & CO"
If Right(strOld, 5) = " & CO" Then strOld = Replace(strOld, " & CO", "")
'Remove trailing " AND CO"
If Right(strOld, 7) = " AND CO" Then strOld = Replace(strOld, " AND CO", "")
'Remove trailing " & CO."
If Right(strOld, 6) = " & CO." Then strOld = Replace(strOld, " & CO.", "")
'Remove trailing " CO. LTD"
If Right(strOld, 8) = " CO. LTD" Then strOld = Replace(strOld, " CO. LTD", "")
'Remove trailing " & CO INC"
If Right(strOld, 9) = " & CO INC" Then strOld = Replace(strOld, " & CO INC", "")
'Remove trailing " & CO., INC."
If Right(strOld, 12) = " & CO., INC." Then strOld = Replace(strOld, " & CO., INC.", "")
'Remove trailing " CO., INC."
If Right(strOld, 10) = " CO., INC." Then strOld = Replace(strOld, " CO., INC.", "")
'Remove trailing " CO (INC)"
If Right(strOld, 9) = " CO (INC)" Then strOld = Replace(strOld, " CO (INC)", "")
'Replace "&" with "AND"
For i = 1 To Len(strOld)
intPosition = InStr(1, strOld, "&", vbTextCompare)
If intPosition > 0 Then
strOld = Left(strOld, intPosition - 1) & "AND" & Right(strOld, (Len(strOld) - (intPosition)))
End If
Next i
'Replace "-" (hyphen) with a space
For i = 1 To Len(strOld)
intPosition = InStr(1, strOld, "-", vbTextCompare)
If intPosition > 0 Then
strOld = Left(strOld, intPosition - 1) & " " & Right(strOld, (Len(strOld) - (intPosition)))
End If
Next i
'Remove leading or trailing "THE"
If Left(strOld, 4) = "THE " Then strOld = Replace(strOld, "THE ", "")
If Left(strOld, 6) = "(THE) " Then strOld = Replace(strOld, "(THE) ", "")
If Right(strOld, 4) = " THE" Then strOld = Replace(strOld, " THE", "")
If Right(strOld, 6) = " (THE)" Then strOld = Replace(strOld, " (THE)", "")
'Remove leading or trailing "LE"
If Left(strOld, 3) = "LE " Then strOld = Replace(strOld, "LE ", "")
If Left(strOld, 5) = "(LE) " Then strOld = Replace(strOld, "(LE) ", "")
If Right(strOld, 4) = " LE" Then strOld = Replace(strOld, " LE", "")
'Remove leading or trailing "LES"
If Left(strOld, 4) = "LES " Then strOld = Replace(strOld, "LES ", "")
If Left(strOld, 6) = "(LES) " Then strOld = Replace(strOld, "(LES) ", "")
If Right(strOld, 4) = " LES" Then strOld = Replace(strOld, " LES", "")
'Remove leading "LA "
If Left(strOld, 3) = "LA " Then strOld = Replace(strOld, "LA ", "")
If Left(strOld, 5) = "(LA) " Then strOld = Replace(strOld, "(LA) ", "")
'Remove leading "(L') "
If Left(strOld, 5) = "(L') " Then strOld = Replace(strOld, "(L') ", "")
'Remove trailing " LTD", " INC", " SVC", " CTR", " LIMITED", " LIMITED PARTNERSHIP",
'" CO", " LT", " MD", " OD", " THE CO LTD", " LTEE", " LTEE CORP", " CORP", " INCORPORATED"
If Right(strOld, 4) = " LTD" Then strOld = Replace(strOld, " LTD", "")
If Right(strOld, 4) = " INC" Then strOld = Left(strOld, (Len(strOld) - 4))
If Right(strOld, 4) = " SVC" Then strOld = Replace(strOld, " SVC", "")
If Right(strOld, 4) = " CTR" Then strOld = Replace(strOld, " CTR", "")
If Right(strOld, 8) = " LIMITED" Then strOld = Replace(strOld, " LIMITED", "")
If Right(strOld, 20) = " LIMITED PARTNERSHIP" Then strOld = Replace(strOld, " LIMITED PARTNERSHIP", "")
If Right(strOld, 3) = " CO" Then strOld = Replace(strOld, " CO", "")
If Right(strOld, 3) = " LT" Then strOld = Replace(strOld, " LT", "")
If Right(strOld, 3) = " MD" Then strOld = Replace(strOld, " MD", "")
If Right(strOld, 3) = " OD" Then strOld = Replace(strOld, " OD", "")
If Right(strOld, 7) = " THE CO LTD" Then strOld = Replace(strOld, " THE CO LTD", "")
If Right(strOld, 5) = " LTEE" Then strOld = Replace(strOld, " LTEE", "")
If Right(strOld, 10) = " LTEE CORP" Then strOld = Replace(strOld, " LTEE CORP", "")
If Right(strOld, 5) = " CORP" Then strOld = Replace(strOld, " CORP", "")
If Right(strOld, 13) = " INCORPORATED" Then strOld = Replace(strOld, " INCORPORATED", "")
'Replace " INC " with a space
For i = 1 To Len(strOld)
intPosition = InStr(1, strOld, " INC ", vbTextCompare)
If intPosition > 0 Then
strOld = Left(strOld, intPosition) & Right(strOld, (Len(strOld) - (intPosition + 4)))
End If
Next i
'Remove "." (period)
For i = 1 To Len(strOld)
intPosition = InStr(1, strOld, ".", vbTextCompare)
If intPosition > 0 Then
strOld = Left(strOld, intPosition - 1) & Right(strOld, (Len(strOld) - (intPosition)))
End If
Next i
'Remove "'" (period)
For i = 1 To Len(strOld)
intPosition = InStr(1, strOld, "'", vbTextCompare)
If intPosition > 0 Then
strOld = Left(strOld, intPosition - 1) & Right(strOld, (Len(strOld) - (intPosition)))
End If
Next i
'Remove trailing " AND"
If Right(strOld, 4) = " AND" Then strOld = Replace(strOld, " AND", "")
Next c
MsgBox "Finished"
End Sub