VBA: Extracting raw data and move into a new Sheet (tidy format)

Chanzuihou

New Member
Joined
Jul 28, 2016
Messages
19
Dear all,

I am very entry-level with VBA and am now desperately needing a set of VBA, for one of my repeating work task. Is possible that any of you VBA expert please help me with a set of VBA code please? Any help is sincerely appreciated.

In the below raw data, would like to move the following data across to Sheet "DATA_AF_VBA" (pls refer to below screen paste, the desired format). Data I would like to copy across is:
- Webshop
- Price
- Product

Really appreciate any advice. thanks heaps ~~~~

Raw data:

Excel 2012
ABCDEFGH
1Price2Spy Tool - Daily notification e-mail
2Price2Spy has detected price changes. Please see the list of the products below.
3Product : CFX40W
4WebshopPriceAvailableLast checkedLast changeFailed attemptsLast message error
512 Volt Technology1,039.00 AUD
6was: 1,079.00 AUD?Yes1/04/2020 2:141/04/2019 0:000
7seabreeze1,099.00 AUD
8was: 973.00 AUD?Yes1/04/2020 2:141/04/2006 19:400
9Tentworld1,099.99 AUD
10was: 973.00 AUD?Yes1/04/2020 2:141/04/2004 20:570
11Snowys1,149.00 AUD
12was: 1,039.00 AUD?Yes1/04/2020 2:141/03/2008 15:410
13Anacondastores1,149.00 AUD
14was: 1,039.20 AUD? 10.57 %Yes1/04/2020 2:141/04/2020 2:140
15My Generator1,249.00 AUD
16was: 1,079.00 AUD?Yes1/04/2020 2:141/04/2019 0:000
17BCF1,299.00 AUDYes1/04/2020 2:141/03/2007 15:140
18Product : CFX50W
19WebshopPriceAvailableLast checkedLast changeFailed attemptsLast message error
2012 Volt Technology1,159.00 AUD
21was: 1,198.00 AUD?Yes1/04/2020 2:141/04/2019 0:190
22Tentworld1,199.00 AUD
23was: 1,085.00 AUD?Yes1/04/2020 2:141/04/2004 20:180
24seabreeze1,199.00 AUD
25was: 1,085.00 AUD?Yes1/04/2020 2:141/04/2006 19:410
26Snowys1,249.00 AUD
27was: 1,159.00 AUD?Yes1/04/2020 2:141/03/2008 15:360
28Anacondastores1,299.00 AUD
29was: 1,159.20 AUD? 12.06 %Yes1/04/2020 2:141/04/2020 2:140
30BCF1,449.00 AUDYes1/04/2020 2:141/03/2007 15:230
31Rays Outdoors1,449.00 AUDYes1/04/2020 2:141/03/2007 15:230
32Product : CFX65W
33WebshopPriceAvailableLast checkedLast changeFailed attemptsLast message error
3412 Volt Technology1,279.00 AUD
35was: 1,399.00 AUD?Yes1/04/2020 1:271/04/2019 0:190
36Tentworld1,399.99 AUD
37was: 1,198.00 AUD?Yes1/04/2020 1:271/04/2004 20:390
38Snowys1,449.00 AUD
39was: 1,279.00 AUD?Yes1/04/2020 1:271/03/2008 15:591There's been a problem while loading a page.
40Anacondastores1,449.00 AUD
41was: 1,279.20 AUD? 13.27 %Yes1/04/2020 1:271/04/2020 1:270
42BCF1,599.00 AUDYes1/04/2020 1:271/03/2007 15:300
43My Generator1,599.00 AUD
44was: 1,299.00 AUD?Yes1/04/2020 1:271/04/2019 0:190
45Product : CFX95DZW
46WebshopPriceAvailableLast checkedLast changeFailed attemptsLast message error
4712 Volt Technology1,519.00 AUD
48was: 1,699.00 AUD?Yes1/04/2020 2:041/04/2019 0:000
49Tentworld1,699.00 AUD
50was: 1,423.00 AUD?Yes1/04/2020 2:041/04/2004 20:180
51Snowys1,749.00 AUD
52was: 1,599.00 AUD?Yes1/04/2020 2:041/03/2008 15:450
53Anacondastores1,749.00 AUD
54was: 1,519.20 AUD? 15.13 %Yes1/04/2020 2:041/04/2020 2:040
55BCF1,899.00 AUDYes1/04/2020 2:041/03/2007 15:350
56Rays Outdoors1,899.00 AUDYes1/04/2020 2:041/03/2007 15:370
57My Generator1,899.00 AUD
58was: 1,649.00 AUD?Yes1/04/2020 2:041/04/2019 0:000
59seabreeze1,569.00 AUD
60was: 1,749.00 AUD?Yes1/03/2025 20:001/03/2014 16:1310This Webshop is supported, but the URL you've entered is not a single product page.
0419



