Delete first and last item for each cell in the same column for two sheets

abdo meghari

Well-known Member
Joined
Aug 3, 2021
Messages
612
Office Version
  1. 2019
Hi guys,
I would macro to delete the first and last item for each cell in column J for two sheets.
the first item always contains space after it and last item always contains space before it . also will contain many letters for first and last item.
CH.xlsm
IJKLMN
1CODEBRANDUNIT PRICE1UNIT PRICE2UNIT PRICE3UNIT PRICE4
21241BS 1200R20 G580 JAP2035000
31244BS 1200R20 R187 JAP2000000
41269BS 1200R24 G580 JAP1900000
51556BS 185R14C R660 TR423000
61221BS 205/70R15C R623 THI405000
71547BS 205R16C D840 THI625000
81502BS 215/65R16C R611 THI60058300
91227BS 215/70R15C R623 THI42554400
101503BS 225/85R16C R202 JAP975000
111310BS 225/95R16C D618 JAP515695715700
121402BS 245/70R16 D697 JAP590000
131346DUN 255/70R15C D840 JAPAN50563500
141326BS 265/60R18 D840 JAP721000
151534BS 265/65R17 D693 THAILAND745000
161391BS 265/65R17 D840 JAP53569000
171411MARS 275/55R20 ALENZA1 KORIA72594200
181190BS 285/50R20 DSPORT JAP93670500
191315BS 315/80R22.5 G580 JAP 2470000
201257BS 315/80R22.5 R184 JAP2015000
211401BS 650R16 R230 JAP570000
221305HILO 700R16 R230 JAP762000
231306BS 750R16 R230 JAP71594000
241307BS 750R16 VSJ JAP910000
251284GC 1200R20 AZ0026 CHINA895112500
261285GC 1200R20 AZ0183 CHI925122512050
271292GC 1200R24 AZ166 CHI935000
281385GC 315/80R22.5 AT161 CHI73595500
291287GC 315/80R22.5 AZ126 CHI735000
301294GC 315/80R22.5 AZ188 CHI74596500
311492GC 385/65R22.5 AT131 CHI1275000
321528TH 185/65R14 H-93 CHI134000
331493WL 195/65R15 Z-108 CHI173000
341486WL 205/55R16 Z-108 CHI185000
PURCHASING


CH.xlsm
IJKLM
1CODEBRANDQTYUNIT PRICETOTAL
21306BS 750R16 R230 JAP107757750
31306BS 750R16 R230 JAP47803120
41305BS 700R16 R230 JAP27701540
SELLING
Cell Formulas
RangeFormula
M2:M4M2=K2*L2




Result
CH.xlsm
IJKLMN
1CODEBRANDUNIT PRICE1UNIT PRICE2UNIT PRICE3UNIT PRICE4
212411200R20 G5802035000
312441200R20 R1872000000
412691200R24 G5801900000
51556185R14C R660423000
61221205/70R15C R623405000
71547205R16C D840625000
81502215/65R16C R61160058300
91227215/70R15C R62342554400
101503225/85R16C R202975000
111310225/95R16C D618515695715700
121402245/70R16 D697590000
131346255/70R15C D84050563500
141326265/60R18 D840721000
151534265/65R17 D693745000
161391265/65R17 D84053569000
171411275/55R20 ALENZA172594200
181190285/50R20 DSPORT93670500
191315315/80R22.5 G5802470000
201257315/80R22.5 R1842015000
211401650R16 R230570000
221305700R16 R230762000
231306750R16 R23071594000
241307750R16 VSJ910000
2512841200R20 AZ0026895112500
2612851200R20 AZ0183925122512050
2712921200R24 AZ166935000
281385315/80R22.5 AT16173595500
291287315/80R22.5 AZ126735000
301294315/80R22.5 AZ18874596500
311492385/65R22.5 AT1311275000
321528185/65R14 H-93134000
331493195/65R15 Z-108173000
341486205/55R16 Z-108185000
PURCHASING


the same thing for second sheet
thanks
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
An option with a slightly shorter 'Evaluate' line would be

VBA Code:
Sub Second_Brand_Only_v4()
  Dim aSheets As Variant, Sh As Variant
  
  aSheets = Split("PURCHASING|SELLING", "|")
  For Each Sh In aSheets
    With Sheets(Sh).Range("J2", Sheets(Sh).Range("J" & Rows.Count).End(xlUp))
      .Value = Evaluate(Replace("trim(mid(substitute(trim(#&"" ""&#),"" "",REPT("" "",100)),100,100))", "#", .Address(External:=True)))
    End With
  Next Sh
End Sub
 
