Separating a Variant "String" into separate columns

engineerseay

New Member
Joined
Mar 13, 2018
Messages
7
I have a large amount of data to separate into columns. I am fairly new to VBA, so I need help figuring it out. I have done a fair amount of searching which led to many failed attempts.


So the current column data looks like this: 2 P011954 88135 SKIN, COLONIST, TEXTURED, 6 PANEL, 3/0 X 8/0

I want to separate it into 4 different columns. 2, P011954, 88135, & (SKIN, COLONIST, TEXTURED, 6 PANEL, 3/0 X 8/0) remaining.

currently this is my code: (cant figure out how to add a screen shot) I know it is not really doing anything at the moment.



Code:
'** PROGRAM PURPOSE:SEPARATE QTY, PART #, TITAN #, AND DISCRIPTION INTO SEPERATE CELLS.
'** INPUTS/OUTPUTS: STING OF DATA/ SEPARATED DATA
'** NOTES:
'**
'**************************************************************************************************
Sub SEPARATE()
    Dim I As Integer
    Dim qty, part, titan, discription, J, n, x As String
    
    Application.ScreenUpdating = False
    For Each Sheet In Worksheets
        Sheet.Activate
            
            I = 5                                  'I is used to indentify starting row.
            
            Columns.AutoFit
        
            For I = 5 To 100         'Continue loop until row 100
                J = Cells(I, 4).Value
                qty = Split(J, " ", 2)
            
                n = Cells(I, 5).Value
                part = Split(n, " ", 2)
                'ActiveCell.Resize(1, UBound(part) + 1).Offset(0, 1) = part   -Tried this and it did not work
                
                x = Cells(I, 6).Value
                titan = Split(x, "S", 2)
                'ActiveCell.Resize(1, UBound(titan) + 1).Offset(0, 1) = titan
                        
                
            Next I
                            
            
    Columns.AutoFit
    Next Sheet
    Application.ScreenUpdating = True
    
    
End Sub
 
Last edited by a moderator:
Re: Separating a Variant "String" into separate columns - NEED HELP PLEASE & THANKS

THAT WORKED PERFECT! Thank you!!!! I attempted to slightly modify your first code, which sort of worked, but it deleted the work "skin" from each description. The code you just posted is exactly what I was looking for!


Thanks Agian!
Sorry, didnt see this until now. But the code i modified for you will only grab the first 3 values seperated by blanks and then the 4th value will be whatever is left. Since your values did not follow the format from the example then it will not work for values that have a space in the parsed values themselves. It looks like you should just use the the solution posted by Rick.

Also regarding the subscript being out of range, might be because the array returned from the split function uses base index 1 instead of 0. Although when I tested it, it worked fine so perhaps you are setting the option for arrays to be based at 1 but i doubt that. Your code is probably a little different... regardless, go with the other solution.
 
Last edited:
Upvote 0

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
Re: Separating a Variant "String" into separate columns - NEED HELP PLEASE & THANKS

Also regarding the subscript being out of range, might be because the array returned from the split function uses base index 1 instead of 0.
The lower bound for an array created by the Split function is always 0 (even if you are using Option Base 1).
 
Upvote 0

Forum statistics

Threads
1,214,922
Messages
6,122,281
Members
449,075
Latest member
staticfluids

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