Validate the letter "A" exist at the end and if doesn't put in

aaronr

New Member
Joined
Jul 8, 2010
Messages
34
I have group number and need to validate the letter "A" at the end and if doesn't exist need to put in. I currently us this code to rename and want to add a validate if the letter A is at the end then rename to B.

Dim Addr As String
Addr = "A2:A" & Cells(rows.Count, "A").End(xlUp).Row
Range(Addr) = Evaluate(Replace("IF(RIGHT(@)=""A"",LEFT(@,LEN(@)-1)&""B"",@)", "@", Addr))

Thanks for the help
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
Code:
Dim r as range, Addr as range
Addr = "A2:A" & Cells(rows.Count, "A").End(xlUp).Row
For each r in Addr
If Right(r.value,1) = "A" then
r.value = Left(r.value, Len(r.value)-1)) & "B"
Else
r.value = r.value & "A"
end if
Next r
HTH. Dave
 
Last edited:
Upvote 0
Dave,
line 5 is Red in VBA and when i run it gives me compile error and Syntax Error.

Code:
Dim r as range, Addr as range
Addr = "A2:A" & Cells(rows.Count, "A").End(xlUp).Row
For each r in Addr
If Right(r.value,1) = "A" then
r.value = Left(r.value, Len(r.value)-1)) & "B"
Else
r.value = r.value & "A"
end if
Next r
HTH. Dave
 
Upvote 0
I have group number and need to validate the letter "A" at the end and if doesn't exist need to put in.

Hi, using a similar approach to what you are already using..

Code:
Dim Addr As String
Addr = "A2:A" & Cells(Rows.Count, "A").End(xlUp).Row
Range(Addr) = Evaluate(Replace("IF(RIGHT(@)<>""A"",@&""A"",@)", "@", Addr))
 
Upvote 0
My apologies for not posting tested code. FormR your code missed the requirement to change the "A" to a "B" if it is present. Tested this code and it seems to work, Dave
Code:
Sub test()
Dim r As Range, Addr As Range
Set Addr = Sheets("sheet1").Range("A2:A" _
       & Sheets("sheet1").Cells(Rows.Count, "A").End(xlUp).Row)
For Each r In Addr
MsgBox Right(r.Value, 1)
If Right(r.Value, 1) = "A" Then
r.Value = Left(r.Value, Len(r.Value) - 1) & "B"
Else
r.Value = r.Value & "A"
End If
Next r
End Sub
 
Upvote 0
FormR your code missed the requirement to change the "A" to a "B" if it is present.

To be honest, I thought it was a new and separate request - maybe the OP will return and clarify at some point.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,171
Members
453,021
Latest member
Justyna P

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