Desired format: using VBA to move raw data into the desired format:


Excel 2012
ABCD
1WebshopPriceProductDate
212 Volt Technology1,039.00CFX40W19/04/2017
3seabreeze1,099.00CFX40W19/04/2017
4Tentworld1,099.99CFX40W19/04/2017
5Snowys1,149.00CFX40W19/04/2017
6Anacondastores1,149.00CFX40W19/04/2017
7My Generator1,249.00CFX40W19/04/2017
8BCF1,299.00CFX40W19/04/2017
912 Volt Technology1,159.00CFX50W19/04/2017
10Tentworld1,199.00CFX50W19/04/2017
11seabreeze1,199.00CFX50W19/04/2017
12Snowys1,249.00CFX50W19/04/2017
13Anacondastores1,299.00CFX50W19/04/2017
14BCF1,449.00CFX50W19/04/2017
15Rays Outdoors1,449.00CFX50W19/04/2017
1612 Volt Technology1,279.00CFX65W19/04/2017
17Tentworld1,399.99CFX65W19/04/2017
18Snowys1,449.00CFX65W19/04/2017
19Anacondastores1,449.00CFX65W19/04/2017
20BCF1,599.00CFX65W19/04/2017
21My Generator1,599.00CFX65W19/04/2017
2212 Volt Technology1,519.00CFX95DZW19/04/2017
23Tentworld1,699.00CFX95DZW19/04/2017
24Snowys1,749.00CFX95DZW19/04/2017
25Anacondastores1,749.00CFX95DZW19/04/2017
26BCF1,899.00CFX95DZW19/04/2017
27Rays Outdoors1,899.00CFX95DZW19/04/2017
28My Generator1,899.00CFX95DZW19/04/2017
29seabreeze1,569.00CFX95DZW19/04/2017
Data_AF_VBA
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
Re: VBA help pls~ Extracting raw data and move into a new Sheet (tidy format)

So the date in column D of the desired format is the date the sheet was generated ie Today's date?
 
Upvote 0
Re: VBA help pls~ Extracting raw data and move into a new Sheet (tidy format)

This does as you ask but doesn't do any formatting. I'm guessing you'll be able to do that.

Place this code in the worksheet that has the raw data:

Code:
Sub GenerateSheet()


    Dim ws As Worksheet 'New worksheet to copy data to.
    Dim sCurrentProduct As String
    Dim lr As Long 'last row on column B as Column A has blanks
    Dim nr As Long 'next available row on new sheet
    Dim c As Range
    
    Set ws = Worksheets.Add(After:=Worksheets(Worksheets.Count))
    'set column headers
    With ws
        .Name = "DATA_AF_VBA"
        .Cells(1, 1) = "Webshop"
        .Cells(1, 2) = "Price"
        .Cells(1, 3) = "Product"
        .Cells(1, 4) = "Date"
    End With
    
    lr = Range("B" & Rows.Count).End(xlUp).Row
    nr = 2
    
    'loop down column A extracting all information.
    
    For Each c In Range(Cells(3, 1).Address, Cells(lr, 1).Address)
        If Left(c, 7) = "Product" Then
            sCurrentProduct = Right(c, Len(c) - InStr(1, c, ":") - 1)
        ElseIf Trim(c) <> "Webshop" And Len(c) > 0 Then 'ignore cells with webshop in them and blank cells
            ws.Range("A" & nr) = c 'Webshop
            ws.Range("B" & nr) = c.Offset(0, 1) 'Price
            ws.Range("C" & nr) = sCurrentProduct 'Product
            ws.Range("D" & nr) = Format(Now, "dd/mm/yyyy")
            nr = nr + 1
        End If
    Next c
    
    'Do formatting here
    
