Change first 2 charecters in cell to certain integer (with loop,VBA)

flay

New Member
Joined
Feb 9, 2021
Messages
19
Office Version
  1. 365
Platform
  1. Windows
I was trying to create a script in vba which check the cell "A1" and replaces the first 2 charecters of the string to a integer if the first 2 characters are Ni lets say then change them to 801 . should look something like that:

originaltext = Range("a1").Value

If (Left(A1, 2) = "NI") Then newtext = Replace("originaltext", "NI", "801")

ElseIf (Left(A1, 2) = "RE") Then newtext = Replace("originaltext", "RE", "821")

ElseIf (Left(A1, 2) = "NV") Then newtext = Replace("originaltext", "NV", "571")

ElseIf (Left(A1, 2) = "NF") Then newtext = Replace("originaltext", "NF", "831")

end if n=n+1 Loop

i want it to go trough every cell and do that action starting from a1 lets say all the way down to a2 and a3 and so on.

how do i create a right loop for that?
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
Hi & welcome to MrExcel.
How many different strings do you need to check for?
 
Upvote 0
Hi & welcome to MrExcel.
How many different strings do you need to check for?
i have a very big database, all of the cells from A1 to somewhere around A130000 starts with one of these charecters : "NI" , "RE", "NV", "NF"
i need to check which one they start with and Replace them with a certain integer as mentioned in the code.
NI=801
RE=821
NV=571
NF=831
 
Upvote 0
So you just need to check for those 4 strings?
 
Upvote 0
Sound like a case for: 'Case Select' Fluff. Thanks to you and your help in the past I use it a lot now (y):biggrin:
 
Upvote 0
Ok, how about
VBA Code:
Sub flay()
   Dim Ary As Variant
   Dim i As Long
   
   Ary = Array("NI", "801", "RE", "821", "NV", "571", "NF", "831")
   For i = 0 To UBound(Ary) Step 2
      With Range("A2", Range("A" & Rows.Count).End(xlUp))
         .Value = Evaluate(Replace("if(left(@,2)=""" & Ary(i) & """," & Ary(i + 1) & "&mid(@,3,100),@)", "@", .Address))
      End With
   Next i
End Sub
 
Upvote 0
Ok, how about
VBA Code:
Sub flay()
   Dim Ary As Variant
   Dim i As Long
  
   Ary = Array("NI", "801", "RE", "821", "NV", "571", "NF", "831")
   For i = 0 To UBound(Ary) Step 2
      With Range("A2", Range("A" & Rows.Count).End(xlUp))
         .Value = Evaluate(Replace("if(left(@,2)=""" & Ary(i) & """," & Ary(i + 1) & "&mid(@,3,100),@)", "@", .Address))
      End With
   Next i
End Sub
that
works
perfectely..
im stunned thank you so much !
What if when it goes trough the row and searches for one of the 4 integers if it doesnt find a match it deletes the row .
is that possible?
 
Upvote 0
That's a totally different question, so needs a new thread. Thanks
 
Upvote 0

Forum statistics

Threads
1,215,009
Messages
6,122,674
Members
449,091
Latest member
peppernaut

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top