Link to home
Create AccountLog in
Avatar of Kim Hart
Kim HartFlag for United States of America

asked on

What is the best way to split information in a cell that contains numbers and words while keeping the words together?

I can split this up using text to columns but it put the information in 5 different cells. Is there a better way to just split the numbers from the letters? I have a lot of data to manipulate and only want the letters for description
0013976475 00001 Chip Mill Maintenance
Avatar of Kim Hart
Kim Hart
Flag of United States of America image

ASKER

Excel 2016

I have data like the example above that has 10 numbers that I want to remove from the data. Some of the data in the list does not contain the numbers so it is OK like it is

Avatar of Tom Farrar
I believe Power Query has the option to split numbers from letters.  In Power Query : 
Transform>Split  Column> By Digit to Non Digit


@Tom
That's true but it will not replace the original data in the first place rather it would insert a new table with the transformed data. If intention is to replace the data in place, VBA is the only way to achieve this.
Also, from the description it seems only removing the first 10 digits from the string is required if I got it right.
i.e. from the original string 0013976475 00001 Chip Mill Maintenance, the expected output is 00001 Chip Mill Maintenance.
Is my assumption correct Kim?
This assumes that the data is in column 'A' starting in row 1 and that you want the results in columns B to D. Columns B and C should be formatted as Text.
Sub SplitData()
Dim lngLastRow As Long
Dim lngRow As Long
Dim strParts() As String
Dim lngPart As Long
Dim ws As Worksheet

Set ws = ActiveSheet
With ws
    lngLastRow = .Range("A1048576").End(xlUp).Row
    For lngRow = 1 To lngLastRow
        strParts = Split(.Cells(lngRow, "A"), " ")
        .Cells(lngRow, "B") = strParts(0)
        .Cells(lngRow, "C") = strParts(1)
        For lngPart = 2 To UBound(strParts) - 1
            .Cells(lngRow, "D") = .Cells(lngRow, "D") & strParts(lngPart) & " "
        Next
        .Cells(lngRow, "D") = .Cells(lngRow, "D") & strParts(UBound(strParts))
    Next
End With
End Sub

Open in new window

The  00001 would also be removed only keeping the description. I can use text to columns to split and then only keep the description. I was just wondering if there was a better way to accomplish this.
 
This assumes that the data is in column 'A' starting in row 1. It ignores the numbers and puts the description in column 'B'.
Sub SplitData()
Dim lngLastRow As Long
Dim lngRow As Long
Dim strParts() As String
Dim lngPart As Long
Dim ws As Worksheet

Set ws = ActiveSheet
With ws
    lngLastRow = .Range("A1048576").End(xlUp).Row
    For lngRow = 1 To lngLastRow
        strParts = Split(.Cells(lngRow, "A"), " ")
        For lngPart = 2 To UBound(strParts) - 1
            .Cells(lngRow, "B") = .Cells(lngRow, "B") & strParts(lngPart) & " "
        Next
        .Cells(lngRow, "B") = .Cells(lngRow, "B") & strParts(UBound(strParts))
    Next
End With
End Sub

Open in new window

If you only want to replace the numbers from the string, you may try the following code.
In the attached click the button called "Get Description" to run the code.

Sub GetDescription()
Dim ws      As Worksheet
Dim lr      As Long
Dim x       As Variant
Dim y       As Variant
Dim i       As Long
Dim Matches As Object

Set ws = ThisWorkbook.Worksheets("Sheet1")
lr = ws.Cells(Rows.Count, 1).End(xlUp).Row
x = ws.Range("A1").CurrentRegion.Value
ReDim y(1 To lr, 1 To 1)
With CreateObject("VBScript.RegExp")
    .Global = False
    .Pattern = "^\d+ \d+"
    For i = 1 To UBound(x, 1)
        If .Test(x(i, 1)) Then
            Set Matches = .Execute(x(i, 1))
            y(i, 1) = VBA.Trim(Replace(x(i, 1), Matches(0), ""))
        Else
            y(i, 1) = x(i, 1)
        End If
    Next i
End With
ws.Range("A1").Resize(lr, 1).Value = y
End Sub

Open in new window



Get Description.xlsm
Also if your strings always follow the following pattern...
<10 digits number><space><5 digits number><space><desired description>
and assuming your strings start from A2, you may try the following formula in B2
=IF(ISNUMBER(LEFT(A2)+0),MID(A2,18,255),A2)

Open in new window

and then copy it down. Then copy all the formula cells and paste them back to the original column (column A in this case) as VALUES only and delete the formula cells in the end.


If the only numbers in the string are at the beginning, ie not in the description part, you can use this code to remove all numbers from the selected range.

Sub RemoveNumbers()
For I = 0 To 9
    Selection.Replace What:=I, Replacement:="", LookAt:=xlPart, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False
Next I
For Each Cell In Selection
    Cell.Value = Trim(Cell.Value)
Next Cell
End Sub

Open in new window


Some of my data has the string that I included above and some of it does not. I did figure out how to separate the numbers from the text by using text to columns but that did not solve my issue because the data that had the numbers does not contain enough text to match it with other data after the numbers have been removed. In the end I want the data to match in a pivot table so that it no longer shows in the view if there is a debit and credit. All of the responses above have been very helpful but I think now I have to figure out the matching issue. I have included a screenshot of an example. Thanks!
GE to perform the Annual Inspection TG 5

0014956695 00001 GE to perform the Annua

So, in summary, the numbers are not of value to you.  You just want to remove them so you can match the text showing on the debits and credits.  And the problem is sometimes the text gets truncated when the number is removed.  Is this correct?
As always, a sample file would be helpful.
ASKER CERTIFIED SOLUTION
Avatar of Kim Hart
Kim Hart
Flag of United States of America image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
So, I do not see a clear way of matching the debit/credit "Pivot Description" so the debit eliminates the credit.  You could parse out the Pivot Description so only the first 20 characters show, and in the case of your example, they would match up.  But I suspect this would create other issues with other transactions.  Also I think there is one example (see my spreadsheet, sheet1, rows 17-18) where this would not work as the Pivot Descriptions are totally different.  The only way something like this would work would be if there were strict requirements on how transaction fields were coded.  Even then there would be instances where things that should match up, won't.  

The process of matching up may be a manual process that could be handled by adding a Absolute Value Column and sorting on this to see matching values.  I did this in sheet1, columns Absolute Value and Match.  This is an ugly way of matching, but it can work better than scanning all transactions.  

Another way to approach it would be using the system assigned columns to bring transactions together.  For instance, cost center, cost element, Area, Unique, etc.  This may not totally match transactions in a satisfactory way to eliminate values in the pivot table, but working with system assigned fields provides a more reliable way of ensuring someone has not used descriptions that do not match up .  

My recommendation is you continue to look at options like those I suggest to see the best way to get the reconciliation to match up.  I know this does not give you what you asked for, but even by parsing the code (as suggested for matching up the example you provided) will most likely end up creating more problems than solving.

If any other experts have suggestions, please chime in.....

S--ACCOUNTING-CheckBook-Month-End--2.xlsx