Macro to Delete characters from left and right

gurtejhira

New Member
Joined
Jul 28, 2015
Messages
26
I have to write macro for one column while keeping following scenarios in mind :
1. Remove first character from left side.
2. Remove all the characters from right side after "-" or "ER"

I will really appreciate if anyone can help me out.

Regards
 

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
A macro may be fairly slow depending on how much data you have when you are wanting an entire column to equal something from another column.

Either way, try using the MID function. The start point is the second character, but you will have to find the end point. With VBA, use INSTRREV function(looks for string from right side of target string.) With excel, use a nested MID and FIND function such as:
=MID(B2,2,FIND("-",B2,1)-2) In this example, I am subtracting 2 because I don't want the hypen in my returned results.
 
Upvote 0
Give this a try:
Code:
Sub Test()
    Application.ScreenUpdating = False
    Dim LastRow As Long
    LastRow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    Dim rng As Range
    On Error Resume Next
    For Each rng In Range("A1:A" & LastRow)
        rng = Left(Mid(rng, 2, 9999), WorksheetFunction.Find("-", rng) - 2)
        rng = Left(Mid(rng, 2, 9999), WorksheetFunction.Find("ER", rng) - 2)
    Next rng
    Application.ScreenUpdating = True
End Sub
This assumes the data is in column A. Change to suit your needs.
 
Last edited:
Upvote 0
Mumps now I am told to put one more logic if "ER" logic should only be used if "-" logic is not true.

Kindly guide.
 
Upvote 0
This worked for me... not sure if it's exactly what you are wanting. The formula in column C will first look for the hypen. If not found it searches for "ER" (case-sensitive). If neither is a match, it will return nothing.

Excel 2010
ABC
1FULL STRINGPARTIAL
21Test-SuperTest
32SecondERRORSecond
43SecondError

<colgroup><col><col><col><col></colgroup><thead>
</thead><tbody>
</tbody>
Sheet1

Worksheet Formulas
CellFormula
C2=IFERROR(MID(B2,2,IFERROR(FIND("-",B2,1),FIND("ER",B2,1))-2),"")
C3=IFERROR(MID(B3,2,IFERROR(FIND("-",B3,1),FIND("ER",B3,1))-2),"")
C4=IFERROR(MID(B4,2,IFERROR(FIND("-",B4,1),FIND("ER",B4,1))-2),"")

<thead>
</thead><tbody>
</tbody>

<tbody>
</tbody>
 
Upvote 0
Mumps

Thank you for your help. Please ignore my previous msg . Just one issue left whenever I try to use the vba code you provided its cutting first letter of the field even if its not "*" . Is it possible to fix that issue ?
 
Upvote 0

Forum statistics

Threads
1,214,991
Messages
6,122,628
Members
449,095
Latest member
bsb1122

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