Delete the last character of cells in column "K" if it is equal to "X" or "x"

harzer

Board Regular
Joined
Dec 15, 2021
Messages
159
Office Version
  1. 2016
Platform
  1. Windows
Hello everyone,
I am asking for support to develop a macro in vba.
I would like to use this Macro to browse all the cells in column "K" and test if the last character is equal to "X" or "x", if this is the case, then we delete this character.
I draw your attention to the fact that the data in my Range is important, hence the need to use tables so that the code can run quickly.
Example :

VBA Code:
If Right(arr(i, 11), 1) = "X" Or Right(arr(i, 11), 1) = "x" Then

I remain at your disposal for any further information.
Thank you for your contributions.

Classeur1
ABCDEFGHIJK
1JeunePèreMèreEleveurAgeVolièreCageNé(e)ToursCouleurElevage
2AEM27-002/2024 FAEM27-010/2021 MAEM27-017/2023 FDegallaix Fabrice0a 5m 18j5H1224-04-20244TFemelle Tt jauneX
3AEM27-003/2024 FAEM27-036/2023 MHWA96-045/2022 FDegallaix Fabrice0a 5m 14j4B1328-04-20244TFemelle Tte jauneElevé/AEM27-017/23 Cage 12x
4AEM27-004/2024 MAEM27-036/2023 MHWA96-045/2022 FDegallaix Fabrice0a 5m 14j4B1328-04-20244TMâle Tâche aile droiteElevé/AEM27-017/23 Cage 12 X
5AEM27-005/2024 MAEM27-022/2023 MAEM27-015/2023 FDegallaix Fabrice0a 9m 6j5H115-01-20244TMâle Tte jauneElevé/AEM27-023/23 Cage 8 X
6AEM27-006/2024 MAEM27-022/2023 MAEM27-015/2023 FDegallaix Fabrice0a 9m 6j5H115-01-20244TMâle Tâche derrière œil gaucheElevé/AEM27-023/23 Cage 9
7AEM27-007/2024 MAEM27-001/2023 MAEM27-023/2023 FDegallaix Fabrice0a 9m 6j5H85-01-20244TMâle Tt jauneElevé/AEM27-023/23 Cage 10 x
8AEM27-008/2024 MAEM27-033/2022 MAEM27-009/2023 FDegallaix Fabrice0a 9m 6j5H45-01-20244TMâle Tâche au dosElevé/AEM27-023/23 Cage 11X
9AEM27-009/2024 MAEM27-033/2022 MAEM27-009/2023 FDegallaix Fabrice0a 9m 6j5H45-01-20244TMâle Tâche au dosx
Feuil1
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
Not sure what you're asking for because it appears you already have a vba procedure for this (although you only posted one line). Can suggest that all you need is one comparison though, like
VBA Code:
If UCase(Right(arr(i, 11), 1)) = "X" Then ...
 
Upvote 0
Good evening Micron,
Thank you for your answer.
No I do not have any code implemented, it is a line of code that I found on the internet according to my research according to my request.
Greetings.
 
Upvote 0
If you use Micron's suggestion in a macro with the use of an array, as indicated in both posts, you might end up with
Code:
Sub Maybe()
Dim lr As Long, i As Long
Dim datArr
lr = Cells(Rows.Count, "K").End(xlUp).Row
datArr = Range("K1:K" & lr).Value
    For i = LBound(datArr) To UBound(datArr)
        If UCase(Right(datArr(i, 1), 1)) = "X" Then datArr(i, 1) = Left(datArr(i, 1), Len(datArr(i, 1)) - 1)
    Next i
Range("K1").Resize(UBound(datArr)).Value = datArr
End Sub
 
Upvote 0
Solution
Maybe this would work for you
VBA Code:
Sub removeX()
Dim i As Integer
Dim Lrow As Long
Dim strValue As String

Lrow = Cells(rows.Count, "K").End(xlUp).Row
For i = 1 To Lrow
    strValue = Range("K" & i)
    If UCase(Right(Range("K" & i), 1)) = "X" Then
        strValue = Trim(Left(strValue, Len(strValue) - 1))
        Range("K" & i) = strValue
    End If
Next

End Sub
 
Upvote 0
try
Code:
Sub test()
    [k2:k10000] = Evaluate(Replace("if(right(♪)=""X"",left(♪,len(♪)-1),if(♪="""","""",k2:k1000))", "♪", "k2:k10000"))
End Sub
 
Upvote 0
Good evening Micron and jolivanes,
Thank you both for your two proposals.
I tested both Macros, they are both functional and give me total satisfaction.
With respect for the work done by each of you, I have a preference for jolivanes' solution for the simple reason that the code is faster, because it uses an array.
My sincere thanks to both of you.
 
Upvote 0
Good evening Fuji,
Your solution is just as good and fast.
I also add that it is perfect because it consists of a single line of code that I unfortunately cannot decipher given my level in vba.
I thank you and I am very satisfied and happy with the abundance of good answers.
Greetings.
 
Upvote 0
You are welcome and thanks for the feedback.
I just couldn't edit my code and it should be
Rich (BB code):
Sub test()
    [k2:k10000] = Evaluate(Replace("if(right(♪)=""X"",left(♪,len(♪)-1),if(♪="""","""",))", "♪", "k2:k10000"))
End Sub
So that you can replace k2:k1000 to whatever the range you want.
 
Upvote 0

Forum statistics

Threads
1,224,804
Messages
6,181,056
Members
453,015
Latest member
ZochSteveo

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