Dealing with colon separated values when values themselves contain colons

James

I have got data in the following form : username:hash:email:ip

but there are no delimiters for the values themselves.

My problem is that when importing the data into Excel when a username contain a colon or more it make a new column and thus it makes this data very hard to handle for me.

One option is adding delimiters to the values like ". I guess that we could make a regular expression that matches the hash and the colon before it and then enclose everything before this regular expression into quotes but I don't really know how to do that.

Example :

Image

user385793

This will fix your import providing only the username could have one or more colons. Explanations are in code comments. I chose working with an array because it's faster than working on the worksheet and your sample data showed at least 47K entries. If any other import field could have colons then you will need to rework it to compensate for them.

Option Explicit

Sub Fix_Import()

    Dim i As Long, j As Long, arr As Variant

    'work with the data area expanding outward from Range("A1")
    With Worksheets("sheet1").Cells(1).CurrentRegion

        'if there are no split usernames  then don't continue
        If .Columns.Count = 4 Then Exit Sub

        'collect the imported values (minus the column headers) into an array
        arr = .Offset(1, 0).Resize(.Rows.Count - 1, .Columns.Count).Value2

    End With

    'loop through the 'rows' of the array
    For i = LBound(arr, 1) To UBound(arr, 1)

        'when everything is correct, the 5th array element will be blank
        Do While arr(i, 5) <> vbNullString

            'loop through this 'row' of the array
            For j = 1 To UBound(arr, 2)

                'do different stuff depending on which of the array's 'columns' you are working on
                Select Case True
                    Case j = 1
                        arr(i, j) = Join(Array(arr(i, j), arr(i, j + 1)), Chr(58)) 'join on colon
                    Case j < UBound(arr, 2)
                        arr(i, j) = arr(i, j + 1) 'make this array element the same as the next
                    Case j = UBound(arr, 2)
                        arr(i, j) = vbNullString 'make the final element a null string
                End Select

            Next j
        Loop

    Next i

    'put the array elements back on the worksheet
    Worksheets("sheet1").Cells(2, 1).Resize(UBound(arr, 1), UBound(arr, 2)) = arr

End Sub

Before
enter image description here
After
enter image description here

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

IFS=: leads to different behavior while looping over colon-separated values

Dealing with missing values when calculating weighted mean

Dealing with Null values oustystems

Find rows that contain same value inside comma separated values

Is there a standard or recommended way to use parts of values in JSON that are separated by colons?

how can I convert values to a meaning separated with a colon (double point)

Dealing with null values when pd.merge

Using VLOOKUP or MATCH for cells that contain comma separated values

How to extract a specific column from : ( colon ) separated values?

How to place comma separated values into newline and remove all the id's before including colon

Convert text file with colon-separated values to html table

MySQL sort values that contain two numbers separated by "x"

Dealing with no data values

Are implicit parameter values implicit values themselves?

Notepad++ split numeric values separated with semi-colons and commas keeping the lines having single pairs intact

sql server - multiply values by themselves

How do I reformat .JSON key value pairs to comma-separated values using equals instead of colon?

Have MySQL return JSON keys which themselves contain certain values

How to extract colon separated values from the same line?

Get column values separated by semi colon

Accounting for "missing" values, dealing with instances when 0's should be entered

Dealing with uninstantiated values in Prolog

Dealing with missing values when converting a list of dictionaries into a dataframe

Dealing with null values position when sorting an array based on numeric values

How to split a string with comma separated pairs of colon separated keys and values and pass it into a javascript object?

How to get values from a list of dictionaries, which themselves contain lists of dictionaries in Python

Excel - How to split semi-colon separated values in multiple columns into rows

JSONEncoder encodes nil values when dealing with generic types

load colon separated values into a dbms_sql.varchar2_table