Hi,
I have data in column A that are alphanumeric strings which should always be in upper case and in any of the formats below (the question marks represent numbers that could be in any combination, and all other characters should always remain the same).
94_00????_DMT_????
94_00????_LOC_????
94_00????_NME_????
94_00????_PTN_????
The data entered may be in any incorrect formats, including omitting any number of the _ or ? characters, such as in the examples below. This means that the length of the string varies, and the position of the characters needing to be replaced can also vary. The corrections need to be made until the last row of data.
Incorrect entries:
94_00????_LOC????
94_00???DMT_???
94_00????dmt???
9400???_nme_??
9400???_PTn_????
94_00??_loc_????
9400???PTN????
I am using the code below to amend to upper case, correct the first part of the string if required, and to change the variations around "DMT" to just "DMT" and then to the correct format of "_DMT_". I have repeated this for the other formats.
Columns("A:A").Select
Selection.Replace What:="9400", Replacement:="94_00", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Selection.Replace What:="_DMT_", Replacement:="DMT", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Selection.Replace What:="_DMT", Replacement:="DMT", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Selection.Replace What:="DMT_", Replacement:="DMT", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Selection.Replace What:="DMT", Replacement:="_DMT_", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
With Range("A1", Cells(Rows.Count, "A").End(xlUp))
.Value = Evaluate("INDEX(UPPER(" & .Address(External:=True) & "),)")
End With
I only want 9400 to be changed in the first instance, what amendment do I need to make to the above code for this? Also, I am self teaching VBA and therefore using the record macro function to generate the code; in order to aid my understanding, is there more concise code or a better alternative I can use to acheive the same result?
Bookmarks