[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 610
  • Last Modified:

Editing CSV with Excel VBA

Hi,

I'm preparing a CSV file for a web upload. I have thousands of products and I'm attempting to do this using VBA before I hit it manually. The CSV is a file full of products, SKUs and their attributes such as color and size. Below is a sample:

A                      B                                                      C                     D              E
SKU01               T-Shirt 1 Black S                              T-Shirt 1               Black       S
SKU02               T-Shirt 1 Black M                              T-Shirt 1               Black       M
SKU03               T-Shirt 1 Black L                              T-Shirt 1               Black       L
SKU04               Running Gloves 1 Red M                      Running Gl       Red        M
SKU05               Running Gloves 1 Red L                       Running Gl       Red               L
SKU06               Running Gloves 1 Grey M               Running Gl       Grey       M
SKU07               Running Gloves 1 Grey L               Running Gl       Grey       L
SKU08               Wooly Hat                                     Wooly Hat             
SKU09               Running Socks 1 White S               Running So       White       S
SKU10               Running Socks 1 White M               Running So       White       M

Column A is the SKU, B the Description, C is the first 10 characters of the description, D and E are the product attributes which have been extracted from the description. I basically need the VBA script to generate the following:

A                      B                                           C                     D           E
SKU01CON       T-Shirt 1                   
SKU01               T-Shirt 1 Black S                    T-Shirt 1           Black         S
SKU02               T-Shirt 1 Black M                    T-Shirt 1           Black         M
SKU03               T-Shirt 1 Black L                    T-Shirt 1           Black         L
SKU04CON       Running Gloves 1                   
SKU04               Running Gloves 1 Red M            Running Gl           Red         M
SKU05               Running Gloves 1 Red L            Running Gl           Red         L
SKU06               Running Gloves 1 Grey M     Running Gl           Black         M
SKU07               Running Gloves 1 Grey L      Running Gl           Black         L

You can see that there have been two additional entries made, this is what I need. The additional product entries group the similar products together.

This is the way I was looking at achieving it:

Move through each row, IF columns D or E <> "" AND column C is <> to the cell above then insert a new row.
Copy the contents of A to the newly generated row whilst concatenating it with "CON"
Copy the contents of B to the newly generated row whilst removing the attributes in D and/or E (with spaces before)

Firstly, would you agree? It's not a solid plan. Secondly, how do I do this in VBA?

I have 7000 product entries to work through, I'm hoping to avoid any manual work. Thanks for your help!

Nick
0
nickedwards
Asked:
nickedwards
1 Solution
 
Patrick MatthewsCommented:
The following macro appears to do the trick:

Q-28372723.xlsm

Open it, then run the macro for FixCSV.

The code itself is:

Sub FixCSV()
    
    Dim SourcePath As Variant
    Dim DestFileNameStub As String
    Dim DestPath As Variant
    Dim SourceWb As Workbook
    Dim DestWb As Workbook
    Dim DestWs As Worksheet
    Dim LastR As Long
    Dim SourceArr As Variant
    Dim Counter As Long
    Dim DestR As Long
    Dim ShortDescr As String
    Dim xAttribute As String
    Dim Product As String
    
    SourcePath = Application.GetOpenFilename("CSV Files (*.csv), *.csv", , "Select original CSV file...", , False)
    If SourcePath = False Then
        MsgBox "No file chosen", vbCritical, "Aborting Macro"
        Exit Sub
    End If
    
    DestFileNameStub = Mid(SourcePath, InStrRev(SourcePath, "\") + 1)
    DestFileNameStub = Left(DestFileNameStub, Len(DestFileNameStub) - 4)
    
    DestPath = Application.GetSaveAsFilename(DestFileNameStub & "-new.csv", "CSV Files (*.csv), *.csv", , "Save new file to...")
    If DestPath = False Then
        MsgBox "No file chosen", vbCritical, "Aborting Macro"
        Exit Sub
    End If
    
    Set SourceWb = Workbooks.Open(SourcePath)
    Set DestWb = Workbooks.Add
    Set DestWs = DestWb.Worksheets(1)
    
    With SourceWb.Worksheets(1)
        LastR = .Cells(.Rows.Count, 1).End(xlUp).Row
        SourceArr = .Range("a1").Resize(LastR, 5)
    End With
    
    With DestWs
        For Counter = 1 To LastR
            If SourceArr(Counter, 3) <> ShortDescr Then
                If SourceArr(Counter, 4) <> "" Or SourceArr(Counter, 5) <> "" Then
                    ShortDescr = SourceArr(Counter, 3)
                    DestR = DestR + 1
                    .Cells(DestR, 1) = SourceArr(Counter, 1) & "CON"
                    Product = SourceArr(Counter, 2)
                    xAttribute = SourceArr(Counter, 5)
                    If xAttribute <> "" Then
                        Product = Left(Product, Len(Product) - Len(xAttribute) - 1)
                    End If
                    xAttribute = SourceArr(Counter, 4)
                    If xAttribute <> "" Then
                        Product = Left(Product, Len(Product) - Len(xAttribute) - 1)
                    End If
                    .Cells(DestR, 2) = Product
                End If
            End If
            DestR = DestR + 1
            .Cells(DestR, 1) = SourceArr(Counter, 1)
            .Cells(DestR, 2) = SourceArr(Counter, 2)
            .Cells(DestR, 3) = SourceArr(Counter, 3)
            .Cells(DestR, 4) = SourceArr(Counter, 4)
            .Cells(DestR, 5) = SourceArr(Counter, 5)
        Next
    End With
    
    Application.DisplayAlerts = False
    DestWb.SaveAs DestPath, xlCSV
    DestWb.Close
    SourceWb.Close
    Application.DisplayAlerts = True
    
    MsgBox "Done"
    
End Sub

Open in new window

0
 
nickedwardsAuthor Commented:
Worked a treat! Thanks.
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now