Upvote 0
An option with a slightly shorter 'Evaluate' line would be
this is what I got.
DLETEITEM.xlsm
IJKLMN
1CODEBRANDUNIT PRICE1UNIT PRICE2UNIT PRICE3UNIT PRICE4
212411200R202035000
312441200R202000000
412691200R201900000
515561200R20423000
612211200R20405000
715471200R20625000
815021200R2060058300
912271200R2042554400
1015031200R20975000
1113101200R20515695715700
1214021200R20590000
1313461200R2050563500
1413261200R20721000
1515341200R20745000
1613911200R2053569000
1714111200R2072594200
1811901200R2093670500
1913151200R202470000
2012571200R202015000
2114011200R20570000
2213051200R20762000
2313061200R2071594000
2413071200R20910000
2512841200R20895112500
2612851200R20925122512050
2712921200R20935000
2813851200R2073595500
2912871200R20735000
3012941200R2074596500
3114921200R201275000
3215281200R20134000
3314931200R20173000
3414861200R20185000
PURCHASING


DLETEITEM.xlsm
IJKLMN
1CODEBRANDQTYUNIT PRICETOTALUNIT PRICE4
21306750R161077577500
31306750R16478031200
41305750R16277015400
SELLING
Cell Formulas
RangeFormula
M2:M4M2=K2*L2
 
Upvote 0
this is what I got.
Don't know how you got that. For me ..

Before:

abdo meghari_4.xlsm
J
1BRAND
2BS 1200R20 G580 JAP
3BS 1200R20 R187 JAP
4BS 1200R24 G580 JAP
5BS 185R14C R660 TR
6BS 205/70R15C R623 THI
7BS 205R16C D840 THI
8BS 215/65R16C R611 THI
9BS 215/70R15C R623 THI
10BS 225/85R16C R202 JAP
11BS 225/95R16C D618 JAP
12BS 245/70R16 D697 JAP
13DUN 255/70R15C D840 JAPAN
14BS 265/60R18 D840 JAP
15BS 265/65R17 D693 THAILAND
16BS 265/65R17 D840 JAP
17MARS 275/55R20 ALENZA1 KORIA
18BS 285/50R20 DSPORT JAP
19BS 315/80R22.5 G580 JAP
20BS 315/80R22.5 R184 JAP
21BS 650R16 R230 JAP
22HILO 700R16 R230 JAP
23BS 750R16 R230 JAP
24BS 750R16 VSJ JAP
25GC 1200R20 AZ0026 CHINA
26GC 1200R20 AZ0183 CHI
27GC 1200R24 AZ166 CHI
28GC 315/80R22.5 AT161 CHI
29GC 315/80R22.5 AZ126 CHI
30GC 315/80R22.5 AZ188 CHI
31GC 385/65R22.5 AT131 CHI
32TH 185/65R14 H-93 CHI
33WL 195/65R15 Z-108 CHI
34WL 205/55R16 Z-108 CHI
PURCHASING


Code:

VBA Code:
Sub Second_Brand_Only_v4()
  Dim aSheets As Variant, Sh As Variant
  
  aSheets = Split("PURCHASING|SELLING", "|")
  For Each Sh In aSheets
    With Sheets(Sh).Range("J2", Sheets(Sh).Range("J" & Rows.Count).End(xlUp))
      .Value = Evaluate(Replace("trim(mid(substitute(trim(#&"" ""&#),"" "",REPT("" "",100)),100,100))", "#", .Address(External:=True)))
    End With
  Next Sh
End Sub

After:

abdo meghari_4.xlsm
J
1BRAND
21200R20
31200R20
41200R24
5185R14C
6205/70R15C
7205R16C
8215/65R16C
9215/70R15C
10225/85R16C
11225/95R16C
12245/70R16
13255/70R15C
14265/60R18
15265/65R17
16265/65R17
17275/55R20
18285/50R20
19315/80R22.5
20315/80R22.5
21650R16
22700R16
23750R16
24750R16
251200R20
261200R20
271200R24
28315/80R22.5
29315/80R22.5
30315/80R22.5
31385/65R22.5
32185/65R14
33195/65R15
34205/55R16
PURCHASING


Ran the code again and the column remained as above.
 
Upvote 0
I have no idea !
here is attachment
Dropbox.
Worked for me. It might relate to your Excel version - which I don't have to test with.

Perhaps we could revert to a line-by-line 'Split' method like Alex was using in the early part of the thread.

VBA Code:
Sub Second_Brand_Only_v4()
  Dim aSheets As Variant, Sh As Variant, a As Variant, Bits As Variant
  Dim i As Long
 
  aSheets = Split("PURCHASING|SELLING", "|")
  For Each Sh In aSheets
    With Sheets(Sh).Range("J2", Sheets(Sh).Range("J" & Rows.Count).End(xlUp))
      a = .Value
      For i = 1 To UBound(a)
        Bits = Split(Application.Trim(a(i, 1)))
        If UBound(Bits) > 0 Then a(i, 1) = Bits(1)
      Next i
      .Value = a
    End With
  Next Sh
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,900
Messages
6,175,276
Members
452,629
Latest member
SahilPolekar

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