+ Reply to Thread
Results 1 to 15 of 15

Isolating a certain area by stripping out unwanted charachters and numbers.

Hybrid View

CDandVinyl Isolating a certain area by... 07-23-2022, 02:46 PM
oeldere Re: Isolating a certain area... 07-23-2022, 03:18 PM
CDandVinyl Re: Isolating a certain area... 07-23-2022, 05:06 PM
TMS Re: Isolating a certain area... 07-23-2022, 06:37 PM
jolivanes Re: Isolating a certain area... 07-23-2022, 06:38 PM
CDandVinyl Re: Isolating a certain area... 07-23-2022, 06:55 PM
TMS Re: Isolating a certain area... 07-23-2022, 07:07 PM
TMS Re: Isolating a certain area... 07-23-2022, 07:08 PM
CDandVinyl Re: Isolating a certain area... 07-23-2022, 07:21 PM
TMS Re: Isolating a certain area... 07-23-2022, 07:25 PM
oeldere Re: Isolating a certain area... 07-24-2022, 04:29 AM
CDandVinyl Re: Isolating a certain area... 07-26-2022, 02:12 AM
TMS Re: Isolating a certain area... 07-26-2022, 03:09 AM
CDandVinyl Re: Isolating a certain area... 07-26-2022, 11:10 AM
TMS Re: Isolating a certain area... 07-26-2022, 12:36 PM
  1. #1
    Forum Contributor
    Join Date
    01-18-2013
    Location
    London
    MS-Off Ver
    Excel 2003
    Posts
    641

    Isolating a certain area by stripping out unwanted charachters and numbers.

    I'm trying to strip our extraneous elements of a reference number to leave just the numerical section.

    Sometimes the reference number starts with letter , sometimes not. Each ends with an under score followed by other numbers and letters. Essentially , I need to remove any and all letters at the beginning and remove everything including and after the underscore.

    For example :

    CDU75945_CDbx185
    CDU153900_LPsbx117
    SU151683_CDbxMV1
    141972_CDbx262
    U151571_CDbx262
    CDU93090_Boxbx194
    U151845_7bxMV8
    160884_CDsbx157
    CDU160868_12bx192


    Would become :


    75945
    153900
    151683
    141972
    151571
    93090
    151845
    160884
    160868


    The amount of rows containing these references would vary , as would the column they are in , so a message box requesting the column to work on would be helpful.

    Can someone suggest some code to satisfy this?

    I'll put a sample file below.

    Grateful for any advice.

    Attached Files Attached Files
    Last edited by CDandVinyl; 07-23-2022 at 05:00 PM.
    Using Excel , Word and Access 2003 - For the whole of 2024

  2. #2
    Forum Expert
    Join Date
    05-30-2012
    Location
    The Netherlands
    MS-Off Ver
    Office 365
    Posts
    14,987

    Re: Isolating a certain area by stripping out unwanted charachters and numbers.

    Non VBA solution


    F2=MAX(IFERROR(MID(E2,ROW($1:$100),COLUMN($A:$Z))*1,""))

    confirm with CTRL+Shift + Enter


    See the attached file.
    Attached Files Attached Files
    Notice my main language is not English.

    I appreciate it, if you reply on my solution.

    If you are satisfied with the solution, please mark the question solved.

    You can add reputation by clicking on the star * add reputation.

  3. #3
    Forum Contributor
    Join Date
    01-18-2013
    Location
    London
    MS-Off Ver
    Excel 2003
    Posts
    641

    Re: Isolating a certain area by stripping out unwanted charachters and numbers.

    Ok thanks. I think a VBA solution giving column choice and auto-locating the last row would be the best fit , but this certainly does the job.


  4. #4
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,557

    Re: Isolating a certain area by stripping out unwanted charachters and numbers.

    @oeldere: brilliant. I have no idea how you approach a problem like this and come up with a solution like that. Very clever. Having worked through it, it seems it relies on the first numeric string being the longest/largest. Which seems fine for the data presented, and I’m sure that must be fairly representative.
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  5. #5
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,704

    Re: Isolating a certain area by stripping out unwanted charachters and numbers.

    Function borrowed from Scott Huish (https://www.mrexcel.com/board/thread...string.498357/)

    Function removeAlpha(r As String) As String
    With CreateObject("vbscript.regexp")
        .Pattern = "\D"
        .Global = True
        removeAlpha = .Replace(r, "")
    End With
    End Function
    Re: so a message box requesting the column to work on would be helpful.
    When the inputbox pops up, select any cell in the column in question.

    Sub Clean_It_Up()
    Dim c As Range, col As Long, lr As Long
        With Sheets("Sheet2")    '<---- Change as required
            col = Application.InputBox(Prompt:="Please select any cell in the column to be cleaned.", Title:="Please select a cell.", Type:=8).Column
            lr = .Cells(.Rows.Count, col).End(xlUp).Row
            Application.ScreenUpdating = False
                For Each c In .Range(.Cells(2, col), .Cells(lr, col))
                    c.Value = removeAlpha(Left(c.Value, InStr(c.Value, "_") - 1))
                Next c
        End With
    Application.ScreenUpdating = True
    End Sub
    The inherent weakness of the liberal society: a too rosy view of humanity.

  6. #6
    Forum Contributor
    Join Date
    01-18-2013
    Location
    London
    MS-Off Ver
    Excel 2003
    Posts
    641

    Re: Isolating a certain area by stripping out unwanted characters and numbers.

    Many thanks This works fine.

    By the way , is it possible to modify the code to run on the first sheet , whatever it is named?

  7. #7
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,557

    Re: Isolating a certain area by stripping out unwanted charachters and numbers.

    Simple VBA solution based on oeldere's formula approach.

    Option Explicit
    
    Sub ExtractNumber()
    
    Const cSourceColumn As String = "E"
    
    Dim lLR As Long
    lLR = Range(cSourceColumn & Rows.Count).End(xlUp).Row
    
    Application.ScreenUpdating = False
    
    ' add array formula for first row
    Range(cSourceColumn & 2).Offset(, 1).FormulaArray = _
        "=MAX(IFERROR(MID(E2,ROW($1:$100),COLUMN($A:$Z))*1,""""))"
    
    ' copy array formula down
    With Range(cSourceColumn & 2).Offset(, 1)
        .Copy Range(cSourceColumn & 3 & ":" & cSourceColumn & lLR).Offset(, 1)
    End With
    
    ' convert to values
    With Columns(cSourceColumn).Offset(, 1)
        .Value = .Value
    End With
    
    Application.ScreenUpdating = True
    
    End Sub

  8. #8
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,557

    Re: Isolating a certain area by stripping out unwanted charachters and numbers.

    By the way , is it possible to modify the code to run on the first sheet , whatever it is named?
    Probably refer to Sheets(1)

  9. #9
    Forum Contributor
    Join Date
    01-18-2013
    Location
    London
    MS-Off Ver
    Excel 2003
    Posts
    641

    Re: Isolating a certain area by stripping out unwanted charachters and numbers.

    Very good. Thanks all!

  10. #10
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,557

    Re: Isolating a certain area by stripping out unwanted charachters and numbers.

    You're welcome. Thanks for the rep.

  11. #11
    Forum Expert
    Join Date
    05-30-2012
    Location
    The Netherlands
    MS-Off Ver
    Office 365
    Posts
    14,987

    Re: Isolating a certain area by stripping out unwanted charachters and numbers.

    Thanks for the rep.

    Glad I could help.

    Thanks for marking the question solved.

  12. #12
    Forum Contributor
    Join Date
    01-18-2013
    Location
    London
    MS-Off Ver
    Excel 2003
    Posts
    641

    Re: Isolating a certain area by stripping out unwanted charachters and numbers.

    For my own curiosity , how would I select cell 1 in the column defined by the variable col in this coding provided by jolivanes?

    Sub Clean_It_Up()
    Dim c As Range, col As Long, lr As Long
        With Sheets("Sheet2")    '<---- Change as required
            col = Application.InputBox(Prompt:="Please select any cell in the column to be cleaned.", Title:="Please select a cell.", Type:=8).Column
            lr = .Cells(.Rows.Count, col).End(xlUp).Row
            Application.ScreenUpdating = False
                For Each c In .Range(.Cells(2, col), .Cells(lr, col))
                    c.Value = removeAlpha(Left(c.Value, InStr(c.Value, "_") - 1))
                Next c
        End With
    Application.ScreenUpdating = True
    End Sub
    I've been wrestling with it , but can't find the correct syntax. The variable col is defined by an input box , and to complete the macro I'd like it to select the cell at the top of the column on completion.

    For example , if the column held by the variable is G , I want to select G1 at the end of the macro.

    As it's a variable I'm struggling to find the code...

    Grateful for any advice.


  13. #13
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,557

    Re: Isolating a certain area by stripping out unwanted charachters and numbers.

    This:

    Sub Clean_It_Up()
    Dim c As Range, col As Long, lr As Long
        With Sheets("Sheet2")    '<---- Change as required
            col = Application.InputBox(Prompt:="Please select any cell in the column to be cleaned.", Title:="Please select a cell.", Type:=8).Column
            .Cells(1, col).Select
        End With
    End Sub

  14. #14
    Forum Contributor
    Join Date
    01-18-2013
    Location
    London
    MS-Off Ver
    Excel 2003
    Posts
    641

    Re: Isolating a certain area by stripping out unwanted charachters and numbers.

    That's it - perfect. Many thanks for your help.


  15. #15
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,557

    Re: Isolating a certain area by stripping out unwanted charachters and numbers.

    You're welcome.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Help stripping unwanted formatting from data
    By Martin100 in forum Excel General
    Replies: 12
    Last Post: 08-04-2019, 05:46 AM
  2. [SOLVED] Stripping Numbers from Text String.
    By JRidge in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-05-2014, 05:20 AM
  3. [SOLVED] Isolating numbers from string
    By omaribookal in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 07-03-2014, 03:08 AM
  4. [SOLVED] Help: How to remove unwanted extra area
    By zaidu87 in forum Excel General
    Replies: 6
    Last Post: 11-17-2012, 05:42 AM
  5. Isolating an area of the spreadsheet
    By leroy boyce in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 10-17-2012, 06:12 PM
  6. Stripping out unwanted data
    By mpb1955 in forum Excel General
    Replies: 5
    Last Post: 06-15-2008, 04:12 AM
  7. Stripping Letters from Numbers
    By luke.thomas in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 11-09-2007, 03:28 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1