VBA evaluate cell and remove everything after the dash

FryGirl

Well-known Member
Joined
Nov 11, 2008
Messages
1,364
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
I'm hoping to use the evaluate function in VBA to keep everything before the dash in row 1. If there isn't a dash in the data then just keep what's already in the cell.

Example data:
POV - Private VehicleCATMPH - Miles Per Hour
POVCATMPH

The first row is row 1, and just using row 2 to show you what I want replaced directly in the cells of row 1.

This is close, but the .value line is not right.

VBA Code:
Sub RemoveData()
    Dim LastCol As Long: LastCol = Cells(1, Columns.Count).End(xlToLeft).Column
    With Range(Cells(1, 1), Cells(1, LastCol))
        .Value = Evaluate("IF(ISNUMBER(SEARCH(""-"",@)),LEFT(@,FIND(""-"",@)-2),@))
    End With
End Sub
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
How about
VBA Code:
        .Value = Evaluate(Replace("IF(ISNUMBER(SEARCH(""-"",@)),LEFT(@,FIND(""-"",@)-2),@)", "@", .Address))
 
Upvote 0
Looks like this one-liner should also work...
VBA Code:
Sub RemoveData()
  Range("A1", Cells(1, Columns.Count).End(xlToLeft)).Replace " -*", "", xlPart, , False, , False, False
End Sub
 
Upvote 0
Thanks Fluff. That works perfect.

Thanks Rick. This worked great as well.
 
Upvote 0
Thanks Rick. This worked great as well.
You are quite welcome. One thought on this... since you are processing all cells on Row 1, my code can be simplified...
VBA Code:
Sub RemoveData()
  [1:1].Replace " -*", "", xlPart, , False, , False, False
End Sub
And just so you know, my code is nothing more than the automating of selecting Row 1, pressing CTRL+H to bring up the Replace dialog box, putting " -*" (without the quote marks) in the Find What field, leaving the Replace With field empty, clicking the "Options>>" button to make sure both checkboxes are not checked, then clicking OK.
 
Upvote 0

Forum statistics

Threads
1,215,749
Messages
6,126,661
Members
449,326
Latest member
asp123

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