delete specific digits & letters also hyphen from specific sheets into columns

Alaa mg

Active Member
Joined
May 29, 2021
Messages
365
Office Version
  1. 2019
hello

I would delete theses from sheet1,sheet2(TT,13TM,12TA,TN,TD,-) . the sheet1 should delete from column B , but the sheet2 should delete from columns B,C .about the hyphen should just delete before 12MR ,22TM.
zz.xlsx
B
1brand
2CC 2500M14 TT 11TM NN1 GER
3CC 2500M14 13TM TN T123 GER
4CC 2500M14 11TM T123 TN GER
5CC 2500M14 11TM NN1 TT GER
6BBW 1248-23 TL-ML 13TR NN IT
7FG 12456TR40***** 22TM TT 1445V IT
8DF 125/22MT23.5-22TM SL200 GER
9MLC 111MR12L 12TM TD SV123 IT
10NN 123/34T22.5-12MR SS1/1 NN GR
SHEET1

sheet2
zz.xlsx
BCD
1BRANDTYOR
2CC 2500M14 TT 11TM NN1GER
3CC 2500M14 12TA T123 TNGER
4CC 2500M14 11TM TN T123GER
5CC 2500M14 11TM NN1 TTGER
6BBW 1248-23 TL-ML 13TR NNIT
7FG 12456TR40***** 1445V 22TM TT IT
8DF 125/22MT23.5-22TM ML-120SL200GER
9MLC 111MR12L 12TM TD SV123IT
10NN 123/34T22.5-12MR SS1/1 NNGER
SHEET2

note: my data are about 500 rows into two sheets
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
Try:
VBA Code:
Sub replaceVals()
    Application.ScreenUpdating = False
    Dim v1 As Variant, v2 As Variant, i As Long, ws As Worksheet
    v1 = Array("TT", "13TM", "12TA", "TN", "TD")
    v2 = Array("-12MR", "-22TM")
    For Each ws In Sheets
        If ws.Name = "Sheet1" Then
            For i = LBound(v1) To UBound(v1)
                ws.Range("B:B").Replace v1(i), ""
            Next i
            For i = LBound(v2) To UBound(v2)
                ws.Range("B:B").Replace v2(i), " " & Mid(v2(i), 2, 4)
            Next i
        ElseIf ws.Name = "Sheet2" Then
            For i = LBound(v1) To UBound(v1)
                ws.Range("B:C").Replace v1(i), ""
            Next i
            For i = LBound(v2) To UBound(v2)
                ws.Range("B:C").Replace v2(i), " " & Mid(v2(i), 2, 4)
            Next i
        End If
    Next ws
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Solution
thanks for this code ! actually I don't test it yet . because I see there is mistake about this line
VBA Code:
    v2 = Array("-12MR", "-22TM")
I would just delete the hyphen where locates before 12MR", "22TM as I said in OP
about the hyphen should just delete before 12MR ,22TM.
 
Upvote 0
Actually it is not a mistake. That line of code will replace "-12MR" and "-22TM" with "12MR" and "22TM". Try the macro and see how it works.
 
Upvote 0
my apologies ! thanks for clarifying
code works very well . much appreciated ;)
 
Upvote 0
You are very welcome. No apologies needed. :)
 
Upvote 0

Forum statistics

Threads
1,220,965
Messages
6,157,120
Members
451,399
Latest member
alchavar

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