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

Automated data parsing

0

Hello,

I need to parse data from data export on my company portal so that i can do deeper analysis. The pattern is where I need to break down the combined values in one cell. Please review the attached Excel file for more details.

(Edited)

Below are the answers for each question :

1. Yes, the IDs listed here are not the actual IDs for the charging stations on the data export result. I have altered some of the letters and numbers.

2. Apologies for not realizing that the IDs were separated by space on the first file I attached here. It appears that the semicolon disappeared when I was attempting to change the original ID. 

Each ID is separated by a semicolon followed by a space (; ). I have edited the attachment, please review

3. Exactly, I missed this one as well; it was a mistake from the find/replace I did. I've edited this in the new attachment

4. The data export that I need to parse is from the EV charging portal in my company, and I only download it when I need to check for new or removed data. So the file may vary depending on when I download it from the portal

Thanks alot in advance 

Answer
Discuss

Discussion

Hi again Fidia

You've perhaps inadvertently broken one of the rules of the Forum- the details of your question should be in the question text (not within a file) - users and contributors should not have to look in the file to see what you're trying to do.

Not sure I'll have time to answer today or tomorrow but please respond on four further points too-

1) Am I right to assume that the prefix AB-ABC- is a test one and you'll want to change that in real life?

2) will src_ids always be separated by a (single?) space and will the suffix number (e.g. 1) always be separated from that by a dash "-"?  

3) for the Ikea Wembley location, the last src_id is AB-ABC-ETACW2243920T0567-1 but your expected reslts lists it as AB-ABC-EABCD2243920T0567-1  and a physical reference of ABCD2243920T0567 - I assume the E is just a Find/Replace mistake and should be assumed to be ABCD, right?

4) will you be picking individual export files to parse or will always it be the same filename (and/or the same folder)?

Please edit your question to clarify it and these points.
John_Ru (rep: 6142) Mar 21, '24 at 10:01 am
Hi John, I've edited the question post. Please review, and sorry for not noticing the rules earlier!
Fida_mutia (rep: 28) Mar 21, '24 at 10:31 am
Fidia  Thanks for answering my questions above and for modifiying both your file and question wording. Will try to find time to look at this soon.
John_Ru (rep: 6142) Mar 21, '24 at 10:33 am
Fidia

I looked at your question and file and - after doing some initial coding - realised that you're effectively asking us to do a mini-project for you for free. It would take me a couple of hours more to do that but why should I spend my free time if you haven't tried to fix it for yourself?

I seem to recall you saying on a previous question that yoiu couldn't mark it as Selected (as required by the Forum Rules) unitl your VBA expert had looked at it- why haven't you asked him or her to solve your probelm?

Your comments please...
John_Ru (rep: 6142) Mar 21, '24 at 5:50 pm
Hi John

I've marked 'Selected' to all the answers here. Which one you're refering to? Also sorry for saying I have a VBA expert in the company; the guy I mentioned is actually a Software Engineer and he's not much help with this stuff. I did ask him about this, but he doesn't understand with both the methode to be used and the expected result. May I know how many hours you need to work on this
Fida_mutia (rep: 28) Mar 22, '24 at 6:16 am
Fida. I can't recell which question your referred to an expert but if you selected it then thank you. I woke early this morning  thinking how to finish your code so will try to find time soon. I think the code need about 2 hours of my time (all told) but suspect it will save you many hours, right?
John_Ru (rep: 6142) Mar 22, '24 at 9:06 am
yepp exactly
Fida_mutia (rep: 28) Mar 22, '24 at 1:00 pm
Sounds like a great deal for you whereas I just lose more spare time! 
John_Ru (rep: 6142) Mar 22, '24 at 4:19 pm
Sounds like a great deal for you whereas I just lose more spare time! 
John_Ru (rep: 6142) Mar 22, '24 at 4:19 pm
Sorry but can you help me please?
Fida_mutia (rep: 28) Mar 23, '24 at 12:59 am
Fida

Please see my Answer. Hope it's enough for you since I don't want to spend more tiime on this really- I took me more like 5 hours to do this so my wife is not pleased with me!

To be honest, I doubt I'll be doing something like this again- your company should be paying someone to get work like this done.
John_Ru (rep: 6142) Mar 23, '24 at 1:51 pm
Add to Discussion

Answers

0

Fida

Attached are two files. The second file is like the portal data (only) in your Question test file but:

  1. column B had values like AB-ABC-XYZ90276-1 but the physical ones missed that Z e.g. XY90276 - that's still the case in yellow cell c3 but I corrected them  elsewhere
  2. yellow cell C5 added an initial missing ";" delimiter so AB-ABC-XYZ90070-1; AB-ABC-XYZ90070-2;.... 
  3. the data connection is removed.

Please save that file and close. Then open the first file "Parse EV charger data v0_a.xlsm" (with macros enabled), click the blue button labelled "Parse data from file" and follow the prompts to open the second file (or other files like that).

You should end up with a new file with the second sheet as a copy of the portal data used (as evidence) and the first sheet showing the parsed data in a table. Any yellow cells are where a match wasn't seen (between B and C values). In the case of the second file, that's from the C3 errors I left in.deliberately

