Solved

Editing CSV with Excel VBA

Posted on 2014-02-24
2
348 Views
Last Modified: 2014-02-25
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
Comment
Question by:nickedwards
2 Comments
 
LVL 92

Accepted Solution

by:
Patrick Matthews earned 500 total points
ID: 39883117
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
 
LVL 1

Author Closing Comment

by:nickedwards
ID: 39885519
Worked a treat! Thanks.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

What is a Form List Box? (skip if you know this) The forms List Box is the alternative to the ActiveX list box. If you are using excel 2007, you first make sure you have a developer tab (click the Orb)->"Excel Options"->Popular->"Show Developer tab…
Drop Down List with Unique/Distinct Values (Part II - ComboBox or ListBox and Data Validation List Bonus!) David Miller (dlmille) Intro This article focuses on delivering unique, sorted lists to list objects (e.g., ComboBox, ListBox) and Dat…
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now