End Sub
 
Upvote 0
Re: VBA help pls~ Extracting raw data and move into a new Sheet (tidy format)

So the date in column D of the desired format is the date the sheet was generated ie Today's date?

Dear Gallen

Thanks, the date can be manually entered at later stage (no need to be part of VBA code), which yes it was the date the data was generated

Thanks heaps
 
Upvote 0
Re: VBA help pls~ Extracting raw data and move into a new Sheet (tidy format)

Thanking you SO much Gallen, truly appreciate your help~~~~
 
Upvote 0
Re: VBA help pls~ Extracting raw data and move into a new Sheet (tidy format)

No problem.
 
Upvote 0
Re: VBA help pls~ Extracting raw data and move into a new Sheet (tidy format)

Dear gallen

Thanks again for your help. today I pasted the VBA code into the work sheet and ran. It successfully generated a sheet "DATA_AF_VBA", and in that sheet, it showed up header "Webshop", "Price", "Product" and "Date". So thank you.

However it stopped there and didn't extract any data into the newly generated sheet.

Do you mind to have a look? I was also wondering, if it is possible to make some implementation of the code, to achieve:
1. No need to create a new Sheet "DATA_AF_VBA", because this sheet would have already been created within the workbook.
2. Instead, would like to copy the desirable data ("Webshop", "Price" and "Product") to the already existing Sheet "DATA_AB_VBA".
3. In the already existing DATA_AT_VBA sheet, it already has headers of "Webshop", "Price" and "Product" and underneath each header, it will already have previous dates' information.
4. The extracted data can be pasted to the next available Blank cell, under the right Header, starting from column A. For example, in my example of raw data sheet, 12 Volt Technology is the first "Webshop"; it should be copied to sheet "DATA_AF_VBA" cell A2. Its "price" is 1,039.00 AUD, which ideally is pasted to B2; and the "product" is CFX40W, which should be copied to C2
5. Then the VBA code ideally loops to next Webshop etc etc.

Really appreciate any feedback or assistance.

Thanks again
 
Upvote 0
Re: VBA help pls~ Extracting raw data and move into a new Sheet (tidy format)

I tested that on the exact raw data you provided and the results were exactly as the results you provided.:confused:

The only way that code won't work is if it isn't pasted into the sheet with the raw data.

This loop goes down every cell in column A of raw data. If it finds none-blank cell with a value that doesn't contain "Webshop" or "Product" it uses that value as the webshop:

Code:
    For Each c In Range(Cells(3, 1).Address, Cells(lr, 1).Address)
        If Left(c, 7) = "Product" Then
            sCurrentProduct = Right(c, Len(c) - InStr(1, c, ":") - 1)
