remove specific character and exclude the other for multiple sheets

Ali M

Active Member
Joined
Oct 10, 2021
Messages
290
Office Version
  1. 2019
  2. 2013
Platform
  1. Windows
hello
I have this data for multiple sheets .
c.xlsm
ABCDE
1ITEMCODESSTRQTYBALANCE
21VBA 123T123 215.5*S12.5BOTTOLE 1L PR123 NRIT214 TR2312
32SLFR-101 FOOD-12 PR1011 NRIT2K CHI51
43SLFR FOOOD-1 RF100 NBGFR IND102
54DM/10 120***225l20 FSLUNCH PR121 BMTTR1112
dd

what I want it remove specific characters and exclude the first,second and last item from deletion
should be like this in column B
c.xlsm
ABCDE
1ITEMCODESSTRQTYBALANCE
21VBA 123T123 TR215.5*S12.5BOTTOLE 1L PR123 NRIT2142312
32SLFR-101 FOOD-12 CHIPR1011 NRIT2K51
43SLFR FOOOD-1 INDRF100 NBGFR102
54DM/10 120***225l20 BMTTRFSLUNCH PR1211112
dd

if it's possible by macro will be great because the data could be 2000 rows for each sheet
thanks
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
See if this macro does what you want...
VBA Code:
Sub SeparateData()
  Dim R As Long, LastRow As Long, Txt As String, Arr As Variant
  LastRow = Cells(Rows.Count, "C").End(xlUp).Row
  For R = 2 To LastRow
    Txt = Cells(R, "C").Value
    Arr = Split(Txt)
    Cells(R, "B").Value = Join(Application.Index(Arr, 1, Split("1 2 " & 1 + UBound(Arr))))
    Cells(R, "C").Value = Mid(Left(Txt, InStrRev(Txt, " ") - 1), InStr(InStr(Txt, " ") + 1, Txt, " ") + 1)
  Next
End Sub
 
Upvote 0
great !
two thing please when you have time .could modiyfy code,please? . first about last items . also I want two last items instead of one last item
like this as in first item
VBA 123T123 NRIT214 TR​
second how could implement the code for multiple sheets (sheet1,sheet2,sheet3)?
thanks
 
Upvote 0
2 inputboxes will pop up to let user choose.
VBA Code:
Option Explicit
Sub test()
Dim lr&, FItem, LItem, i&, j&, rng, s, arr()
FItem = InputBox(" number of first items?")
LItem = InputBox(" number of last items?")
If Not IsNumeric(FItem) Or Not IsNumeric(LItem) Then
    MsgBox "Invalid value"
    Exit Sub
End If
lr = Cells(Rows.Count, "C").End(xlUp).Row
rng = Range("A2:E" & lr).Value
ReDim arr(1 To lr - 1, 1 To 2)
For i = 1 To lr - 1
    s = Split(rng(i, 3))
    For j = 0 To UBound(s)
        If j + 1 <= FItem Or j + 1 >= UBound(s) - LItem + 2 Then
            arr(i, 1) = arr(i, 1) & " " & s(j)
        Else
            arr(i, 2) = arr(i, 2) & " " & s(j)
        End If
    Next
Next
Range("B2").Resize(UBound(arr), 2).Value = arr
End Sub
In case do not want inputboxes, but harcode the number to, i.e, first =2, last = 2:
Replace this:
VBA Code:
FItem = InputBox(" number of first items?")
LItem = InputBox(" number of last items?")
by this:
VBA Code:
FItem = 2
LItem = 2
 
Upvote 0
@bebo021999 you're awsome!

two choices are great especially the first. I want implementing your code in multiple sheets as I mentioned in OP & post#3 . can I copy the items instead of move from column C to column B? I mean to should not delete the items from column C should keep the orginal data in column C .

thanks
 
Upvote 0
OK. Try:
VBA Code:
Option Explicit
Sub test()
Dim lr&, FItem, LItem, i&, j&, rng, s, arr(), ws As Worksheet
FItem = InputBox(" number of first items?")
LItem = InputBox(" number of last items?")
    If Not IsNumeric(FItem) Or Not IsNumeric(LItem) Then
        MsgBox "Invalid value"
        Exit Sub
    End If
For Each ws In Sheets
    lr = ws.Cells(Rows.Count, "C").End(xlUp).Row
    rng = ws.Range("C2:C" & lr).Value
    ReDim arr(1 To lr - 1, 1 To 1)
    For i = 1 To lr - 1
        s = Split(rng(i, 1))
        For j = 0 To UBound(s)
            If j + 1 <= FItem Or j + 1 >= UBound(s) - LItem + 2 Then
                arr(i, 1) = arr(i, 1) & " " & s(j)
            End If
        Next
    Next
    ws.Range("B2").Resize(UBound(arr), 1).Value = arr
Next
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,215,366
Messages
6,124,516
Members
449,168
Latest member
CheerfulWalker

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