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
531
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

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
One option with Power query

Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Extracted Text Between Delimiters" = Table.TransformColumns(Source, {{"BRAND", each Text.BetweenDelimiters(_, " ", " ", 0, 1), type text}})
in
    #"Extracted Text Between Delimiters"
 
Upvote 0
In case you need VBA:

VBA Code:
Sub ExtractProductCode()
    Dim ws As Worksheet
    Dim shtNames As Variant
    Dim rngBrand As Range, arrBrand As Variant
    Dim sBrand As String, posStart As Long, posEnd As Long
    Dim LastRow As Long, i As Long, iSht As Long
            
    shtNames = Array("Purchasing", "Selling")                   ' Update to your sheet names
    
    For iSht = 0 To UBound(shtNames)
        Set ws = Worksheets(shtNames(iSht))
        With ws
            LastRow = .Range("J" & Rows.Count).End(xlUp).Row
            Set rngBrand = .Range(.Cells(2, "J"), .Cells(LastRow, "J"))
            arrBrand = rngBrand.Value
        End With
        
        For i = 1 To UBound(arrBrand)
            sBrand = arrBrand(i, 1)
            posStart = InStr(1, sBrand, " ") + 1
            posEnd = InStrRev(sBrand, " ") - 1
            arrBrand(i, 1) = Mid(sBrand, posStart, posEnd - posStart + 1)
        Next i
        
        rngBrand.Value = arrBrand
    
    Next iSht
End Sub
 
Upvote 0
Thanks Alan, but shows error in formula for this word Extracted Text Between Delimiters !
 
Upvote 0
Hi Alex,
it works , but there is problem for highlighted cell . it doesn't delete the last item !
Abdo.xlsm
J
1BRAND
21200R20 G580
31200R20 R187
41200R24 G580
5185R14C R660
6205/70R15C R623
7205R16C D840
8215/65R16C R611
9215/70R15C R623
10225/85R16C R202
11225/95R16C D618
12245/70R16 D697
13255/70R15C D840
14265/60R18 D840
15265/65R17 D693
16265/65R17 D840
17275/55R20 ALENZA1
18285/50R20 DSPORT
19315/80R22.5 G580 JAP
20315/80R22.5 R184
21650R16 R230
22700R16 R230
23750R16 R230
24750R16 VSJ
251200R20 AZ0026
261200R20 AZ0183
271200R24 AZ166
28315/80R22.5 AT161
29315/80R22.5 AZ126
30315/80R22.5 AZ188
31385/65R22.5 AT131
32185/65R14 H-93
33 195/65R15 Z-108
34205/55R16 Z-108
PURCHASING

and how fix error invalid procedure call in this line
VBA Code:
arrBrand(i, 1) = Mid(sBrand, posStart, posEnd - posStart + 1)
after delete the items . I mean when run the macro every time?
 
Upvote 0
1) Check the highlighted cell before you run the macro, does it have an additional space on the end ?
2) How do you want to check if the procedure is run twice ? Does each row have 4 space separated components before and 2 components after ? I need some way of determining if it needs to be split up.
 
Upvote 0
I will be logging off soon so pre-empting a reply to my questions here is a different approach.

VBA Code:
Sub ExtractProductCode_Split()

    Dim ws As Worksheet
    Dim shtNames As Variant
    Dim rngBrand As Range, arrBrand As Variant
    Dim sBrand As String, posStart As Long, posEnd As Long
    Dim splitBrand As Variant
    Dim LastRow As Long, i As Long, iSht As Long
            
    shtNames = Array("Purchasing", "Selling")                   ' Update to your sheet names
    
    For iSht = 0 To UBound(shtNames)
        Set ws = Worksheets(shtNames(iSht))
        With ws
            LastRow = .Range("J" & Rows.Count).End(xlUp).Row
            Set rngBrand = .Range(.Cells(2, "J"), .Cells(LastRow, "J"))
            arrBrand = rngBrand.Value
        End With
        
        For i = 1 To UBound(arrBrand)
            sBrand = Application.Trim(arrBrand(i, 1))
            splitBrand = Split(sBrand, " ")
            If UBound(splitBrand) = 3 Then
                arrBrand(i, 1) = splitBrand(1) & " " & splitBrand(2)
            End If
        Next i
        
        rngBrand.Value = arrBrand
    
    Next iSht
End Sub
 
Upvote 0
That Brand on Line 19 has an extra space on the end.
If it you are still experiencing the same issue then you didn't run the 2nd version of the code that I gave you in Post #7.
The 2nd version also allows you to run it multiple times and it will only update the rows that have 4 parts to the Brand column value.

Note: I have changed the name of it, so if you are using a button you may be running the previous code.
 
Upvote 0
That Brand on Line 19 has an extra space on the end.
sorry about it.
it will only update the rows that have 4 parts to the Brand column value.
may you make 5 or 6 parts to the Brand column value.
I have some brands contains 5 or 6 parts, this is really rare but they're existed.
 
Upvote 0

Forum statistics

Threads
1,221,310
Messages
6,159,176
Members
451,543
Latest member
cesymcox

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