[COLOR=#ff0000][B]        ElseIf Trim(c) <> "Webshop" And Len(c) > 0 Then 'ignore cells with webshop in them and blank cells[/B][/COLOR]
[B][COLOR=#ff0000]            ws.Range("A" & nr) = c 'Webshop[/COLOR][/B]
            ws.Range("B" & nr) = c.Offset(0, 1) 'Price
            ws.Range("C" & nr) = sCurrentProduct 'Product
            ws.Range("D" & nr) = Format(Now, "dd/mm/yyyy")
            nr = nr + 1
        End If
    Next c

Then the cell value to the right of the webshop name on the raw data sheet is copied to the cell to the right of the webshop name we pasted in the previous step:

Code:
    For Each c In Range(Cells(3, 1).Address, Cells(lr, 1).Address)
        If Left(c, 7) = "Product" Then
            sCurrentProduct = Right(c, Len(c) - InStr(1, c, ":") - 1)
        ElseIf Trim(c) <> "Webshop" And Len(c) > 0 Then 'ignore cells with webshop in them and blank cells
            ws.Range("A" & nr) = c 'Webshop
[COLOR=#ff0000][B]            ws.Range("B" & nr) = c.Offset(0, 1) 'Price[/B][/COLOR]
            ws.Range("C" & nr) = sCurrentProduct 'Product
            ws.Range("D" & nr) = Format(Now, "dd/mm/yyyy")
            nr = nr + 1
        End If

    Next c


Finally, the product is added using the variable we extracted when the loop comes to cell with the word "Product" in it.

Code:
    For Each c In Range(Cells(3, 1).Address, Cells(lr, 1).Address)
[COLOR=#ff0000][B]        If Left(c, 7) = "Product" Then[/B][/COLOR]
[COLOR=#ff0000][B]            sCurrentProduct = Right(c, Len(c) - InStr(1, c, ":") - 1)[/B][/COLOR]
        ElseIf Trim(c) <> "Webshop" And Len(c) > 0 Then 'ignore cells with webshop in them and blank cells
            ws.Range("A" & nr) = c 'Webshop
            ws.Range("B" & nr) = c.Offset(0, 1) 'Price
[B][COLOR=#ff0000]            ws.Range("C" & nr) = sCurrentProduct 'Product[/COLOR][/B]
            ws.Range("D" & nr) = Format(Now, "dd/mm/yyyy")
            nr = nr + 1
        End If

    Next c


So if the code isn't working the data is in the wrong place. Have you stepped through to see if the loop actually executes?

I won't address the additional requests until this issue is fixed. Once we know that works it's simple to change to what you want.
 
Last edited:
Upvote 0
Re: VBA help pls~ Extracting raw data and move into a new Sheet (tidy format)

I tested that on the exact raw data you provided and the results were exactly as the results you provided.:confused:

The only way that code won't work is if it isn't pasted into the sheet with the raw data.

This loop goes down every cell in column A of raw data. If it finds none-blank cell with a value that doesn't contain "Webshop" or "Product" it uses that value as the webshop:

Code:
    For Each c In Range(Cells(3, 1).Address, Cells(lr, 1).Address)
        If Left(c, 7) = "Product" Then
            sCurrentProduct = Right(c, Len(c) - InStr(1, c, ":") - 1)
[COLOR=#ff0000][B]        ElseIf Trim(c) <> "Webshop" And Len(c) > 0 Then 'ignore cells with webshop in them and blank cells[/B][/COLOR]
[B][COLOR=#ff0000]            ws.Range("A" & nr) = c 'Webshop[/COLOR][/B]
            ws.Range("B" & nr) = c.Offset(0, 1) 'Price
            ws.Range("C" & nr) = sCurrentProduct 'Product
            ws.Range("D" & nr) = Format(Now, "dd/mm/yyyy")
            nr = nr + 1
        End If
    Next c

Then the cell value to the right of the webshop name on the raw data sheet is copied to the cell to the right of the webshop name we pasted in the previous step:

Code:
    For Each c In Range(Cells(3, 1).Address, Cells(lr, 1).Address)
        If Left(c, 7) = "Product" Then
            sCurrentProduct = Right(c, Len(c) - InStr(1, c, ":") - 1)
        ElseIf Trim(c) <> "Webshop" And Len(c) > 0 Then 'ignore cells with webshop in them and blank cells
            ws.Range("A" & nr) = c 'Webshop
[COLOR=#ff0000][B]            ws.Range("B" & nr) = c.Offset(0, 1) 'Price[/B][/COLOR]
            ws.Range("C" & nr) = sCurrentProduct 'Product
            ws.Range("D" & nr) = Format(Now, "dd/mm/yyyy")
            nr = nr + 1
        End If

    Next c


Finally, the product is added using the variable we extracted when the loop comes to cell with the word "Product" in it.

Code:
    For Each c In Range(Cells(3, 1).Address, Cells(lr, 1).Address)
[COLOR=#ff0000][B]        If Left(c, 7) = "Product" Then[/B][/COLOR]
[COLOR=#ff0000][B]            sCurrentProduct = Right(c, Len(c) - InStr(1, c, ":") - 1)[/B][/COLOR]
        ElseIf Trim(c) <> "Webshop" And Len(c) > 0 Then 'ignore cells with webshop in them and blank cells
            ws.Range("A" & nr) = c 'Webshop
            ws.Range("B" & nr) = c.Offset(0, 1) 'Price
[B][COLOR=#ff0000]            ws.Range("C" & nr) = sCurrentProduct 'Product[/COLOR][/B]
            ws.Range("D" & nr) = Format(Now, "dd/mm/yyyy")
            nr = nr + 1
        End If

    Next c


So if the code isn't working the data is in the wrong place. Have you stepped through to see if the loop actually executes?

I won't address the additional requests until this issue is fixed. Once we know that works it's simple to change to what you want.

Thank you so much Gallen, I will run this through when logging in shortly. Much Appreciate it
 
Upvote 0
Re: VBA help pls~ Extracting raw data and move into a new Sheet (tidy format)

Dear gallen

Yes you are hundred percent correct, after I put the code into the raw data sheet, it works perfectly!!!!

Brilliant work and thanks again.

I tested that on the exact raw data you provided and the results were exactly as the results you provided.:confused:

The only way that code won't work is if it isn't pasted into the sheet with the raw data.

This loop goes down every cell in column A of raw data. If it finds none-blank cell with a value that doesn't contain "Webshop" or "Product" it uses that value as the webshop:

Code:
    For Each c In Range(Cells(3, 1).Address, Cells(lr, 1).Address)
        If Left(c, 7) = "Product" Then
            sCurrentProduct = Right(c, Len(c) - InStr(1, c, ":") - 1)
[COLOR=#ff0000][B]        ElseIf Trim(c) <> "Webshop" And Len(c) > 0 Then 'ignore cells with webshop in them and blank cells[/B][/COLOR]
[B][COLOR=#ff0000]            ws.Range("A" & nr) = c 'Webshop[/COLOR][/B]
            ws.Range("B" & nr) = c.Offset(0, 1) 'Price
            ws.Range("C" & nr) = sCurrentProduct 'Product
            ws.Range("D" & nr) = Format(Now, "dd/mm/yyyy")
            nr = nr + 1
        End If
    Next c

Then the cell value to the right of the webshop name on the raw data sheet is copied to the cell to the right of the webshop name we pasted in the previous step:

Code:
    For Each c In Range(Cells(3, 1).Address, Cells(lr, 1).Address)
        If Left(c, 7) = "Product" Then
            sCurrentProduct = Right(c, Len(c) - InStr(1, c, ":") - 1)
        ElseIf Trim(c) <> "Webshop" And Len(c) > 0 Then 'ignore cells with webshop in them and blank cells
            ws.Range("A" & nr) = c 'Webshop
[COLOR=#ff0000][B]            ws.Range("B" & nr) = c.Offset(0, 1) 'Price[/B][/COLOR]
            ws.Range("C" & nr) = sCurrentProduct 'Product
            ws.Range("D" & nr) = Format(Now, "dd/mm/yyyy")
            nr = nr + 1
        End If

    Next c


Finally, the product is added using the variable we extracted when the loop comes to cell with the word "Product" in it.

Code:
    For Each c In Range(Cells(3, 1).Address, Cells(lr, 1).Address)
[COLOR=#ff0000][B]        If Left(c, 7) = "Product" Then[/B][/COLOR]
[COLOR=#ff0000][B]            sCurrentProduct = Right(c, Len(c) - InStr(1, c, ":") - 1)[/B][/COLOR]
        ElseIf Trim(c) <> "Webshop" And Len(c) > 0 Then 'ignore cells with webshop in them and blank cells
            ws.Range("A" & nr) = c 'Webshop
            ws.Range("B" & nr) = c.Offset(0, 1) 'Price
[B][COLOR=#ff0000]            ws.Range("C" & nr) = sCurrentProduct 'Product[/COLOR][/B]
            ws.Range("D" & nr) = Format(Now, "dd/mm/yyyy")
            nr = nr + 1
        End If

    Next c


So if the code isn't working the data is in the wrong place. Have you stepped through to see if the loop actually executes?

I won't address the additional requests until this issue is fixed. Once we know that works it's simple to change to what you want.
 
Upvote 0

Forum statistics

Threads
1,214,646
Messages
6,120,716
Members
448,985
Latest member
chocbudda

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top