The work is done by the code behind that button (shown below, with comments to explain what's happening broadly). You may need to change the values in the bold lines near the start of the code:

Option Explicit
Option Base 1

Sub ParseButton_Click()

    Dim Dlg As Office.FileDialog
    Dim ImpStr As String, ImpFile As Workbook, ImpSheet As Worksheet
    Dim Strt As Range, Prfx As String, ValDelim As String
    Dim Rw As Long, LstRw As Long, OutRw As Long, ArrRw As Long
    Dim IdArray As Variant, PhysArray As Variant, PhysRw As Long, PhysSuffSep As String, PossID As String
    Dim OutFl As Workbook

    On Error Resume Next
    ' define the delimiter between values
    ValDelim = ";"
    ' define the Prefix string to be removed from EV Spot Src IDs
    Prfx = "AB-ABC-"
    ' define the Suffix separator to be removed from EV Spot Src IDs
    PhysSuffSep = "-"
    ' get the data file to parse
    Set Dlg = Application.FileDialog(msoFileDialogFilePicker)

    With Dlg
        .Filters.Clear
        .Filters.Add "Excel Files", "*.xlsx", 1
        .Title = "Pick a single Excel file to parse data from"
        .AllowMultiSelect = False
        ' optional- set a path to look for files (and uncomment); should end in \
        '.InitialFileName = "C:\"
        If .Show = True Then
            'get the file name
            ImpStr = .SelectedItems(1)
            ' or tell user none selected...
            Else
            MsgBox "No file selected- please try again."
            Exit Sub
        End If
    End With
    ' open file
    Workbooks.Open Filename:=ImpStr
    ' open selected file
    Set ImpFile = ActiveWorkbook

    Set Strt = Application.InputBox(Prompt:="Click on a cell in the data table", _
            Title:="Please select sheet with EV data table and...", Type:=8)
    If Strt Is Nothing Then
        MsgBox "No cell selected, Parse cancelled"
        Exit Sub
    Else
        ' speed up
        Application.ScreenUpdating = False
        ' create new output file
        With Workbooks.Add
            .Sheets(1).Name = "Parsed data"
            With .Sheets.Add(After:=.Sheets(1))
                .Name = "Portal data used"
                'copy data to this sheet and fit columns
                Strt.CurrentRegion.Copy
                .Cells(1, 1).PasteSpecial xlPasteAll
                .Cells(1, 1).CurrentRegion.Columns.AutoFit
                'find last row
                LstRw = .Cells(Rows.Count, 1).End(xlUp).Row
                .Cells(2, 5).Select
                ' add header row to Parsed data sheet
                .Parent.Sheets(1).Range("A1:C1").Value = _
                    Array("Charging station name", _
                    "Charging spot SRC ID)", _
                    "Charging spot physical reference")
                OutRw = 2
                ' loop down sheet
                For Rw = 2 To LstRw
                    'split columns A & B values into arrays
                    IdArray = Split(.Cells(Rw, 2), ValDelim)
                    PhysArray = Split(.Cells(Rw, 3), ValDelim)
                    ' loop though column B parsed values
                    For ArrRw = LBound(IdArray, 1) To UBound(IdArray, 1)
                        ' write values in columns A & B
                        .Parent.Sheets(1).Cells(OutRw, 1).Value = Trim(.Cells(Rw, 1))
                        .Parent.Sheets(1).Cells(OutRw, 2).Value = Trim(IdArray(ArrRw))
                        ' set error message in C
                        .Parent.Sheets(1).Cells(OutRw, 3).Value = "Not matched in " & Trim(.Cells(Rw, 3))
                        .Parent.Sheets(1).Cells(OutRw, 3).Interior.Color = vbYellow
                        For PhysRw = LBound(PhysArray, 1) To UBound(PhysArray, 1)
                            ' refine a string from B to create possible ID (physical)
                            PossID = Trim(Replace(IdArray(ArrRw), Prfx, ""))
                            PossID = Trim(Left(PossID, InStrRev(PossID, PhysSuffSep) - 1))
                            ' check if there's a matching physical ID
                            If Trim(PhysArray(PhysRw)) = PossID Then
                                ' if so, replace error message with value and stop looking
                                .Parent.Sheets(1).Cells(OutRw, 3).Value = Trim(PhysArray(PhysRw))
                                .Parent.Sheets(1).Cells(OutRw, 3).Interior.Color = vbWhite
                                Exit For
                            End If
                        Next PhysRw
                        ' increment row number for output values
                        OutRw = OutRw + 1
                    Next ArrRw
                Next Rw


                With .Parent.Sheets(1)
                    'fit columns
                    .UsedRange.Columns.AutoFit
                    ' convert to table (and name) then show results
                    .ListObjects.Add(xlSrcRange, .UsedRange, , xlYes).Name = "EVtable"
                    ' add details of extract in new row 1
                    .Rows(1).Insert
                    .Cells(1, 1).Value = "Data parsed from file( - sheet): " _
                        & ImpStr & " - " & Strt.Worksheet.Name
                        ' close file
                        ImpFile.Close SaveChanges:=False
                    .Activate
                    .Cells(2, 5).Select
                End With

            End With
        End With
    End If

    Application.ScreenUpdating = True
    ' tell user
    MsgBox "Done! Please click OK then save file..." & vbCr & vbCr & "(Any error messages have a yellow fill; " _
        & vbCr & "source data captured on second sheet)", vbOKOnly, "Data parsed"
    ' prompt save
    Set Dlg = Application.FileDialog(msoFileDialogSaveAs)

    With Dlg
        ' suggest name (including date)
        .InitialFileName = "EV IDs Parsed data " & Format(Date, "dd-mmm-yyyy") & ".xlsx"
        .Title = "Save file (and change file name if needed)"
        If .Show = True Then .Execute
    End With

End Sub

Hope this is what you want (I've spend too much time creating it!). If so please mark this Answer as Selected.

Discuss

Discussion

Fida. Did you see my Answer? If so, please respond 
John_Ru (rep: 6142) Mar 27, '24 at 2:14 am
HI Fida

Please respond to my Answer.
John_Ru (rep: 6142) Apr 1, '24 at 9:50 am
Add to Discussion


Answer the Question

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