Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

Parsing one value from 1 cell to multiple cell

0

Hello,

Hope you have a wonderful day!

I have a table that I need to break down. The table has about 170 rows where there is one column that contains several values ​​in one cell that I want to parse. Please review the file for details on the expected results. The scenario is similar to the condition I am experiencing. So the table I have is in format of tbl 1 and I need to convert it to format of tbl 2

Please let me know if my question is still not clear to you

Answer
Discuss

Discussion

Hi again Fida

Are the combined values always separated by space-pipe-space-space (" | ")? Are you looking for a non-macro way or don't you care? Also is your real data in Excel tables (or just normal cells / ranges like the sample file)? (I'm busy today but might find time to look at your reply)
John_Ru (rep: 6142) Feb 29, '24 at 5:48 am
Hello John,

Same busy as well here like crazy 😥, and yes the combined value is separated spice-pipe-space but there is also from another source which separated like ; & space (apple; strawberry; pear)

For the methode I am fine either using macro or non-macro way, but I prefer non macro way please. I think the xlsm file type could make the workbook become slow in performance because the real dataset has 170 rows but when parsed it is expected to become more than 900 rows. Had experience where I put liitle code on macro just for automated highliht for text and the excel file became slower unfortunately
Fida_mutia (rep: 28) Feb 29, '24 at 10:33 am
Thanks Fida. I already creatwd a macri solution but didn't have time to post sn explanation (might have chance in a few hours). That solution allows you to change to semicolon ; or whatever. Will post the solution and explain when I get chance
John_Ru (rep: 6142) Feb 29, '24 at 11:30 am
P. S. the macro method will be WAY faster than doing it by hand! (As was my separate Connectors solution) 
John_Ru (rep: 6142) Feb 29, '24 at 11:32 am
okay then, waiting for the answer. thanks in advance!
Fida_mutia (rep: 28) Feb 29, '24 at 12:30 pm
Add to Discussion

Answers

0
Selected Answer

Fida

The attached revised file contains this macro (in Module1), with comments to show what happens:

Sub SplitCell()

    Dim Delim As String, InRng As Range, OutRng As Range
    Dim m As Long, n As Long, p As Long

    ' set up divider and where data start and is sent (top left cell)
    Delim = "|"
    Set InRng = Range("A2")
    Set OutRng = Range("F2")

    ' speed up for bigger data sets
    Application.ScreenUpdating = False
    ' clear output
    OutRng.CurrentRegion.Clear

    ' expand top left in to extent of range
    Set InRng = InRng.CurrentRegion

    ' copy header row
    InRng.Rows(1).Copy Destination:=OutRng


    ' set offset counter
    p = 1

    'loop down input range
    For m = 2 To InRng.Rows.Count
        'create an array, splitting text by stated divider
        r = Split(InRng.Cells(m, 2), Delim)
        'loop thorugh that araay
        For n = 0 To UBound(r, 1)
            'write the common values
            OutRng.Offset(p, 0) = InRng.Cells(m, 1)
            'write the split values, one cell to the right
            OutRng.Offset(p, 1) = Trim(r(n))
            ' increment the row counter
            p = p + 1
        Next n
    Next m

    ' write borders for the output
    OutRng.CurrentRegion.Borders.LineStyle = xlContinuous

    ' restore
    Application.ScreenUpdating = True

    MsgBox "Wrote " & p & " rows to " & OutRng.Address(0, 0) & " and down"

End Sub

With this file, press the key combination Ctrl+Shift+J and the macro will run, splitting down column B (in what was your tbl1) and placing the results in F2 and down.

You can change the early portions in bold above to suit different in/ out locations and different delimiters. e.g. you could change Delim = "|" to read Delim = "&"  if your data is separated by the ampersand & instead.

Hope this works for you (and is fast enough with your larger real data). If so, please re,me,mber to mark this Answer as Selected.

Discuss

Discussion

Thanks alot for the solution, John! It runs smoothly when there are only two columns to parse. However, I just did attemp to parse around eight columns and it didn't yield the expected outcome. I've adjusted the end cell in the code. Is there a limit to the number of columns the code can parse??
Fida_mutia (rep: 28) Feb 29, '24 at 10:55 pm
Actually, it seems there are other use case here that I haven't yet explained. I sent those two tables as if they were identical cases, but there's another that I'd like to address as well. Should I create a separate question for this?
For this use case, your code has resolved it perfectly. So I need another use case to be resolved please?
Fida_mutia (rep: 28) Mar 1, '24 at 12:59 am
Fida, glad that worked for you and thanks for selecting my Answer. 

The Answer simply addresses your question and (in this case) isn't extendable to multiple columns.

You should ask a new question (and may refer back to this) but remember the Forum is about answering questions when you hit a problem. We generally expect you to attempt to solve your own problem first. 
John_Ru (rep: 6142) Mar 1, '24 at 1:20 am
To let you know data extraction scenarios I'm talking about, it's related to the setup of charging stations. I think you already know about this as you live in UK where most cars are EV. So the configuration is like:

1 charge station ID = 1 EVSE ID = 1 connector
1 charge station ID = 1 EVSE ID = 2 connector
1 charge station ID = 2 EVSE ID = 2 connector
1 charge station ID = 3 EVSE ID = 3 connector

this is the configuration that related to the scenarios I am referring. I posted this question because I want to extract a data that is in location level

location level table usually have around 180 locations, it depends on the charging stations location total. Thus it could have 180 rows
charging level table here has around 900 rows. For 1 location can have 1 3 to 25 charging station
EVSE level table has around 1200 rows from the extraction of the charging and 1500 rows for connector level

I hope this gives summary of the data flow that I am gonna ask you in another post, if thats okay?
Fida_mutia (rep: 28) Mar 1, '24 at 1:21 am
Oh you just replied to my prev comment. Okay I'll post again for the question
Fida_mutia (rep: 28) Mar 1, '24 at 1:39 am
Add to Discussion


Answer the Question

You must create an account to use the forum. Create an Account or Login