Solved

Editing CSV with Excel VBA

Posted on 2014-02-24
2
374 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

How to quickly and accurately populate Word documents with Excel data, charts and images (including Automated Bookmark generation) David Miller (dlmille) Synopsis In this article you’ll learn how to use ExcelToWord! to copy data,charts, shapes …
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

809 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