Link to home
Start Free TrialLog in
Avatar of Luis Diaz
Luis DiazFlag for Colombia

asked on

Excel VBA: retain values of a column based on reported string

Hello experts,

The following procedure allows me to retain specific string based on prefix retention.
Sub Prefix_Retention()
    
    Dim strCol As String
    Dim lngLastRow As Long
    Dim lngRow As Long
    Dim intPos As Integer
    Dim strPrefix As String
    
    On Error GoTo Error_Routine
    
        strCol = InputBox("Please enter the column letter in which you want to remove the specific string", "Choose Column Letter")
        If strCol = vbNullString Then
            MsgBox "Unable to proceed, please properly report required information", vbCritical
            Exit Sub
        End If

        lngLastRow = Range(strCol & "1048576").End(xlUp).Row
        strPrefix = InputBox("Please enter the Prefix that you want to retain. It should be enter in capital letters", strPrefix)
        If strPrefix = vbNullString Then
            MsgBox "Unable to proceed, please properly report required information", vbCritical
            Exit Sub
        End If

    For lngRow = 1 To lngLastRow
        intPos = InStr(1, UCase(Cells(lngRow, strCol).Value), strPrefix)
        If intPos > 0 Then
            Cells(lngRow, strCol).Value = Trim(Mid(Cells(lngRow, strCol).Value, intPos))
        End If
    Next
    
    Exit Sub
Error_Routine:
    MsgBox Err.Description, vbExclamation, "Something went wrong!"
    
End Sub

Open in new window

1.Differenciate the inputcolumn and ouputcolumn in which should be reported the result.

 strStartChar = Split(strInput, ";")(0)
    strEndChar = Split(strInput, ";")(1)
    
    strIO = InputBox("Please enter the column letters separated by a colon for the data in the form 'InputColum:OutputColumn'", "Choose Column Letters", "A:B")
        
    strColInput = Split(strIO, ":")(0)
    strColOutput = Split(strIO, ":")(1)
    
    If (Not IsValidColumnLetter(strColInput)) Or (Not IsValidColumnLetter(strColOutput)) Then
        MsgBox "You entered Invalid Column Letters.", vbExclamation
        Exit Sub
    End If
    
    lngLastRow = Range(strColInput & "1048576").End(xlUp).Row
    
    For lngRow = 1 To lngLastRow
        Cells(lngRow, strColOutput).Value = getString(Cells(lngRow, strColInput).Value, strStartChar, strEndChar)
    Next
    

Open in new window


2-Instead of reporting prefix string I would like to report the first character example: Value = te-toto
If I report - the result would be toto.

If you have questions, please contact me.
Avatar of Martin Liss
Martin Liss
Flag of United States of America image

Please attach a sample workbook.
Avatar of Luis Diaz

ASKER

Please find attached sample version.
Result in column B with the assumption that I report e which is the first letter of the string to retain.
Retain-based-on-string_20200226_223.xlsx
Given the fact that I am going to manage both procedures in my add-in, I was wondering if we can firstly correct the following procedure in order avoid the replacement in the same column but in another column instead, the next available or the one reported:

Sub Prefix_Retention()
    
    Dim strCol As String
    Dim lngLastRow As Long
    Dim lngRow As Long
    Dim intPos As Integer
    Dim strPrefix As String
    
    On Error GoTo Error_Routine
    
        strCol = InputBox("Please enter the column letter in which you want to remove the specific string", "Choose Column Letter")
        If strCol = vbNullString Then
            MsgBox "Unable to proceed, please properly report required information", vbCritical
            Exit Sub
        End If

        lngLastRow = Range(strCol & "1048576").End(xlUp).Row
        strPrefix = InputBox("Please enter the Prefix that you want to retain. It should be enter in capital letters", strPrefix)
        If strPrefix = vbNullString Then
            MsgBox "Unable to proceed, please properly report required information", vbCritical
            Exit Sub
        End If

    For lngRow = 1 To lngLastRow
        intPos = InStr(1, UCase(Cells(lngRow, strCol).Value), strPrefix)
        If intPos > 0 Then
            Cells(lngRow, strCol).Value = Trim(Mid(Cells(lngRow, strCol).Value, intPos))
        End If
    Next
    
    Exit Sub
Error_Routine:
    MsgBox Err.Description, vbExclamation, "Something went wrong!"
    
End Sub

Open in new window

You attached an xlsx workbook and so it there won't be any code in it. Did you mean to do that?
You can take as a reference the first comment of the question. I posted the procedure.
I'm still a little confused. In your first post you have two pieces of code: Sub Prefix_Retention and a snippet of code below it. Are you saying that you want the Prefix_Retention sub to be modified so that it uses code like the snippet?

2-Instead of reporting prefix string I would like to report the first character example: Value = te-toto
If I report - the result would be toto.
You are already doing that. Let's say that in the current code you enter "TES" as the prefix that you want to retain. It will then find that starting in position 2 and so it will report "test1". However the same would be true if you said the prefix that you wanted to retain was "T", "TE" or "TEST".
I think that the attached workbook may be what you want. Note that the starting character(s) may be either upper case or lower case and the columns selected may be either letters or numbers. There is also a validation that checks if the selected input column has data. And finally, I think I mentioned in one of your other questions that your error routine could be improved and I'd be happy to do that if you want me to.
29173810.xlsm
Thank you very much Martin. I will look at this weekend and let you know.
Hi Martin,

I tested your proposal and it works.
I will be happy if you can help me to improve the error routine.

Thank you for your help.
ASKER CERTIFIED SOLUTION
Avatar of Martin Liss
Martin Liss
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You’re welcome and I’m glad I was able to help.

If you expand the “Full Biography" section of my profile you’ll find links to some articles I’ve written that may interest you.

Marty - Microsoft MVP 2009 to 2017
              Experts Exchange Most Valuable Expert (MVE) 2015, 2017
              Experts Exchange Top Expert Visual Basic Classic 2012 to 2019
              Experts Exchange Top Expert VBA 2018, 2019
              Experts Exchange Distinguished Expert in Excel 2018