VBA extracting data

Grange2

New Member
Joined
Mar 31, 2021
Messages
14
Office Version
  1. 2013
  2. 2011
Platform
  1. Windows
Hi Everyone,

I'm looking to to use a VBA Macro to extract data in a particular fashion. The data sheet looks like below:

Book1
ABCDEFG
1CodeDescriptionQTYPriceGSTTotal
2*Section1
316875Paper 3242341234
416876Paper 3242341234
516877Paper 3242341234
616878Paper 3242341234
716879Paper 3242341234
816880Paper 3242341234
9*Section 2
1016876Paper423845481
1157532Rock12307168
1256621Rock12358259
13*Section3
145412Scissors51215645
152132Scissors212122521
1654Scissors54858331
17
Sheet1


Each section has a different number of items listed ( possibly more than 3 sections per data sheet as well)and if the data could be extracted to resemble below:

Book1
ABCDEFGHIJ
1SectionCodeQTYPriceGSTTotal
2Section 1168751234
3Section 1168761234
4Section 1168771234
5Section 1168781234
6Section 1168791234
7Section 1168801234
Sheet2



Any help would be greatly appreciated :)

Thank you.
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
Try this
VBA Code:
Sub Sectionize()

Dim nSection As Long, nRow As Long
Dim Section As Boolean
Dim cell As Range, rngData As Range
Dim ws1 As Worksheet, w2 As Worksheet

Application.ScreenUpdating = False

Set ws1 = ActiveWorkbook.Sheets("Sheet1")
Set ws2 = ActiveWorkbook.Sheets("Sheet2")

Set rngData = ws1.Range("A1", ws1.Cells(Rows.Count, "A").End(xlUp))

nSection = 0
nRow = 1
For Each cell In rngData
    If cell = "*" Then
        nSection = nSection + 1
        Section = True
        Cont = False
        GoTo nxLine
    End If
    If Section Then
        nRow = nRow + 1
        With ws2
            .Range("A" & nRow) = "Section " & nSection
            .Range("B" & nRow) = ws1.Range("A" & cell.Row)
            ws1.Range("D" & cell.Row, "G" & cell.Row).Copy .Range("G" & nRow)
        End With
    End If
nxLine:
Next
ws2.Activate

End Sub
 
Upvote 0
Wow that works splendidly! To add on to the previous issue if the sections were different names is it possible to capture that as well? E.G

Book1
ABCDEFG
1CodeDescriptionQTYPriceGSTTotal
2*FoodDelight
316875Paper 3242341234
416876Paper 3242341234
516877Paper 3242341234
616878Paper 3242341234
716879Paper 3242341234
816880Paper 3242341234
9*OfficeSuppliers
1016876Paper423845481
1157532Rock12307168
1256621Rock12358259
13*PostOfficeInternational
145412Scissors51215645
152132Scissors212122521
1654Scissors54858331
Sheet1


I really appreciate your time and effort on this!

Thank you so much!
 
Upvote 0
You meant Food, Suppliers and Office is section name?. Then I need o modify code since I have no idea in your previous example since the word section is in two different columns.

Now I think the section name would be
Food Delight
Office Suppliers
Post Office International

If this is true then it is different ball game. You should have provided real example and explain better from the very beginning. This would save time
 
Upvote 0
Okay. Try this
VBA Code:
Sub Sectionize()

Dim nRow As Long
Dim SectionName As String
Dim Section As Boolean
Dim cell As Range, rngData As Range
Dim ws1 As Worksheet, w2 As Worksheet

Application.ScreenUpdating = False

Set ws1 = ActiveWorkbook.Sheets("Sheet1")
Set ws2 = ActiveWorkbook.Sheets("Sheet2")

Set rngData = ws1.Range("A1", ws1.Cells(Rows.Count, "A").End(xlUp))

nRow = 1
For Each cell In rngData
    If cell = "*" Then
        With ws1
            SectionName = Trim(.Range("B" & cell.Row) & .Range("C" & cell.Row) & _
                                    .Range("D" & cell.Row) & .Range("E" & cell.Row))
            Section = True
            GoTo nxLine
        End With
    End If
    If Section Then
        nRow = nRow + 1
        With ws2
            .Range("A" & nRow) = SectionName
            .Range("B" & nRow) = ws1.Range("A" & cell.Row)
            ws1.Range("D" & cell.Row, "G" & cell.Row).Copy .Range("G" & nRow)
        End With
    End If
nxLine:
Next
ws2.Activate

End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,224,818
Messages
6,181,152
Members
453,021
Latest member
Justyna P

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