Solved

Editing CSV with Excel VBA

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

INDEX and MATCH can be used to great effect to replace HLOOKUP and VLOOKUP as it does not have the limitation of needing the data to be sorted so that the reference value is in the first column or row. It also has the ability to perform a bi-directi…
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 …
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 in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.

911 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

22 Experts available now in Live!

Get 1:1 Help Now