Replace text in a cell with criteria

gtd526

Well-known Member
Joined
Jul 30, 2013
Messages
657
Office Version
  1. 2019
Platform
  1. Windows
Hello,
Using VBA:
Im trying to remove text from a cell if the last 4 digits are " - x" or " - y" or " - z". Any of the 3 criteria's.

NBA.xlsm
TUVW
1EasternWLPct
2Milwaukee - y5414.794
3Toronto - x4918.731
4Boston - x4423.657
5Miami - z4325.632
6Indiana - z4226.618
7Philadelphia4127.603
8Brooklyn3235.478
NBA Standings-Yahoo

The code I created:
Dim c As Range

For Each c In Range("T:T")
If c.Value = Right(c, 4) = " - y" Then
c = Left(c, Len(c) - 4)
End If
MsgBox c.Address 'just to see if its working 'c'
Next c

I've tried putting " " for the spaces, among other changes, but its not working.
There are formula's that work, but I want VBA to do it for me (all at once).
Thank you.
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
VBA Code:
Function ExtractNthWord(x As String, y As Integer) 'GOT THIS FROM https://www.exceltip.com/tips/how-to-extract-nth-word-from-text-string-using-vba-in-microsoft-excel-2010.html
Dim word() As String, wordCount As Long
word = VBA.Split(x, " ")
wordCount = UBound(word)
    If wordCount < 1 Or (y - 1) > wordCount Or y < 0 Then
        ExtractNthWord = ""
    Else
        ExtractNthWord = word(y - 1)
    End If
End Function
Sub gtd546()
Dim cl As Object, lastRow As Long
With Sheets("NBA Standings-Yahoo")
    lastRow = .Cells(.Rows.Count, "T").End(xlUp).Row
    For Each cl In .Range("T2:T" & lastRow)
        If ExtractNthWord(cl.Value, 1) <> "" Then
            cl.Value = ExtractNthWord(cl.Value, 1)
        End If
    Next cl
End With
End Sub
 
Upvote 0
Try:
VBA Code:
Sub ReplaceString()
    Application.ScreenUpdating = False
    Dim LastRow As Long, rng As Range
    LastRow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    For Each rng In Range("T2:T" & LastRow)
        If InStr(rng, "-") > 0 Then
            rng = Mid(rng, 1, WorksheetFunction.Find("-", rng) - 1)
        End If
    Next rng
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Solution
Assuming x, y or z are the only text that can follow the space/dash/space, then give this macro a try...
VBA Code:
Sub DeleteDashedText()
  Columns("T").Replace " - *", "", xlPart, , False, , False, False
End Sub
 
Upvote 0
Assuming x, y or z are the only text that can follow the space/dash/space, then give this macro a try...
VBA Code:
Sub DeleteDashedText()
  Columns("T").Replace " - *", "", xlPart, , False, , False, False
End Sub
Thank you. Love the one-liners.
 
Upvote 0
Try:
VBA Code:
Sub ReplaceString()
    Application.ScreenUpdating = False
    Dim LastRow As Long, rng As Range
    LastRow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    For Each rng In Range("T2:T" & LastRow)
        If InStr(rng, "-") > 0 Then
            rng = Mid(rng, 1, WorksheetFunction.Find("-", rng) - 1)
        End If
    Next rng
    Application.ScreenUpdating = True
End Sub
Thank you for your reply and macro. I only had to adjust -1 to -2, because it was leaving 1 extra space and the end of the value,
so my vlookup was not working.
 
Upvote 0

Forum statistics

Threads
1,214,636
Messages
6,120,664
Members
448,976
Latest member
sweeberry

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