Copy Page Name Multiple Times

grabrail

Board Regular
Joined
Sep 6, 2010
Messages
128
Office Version
  1. 365
Platform
  1. Windows
I have a workbook that has repeating multiple worksheets, the sheets are used to collect data about an inspection. Each worksheet is identical. I am trying to collate all information gathered from all completed worksheets into a single worksheet, which is acting like a DB. Ive managed this for the most part but a bit stuck on one last bit.

I have a table in the worksheets that is structured as such

Check NoDefect FoundServiceableOCRS Score
1ghjghjIP200
2hjghjDP200

I am collating to a table as such

DPU NumberCheck No.Defect FoundServiceableOCRS Score
1​
ghjghjghjghjIP
200​
hjghjhjghjDP
200​
3​
t5trtgrtgt5trtgrtgSIP
100​
gtgtrggtgtrgDP
200​

All this is dummy data

The code I am using to achieve this is:

VBA Code:
'DPU Number
    Sheets("GCDefects").Range("B" & lastrowgc + 1 & ":B" & (lastrowgc + 1)).Value = i
    
    'CheckNo
    Worksheets("DPU Report " & i).Range("B38:B" & Rng).Copy
    Sheets("GCDefects").Range("C" & lastrowgc + 1 & ":C" & (lastrowgc + 1)).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False

    'SERVICABLE
    Worksheets("DPU Report " & i).Range("G38:G" & Rng).Copy
    Sheets("GCDefects").Range("E" & lastrowgc + 1 & ":E" & (lastrowgc + 1)).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False

    'DEFECT Text
    Worksheets("DPU Report " & i).Range("B38:B" & Rng).Copy
    Sheets("GCDefects").Range("D" & lastrowgc + 1 & ":D" & (lastrowgc + 1)).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False

    'OCRS
    Worksheets("DPU Report " & i).Range("H38:H" & Rng).Copy
    Sheets("GCDefects").Range("F" & lastrowgc + 1 & ":F" & (lastrowgc + 1)).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False

Each worksheet is called "DPU Report 1" "DPU Report 2", etc. The number comes from a variable called i.

so effectivey each worksheet is called "DPU Report" & i


when I import the code using the code above, each column (apart from DPU Number) is selected on the source worksheet, selecting all rows of data and then copied in, however, I also want the DPU Number to be next to each row so it can be identified which worksheet it came from.

My code, does this for the first entry in the list but then not below, obviously becuase its only copying and pasting each worksheets data once.

My question is how do I get it to repeat the DPU number for each worksheets data so it ends up like this.

DPU NumberCheck No.Defect FoundServiceableOCRS Score
1​
ghjghjghjghjIP
200​
1​
hjghjhjghjDP
200​
3​
t5trtgrtgt5trtgrtgSIP
100​
3​
gtgtrggtgtrgDP
200​
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
How about cheating?
VBA Code:
    'CheckNo
    Worksheets("DPU Report " & i).Range("B38:B" & Rng).Copy
    Sheets("GCDefects").Range("C" & lastrowgc + 1 & ":C" & (lastrowgc + 1)).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
    'DPU Number
    With Sheets("GCDefects")
    .Range("B" & .Cells(Rows.Count, 2).End(xlUp).Offset(1).Row + 1 & ":B" & .Cells(Rows.Count, 3).End(xlUp).Offset(1).Row).Value = i
    End With
 
Upvote 0
Solution
How about cheating?
VBA Code:
    'CheckNo
    Worksheets("DPU Report " & i).Range("B38:B" & Rng).Copy
    Sheets("GCDefects").Range("C" & lastrowgc + 1 & ":C" & (lastrowgc + 1)).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
    'DPU Number
    With Sheets("GCDefects")
    .Range("B" & .Cells(Rows.Count, 2).End(xlUp).Offset(1).Row + 1 & ":B" & .Cells(Rows.Count, 3).End(xlUp).Offset(1).Row).Value = i
    End With
Perfect, thank you. Had to alter the code very slightly, and remove the offset(1) as it was putting the data one row too low, but other than this, perfect.
 
Upvote 0
Oh.. I put it there to remove +1 but forgot to remove +1 ;) here:

VBA Code:
    'CheckNo
    Worksheets("DPU Report " & i).Range("B38:B" & Rng).Copy
    Sheets("GCDefects").Range("C" & lastrowgc + 1 & ":C" & (lastrowgc + 1)).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
    'DPU Number
    With Sheets("GCDefects")
    .Range("B" & .Cells(Rows.Count, 2).End(xlUp).Offset(1).Row & ":B" & .Cells(Rows.Count, 3).End(xlUp).Offset(1).Row).Value = i
    End With
 
Upvote 0

Forum statistics

Threads
1,215,054
Messages
6,122,895
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