VBA Code to remove trailing "/"

MHamid

Active Member
Joined
Jan 31, 2013
Messages
472
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
Hello,

I am trying to figure out a code that will remove the trailing "/" from a string. I haven't found a code that I can use for my situation.
The range is in column AR3:AR (from row 3 to last row with data). My current data in column AR has values such as "Business/Operations/" and I am looking to remove the trailing "/' if there is any within the range.
Any assistance will be greatly appreciated.


Thank you,
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
VBA Code:
Sub test()
  For Each rng In Range("AR:AR")
    If Right(Trim(rng.Value), 1) = "/" Then
      rng.Value = Left(Trim(rng.Value), Len(Trim(rng.Value))-1)
    End If
  Next
End Sub
 
Last edited by a moderator:
Upvote 0
VBA Code:
Sub test()
  For Each rng In Range("AR:AR")
    If Right(Trim(rng.Value), 1) = "/" Then
      rng.Value = Left(Trim(rng.Value), Len(Trim(rng.Value))-1)
    End If
  Next
End Sub
Awesome! Thank you! I just realized that I also will need to remove the "/" from the beginning of the string as well. Some cell begin with "/" and I need that removed as well. Do I need to just copy this same code and replace the right with left function?
 
Upvote 0
Another option?
VBA Code:
Sub Clean()
    With Range("AR3:AR" & Cells(Rows.Count, "AR").End(xlUp).Row)
        .Replace "/", "", xlPart
    End With
End Sub
 
Upvote 0
Awesome! Thank you! I just realized that I also will need to remove the "/" from the beginning of the string as well. Some cell begin with "/" and I need that removed as well. Do I need to just copy this same code and replace the right with left function?
Yes, Left condition will look like that:
VBA Code:
Sub test()
  For Each rng In Range("AR:AR")
    If Right(Trim(rng.Value), 1) = "/" Then
      rng.Value = Left(Trim(rng.Value), Len(Trim(rng.Value))-1)
    End If
    If Left(Trim(rng.Value), 1) = "/" Then
      rng.Value = Right(Trim(rng.Value), Len(Trim(rng.Value))-1)
    End If
  Next
End Sub
 
Upvote 1
Solution
Another option?
VBA Code:
Sub Clean()
    With Range("AR3:AR" & Cells(Rows.Count, "AR").End(xlUp).Row)
        .Replace "/", "", xlPart
    End With
End Sub
Thank you Kevin, but I just need to remove any "/" at the beginning and end of a string. I need to keep the ones in between the string.
 
Upvote 0
Yes, Left condition will look like that:
VBA Code:
Sub test()
  For Each rng In Range("AR:AR")
    If Right(Trim(rng.Value), 1) = "/" Then
      rng.Value = Left(Trim(rng.Value), Len(Trim(rng.Value))-1)
    End If
    If Left(Trim(rng.Value), 1) = "/" Then
      rng.Value = Right(Trim(rng.Value), Len(Trim(rng.Value))-1)
    End If
  Next
End Sub
Thank you for confirming Flash ... is exactly what I need.
 
Upvote 0
Thank you Kevin, but I just need to remove any "/" at the beginning and end of a string. I need to keep the ones in between the string.
Understood, and thanks for the feedback :)
 
Upvote 0

Forum statistics

Threads
1,215,053
Messages
6,122,888
Members
449,097
Latest member
dbomb1414

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