VBA – copy rows of data from specified worksheets

Richard JIBS

New Member
Joined
Feb 3, 2015
Messages
13
Hello,

I have a workbook that contains logistics plans for several events. Each event has its own worksheet.

I would like all data from selected event logistics plan copied into an ALL DATA worksheet.

The workbook is structured as follows with these worksheets:

ACTIVE EVENTS (contains a list of event codes, which are also the names of the worksheets for each of the events. The number of events will change throughout the year.)

Event codes
EV18-AAA
EV18-BBB
EV19-DDD

<tbody>
</tbody>

EV18-AAA (for example, contains the logistics plan for the EV18-BBB event)

Who Days beforeTask Status
Damien200Invite speakersComplete
Chris50Meet sponsorsIn progress
Damien12Book taxisTo do

<tbody>
</tbody>

ALL DATA (this is what this should look like)

Event codeWho Days beforeTask Status
EV18-AAADamien200Invite speakersComplete
EV18-AAAChris50Meet sponsorsIn progress
EV18-AAADamien12Book taxisTo do
EV18-BBBDamien150Invite speakersComplete
EV19-DDDChris100Visit venuesIn progress
EV19-DDDDamien80Send emarketingTo do
EV19-DDDChris40Ask Jo to chairIn progress
EV19-DDDDamien12Book taxisTo do

<tbody>
</tbody>

Could I have some help please with the VBA code to achieve this?

Note that there might be other event worksheets in the file, but ALL DATA should only pull through data from the worksheet names listed in ACTIVE EVENTS. There will be different numbers of rows in the logistics plans depending on the event. The data will start in the same row and column in each logistics worksheet (Cell A6). ALL DATA should also have a column that indicates which worksheet the logistics data has been pulled from (Column A in example above).

Thanks for your help.

Richard
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.

Book1
A
1Event codes
2EV18-AAA
3EV18-BBB
4EV18-DDD
ACTIVE EVENTS



Book1
ABCD
6WhoDays beforeTaskStatus
7Damien200Invite speakersComplete
8Chris50Meet sponsorsIn progress
9Damien12Book taxisTo do
EV18-AAA



Book1
ABCD
6WhoDays beforeTaskStatus
7Damien150Invite speakersComplete
EV18-BBB



Book1
ABCD
6WhoDays beforeTaskStatus
7Chris100Visit venuesIn progress
8Damien80Send emarketingTo do
9Chris40Ask Jo to chairIn progress
10Damien12Book taxisTo do
EV18-DDD



Book1
ABCDE
1Event CodeWhoDays beforeTaskStatus
2EV18-AAADamien200Invite speakersComplete
3EV18-AAAChris50Meet sponsorsIn progress
4EV18-AAADamien12Book taxisTo do
5EV18-BBBDamien150Invite speakersComplete
6EV18-DDDChris100Visit venuesIn progress
7EV18-DDDDamien80Send emarketingTo do
8EV18-DDDChris40Ask Jo to chairIn progress
9EV18-DDDDamien12Book taxisTo do
ALL DATA


Code:
Private Const ActiveEventsTab = "ACTIVE EVENTS" ' Name of the tab holding active events
Private Const AllDataTab = "ALL DATA" ' Name of the tab holding all data
Private Const ActiveEventsData = "A1" ' Cell on the active events tab containing "Event Codes"
Private Const AllDataData = "A1" ' Cell on the all data tab containing "Event Code"
Private Const EventData = "A6" ' Cell on the event tab holding "Who"
Public Sub AggregateData()

Dim activeEventsSheet As Worksheet
Dim allDataSheet As Worksheet
Dim eventSheet As Worksheet
Dim lastEvent As Long
Dim thisEvent As Long
Dim lastAction As Long
Dim thisAction As Long
Dim nextRow As Long
Dim thisCol As Long

Set activeEventsSheet = Sheets(ActiveEventsTab)
Set allDataSheet = Sheets(AllDataTab)

' Clear existing data?
With allDataSheet
    lastEvent = .Cells(.Rows.Count, .Range(AllDataData).Column).End(xlUp).Row
    If lastEvent > .Range(AllDataData).Row Then
        .Range(.Cells(.Range(AllDataData).Row + 1, .Range(AllDataData).Column), .Cells(lastEvent, .Range(AllDataData).Column + 4)).ClearContents
    End If
End With

' Find last event
With activeEventsSheet
    lastEvent = .Cells(.Rows.Count, .Range(ActiveEventsData).Column).End(xlUp).Row
    If lastEvent <= .Range(ActiveEventsData).Row Then Exit Sub
End With

' Process all events
nextRow = allDataSheet.Range(AllDataData).Row + 1
For thisEvent = activeEventsSheet.Range(ActiveEventsData).Row + 1 To lastEvent
    Set eventSheet = Sheets(activeEventsSheet.Cells(thisEvent, activeEventsSheet.Range(ActiveEventsData).Column).Value)
    lastAction = eventSheet.Cells(eventSheet.Rows.Count, eventSheet.Range(EventData).Column).End(xlUp).Row
    If lastAction > eventSheet.Range(EventData).Row Then
        For thisAction = eventSheet.Range(EventData).Row + 1 To lastAction
            allDataSheet.Cells(nextRow, allDataSheet.Range(AllDataData).Column).Value = eventSheet.Name
            For thisCol = 1 To 4
                allDataSheet.Cells(nextRow, allDataSheet.Range(AllDataData).Column + thisCol).Value = eventSheet.Cells(thisAction, eventSheet.Range(EventData).Column + thisCol - 1).Value
            Next thisCol
            nextRow = nextRow + 1
        Next thisAction
    End If
Next thisEvent

End Sub

WBD
 
Upvote 0

Forum statistics

Threads
1,215,051
Messages
6,122,872
Members
449,097
Latest member
dbomb1414

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