VBA to replace the last character with a different character

idres

New Member
Joined
Jul 22, 2021
Messages
2
Office Version
  1. 2013
Platform
  1. Windows
أحتاج إلى VBA من شأنه:

1 / ابحث في العمود "A" عن الكلمات التي تحتوي على الحرف الأخير "m".

2 / إن وجد. استبدل الحرف الأخير في كل كلمة بالحرف "s"

لماذا هذا الطلب؟
في لغتي العربية يوجد حرف معقد يتشكل حسب موضع الكلمة. استبدال الجميع لا يعمل ، سوف تفسد الكلمات
إذا كانت هذه الرسالة تأتي في بداية الكلمة أو في وسطها ، فلا خطأ. لكن في نهاية الكلمة ، يكتبها الناس في شكلين ، وهذا يسبب مشكلة عند البحث
 

Attachments

  • 33.jpg
    33.jpg
    47.3 KB · Views: 6

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce

Crystalyzer

Well-known Member
Joined
Oct 18, 2011
Messages
688
Office Version
  1. 365
Platform
  1. Windows
يرجى المعذرة عن أي أخطاء في الترجمة ، أنا أستخدم مترجم جوجل.
هل هناك كلمات متعددة في خلية واحدة وإذا كانت هذه الكلمات تنتهي بالحرف "م" ، فأنت ترغب في استبدال "م" بالحرف "ق"؟
 

GWteB

Well-known Member
Joined
Sep 10, 2010
Messages
1,834
Office Version
  1. 2013
Platform
  1. Windows
Hi @idres, Welcome to MrExcel!

You might be looking for this:
VBA Code:
Sub Idris()
   
    Dim r       As Range
    Dim arr     As Variant
    Dim s       As String
    Dim i       As Long

    With ThisWorkbook.ActiveSheet
        Set r = .Range("A1:A" & .Cells(.Rows.Count, "A").End(xlUp).Row)
        arr = r.Value
        For i = LBound(arr) To UBound(arr)
            If Not IsEmpty(arr(i, 1)) Then
                s = arr(i, 1)
                s = VBA.Join(VBA.Split(s, "m "), "s ")
                s = VBA.Join(VBA.Split(s, "m" & vbLf), "s" & vbLf)
                If Right$(s, 1) = "m" Then
                    s = Left$(s, Len(s) - 1) & "s"
                End If
                arr(i, 1) = s
            End If
        Next i
        r.Value = arr
    End With
End Sub
 
Solution

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
62,507
Office Version
  1. 365
Platform
  1. Windows
Welcome to the MrExcel Message Board!

Cross-posting (posting the same question in more than one forum) is not against our rules, but the method of doing so is covered by #13 of the Forum Rules.

Be sure to follow & read the link at the end of the rule too!

Cross posted at: VBA to replace the last character with a different character
If you have posted the question at more places, please provide links to those as well.

If you do cross-post in the future and also provide links, then there shouldn’t be a problem.
 

idres

New Member
Joined
Jul 22, 2021
Messages
2
Office Version
  1. 2013
Platform
  1. Windows
Hi @idres, Welcome to MrExcel!

You might be looking for this:
VBA Code:
Sub Idris()
  
    Dim r       As Range
    Dim arr     As Variant
    Dim s       As String
    Dim i       As Long

    With ThisWorkbook.ActiveSheet
        Set r = .Range("A1:A" & .Cells(.Rows.Count, "A").End(xlUp).Row)
        arr = r.Value
        For i = LBound(arr) To UBound(arr)
            If Not IsEmpty(arr(i, 1)) Then
                s = arr(i, 1)
                s = VBA.Join(VBA.Split(s, "m "), "s ")
                s = VBA.Join(VBA.Split(s, "m" & vbLf), "s" & vbLf)
                If Right$(s, 1) = "m" Then
                    s = Left$(s, Len(s) - 1) & "s"
                End If
                arr(i, 1) = s
            End If
        Next i
        r.Value = arr
    End With
End Sub
Yes, this is . He succeeded. I'm very happy . I was stuck for seven days. Thank you Mr. GWteB Thank you all for the replies you are kind
Regarding cross-posting, I didn't notice, and this is the link VBA to replace the last character with a different character
 

GWteB

Well-known Member
Joined
Sep 10, 2010
Messages
1,834
Office Version
  1. 2013
Platform
  1. Windows
You are welcome and thanks for the feedback.
 

Forum statistics

Threads
1,141,570
Messages
5,707,141
Members
421,493
Latest member
Tusharrm

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
Top