azhun_ctech
New Member
- Joined
- Jun 3, 2019
- Messages
- 5
Hi All,
Please forgive me as I am still new to VBA and haven't quite figured out everything. I have a set of values (all in column A) that start with "7971" and I want to replace those characters with an "A" (IE: 797100910885 becomes A00910885") in the same column, if possible. If it goes to column B then that is ok as well. I want a formula to be able to loop through all of the values in column A and apply this change.
I was able to do it for one cell using the following code:
Please forgive me as I am still new to VBA and haven't quite figured out everything. I have a set of values (all in column A) that start with "7971" and I want to replace those characters with an "A" (IE: 797100910885 becomes A00910885") in the same column, if possible. If it goes to column B then that is ok as well. I want a formula to be able to loop through all of the values in column A and apply this change.
I was able to do it for one cell using the following code:
Code:
Sub macro3()
'Macro3 Macro
Dim originaltext As String, correctedtext As String
originaltext = Range("a2").Value
correctedtext = replace(originaltext, "7971", "A")
Range("a2").Value = correctedtext
End Sub
[End of code]
I have tried a few different approaches but am unable to make it so that it loops succesfully. I have tried the following code and get a mismatch type error and I'm not sure why.
[code]
Sub replacement()
Dim cell As Range, correctedText As String, originalText As String
'loops through each cell in column A
For Each cell In ActiveSheet.Range("A1:A50000")
correctedText = replace(originalText, "7971", "A")
Next cell
Dim ws As Worksheet
Dim LR As Long
Set ws = Application.ActiveSheet
LR = ws.Range("A:A" And ws.Rows.Count).End(xlUp).Row
ws.Range("A:A" & LR).Formula = replace(Range("A:A").Value, "7971", "A")
End Sub
[end of code]
What am I missing here? I have tried defining originalText and it doesn't seem to pull it as desired. I am sure it's a fairly simple fix but I've not had any luck so far!
Thanks in advanced for your help!
Ctech