VBA copy range from multiple sheets into one, adding the sheet name

AriannaVV

New Member
Joined
Aug 6, 2017
Messages
34
Office Version
  1. 365
Platform
  1. Windows
Hi everyone!
I found in this forum (VBA - Copy and Paste Range From Multiple Sheets into Summary) the following code (changed somehow in order to transfer various ranges of cells from specific sheets in a new sheet called "summary"). Works great but when I run again the code to add data below previous copied range, selecting different sheets this time, it ovewrittes the first row and then continues nicely all other rows after the previous last row. Can anyone help to find out what goes wrong?
And another one question. Is it possible to mention the namesheet from where each result comes from? Thank you very much for your kind help.
VBA Code:
Sub Copy_Range_From_Sheets_De()

On Error GoTo M
 
Application.ScreenUpdating = False
 Dim i As Long
Dim ans As String
Dim Lastrow As Long
Dim Lastrowa As Long
Lastrow = Sheets("de.").Cells(Rows.Count, "A").End(xlUp).Row
Lastrowa = 3
For i = 2 To Lastrow
     ans = Sheets("de.").Cells(i, 1).Value
 With Sheets(ans)
 .Range("e93:o93").Copy
 Sheets("Summary").Cells(Lastrowa, 1).PasteSpecial xlPasteValues
  Lastrowa = Sheets("Summary").Cells(Rows.Count, "A").End(xlUp).Row + 1
 End With
 With Sheets(ans)

 .Range("e141:o141").Copy
  Sheets("Summary").Cells(Lastrowa, 1).PasteSpecial xlPasteValues
   Lastrowa = Sheets("Summary").Cells(Rows.Count, "A").End(xlUp).Row + 1
  End With
  Next
  Application.ScreenUpdating = True
 Exit Sub
M:
 MsgBox "You tried to use a sheet name that does not exist" & vbNewLine & "Or we had another problem"
 Application.ScreenUpdating = True
End Sub
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
After the line:
VBA Code:
lastrowA = 3
add this code:
VBA Code:
lastrowA = 3
  answer = MsgBox("Do you want to start at row 3?", vbYesNo)
  If answer = vbNo Then
   lastrowA = Sheets("Summary").Cells(Rows.Count, "A").End(xlUp).Row + 1
  End If
this will ask whether you want to start at row 3 or carry on from the last position
 
Upvote 1
After the line:
VBA Code:
lastrowA = 3
add this code:
VBA Code:
lastrowA = 3
  answer = MsgBox("Do you want to start at row 3?", vbYesNo)
  If answer = vbNo Then
   lastrowA = Sheets("Summary").Cells(Rows.Count, "A").End(xlUp).Row + 1
  End If
this will ask whether you want to start at row 3 or carry on from the last position
That solves the first part of my question. Thank you!!
 
Upvote 0
where do you want the sheet name put?? It is very easy just add this one line of code after the With Sheets(ans)
VBA Code:
With Sheets(ans)
 .Range("A1") = .Name  ' add this line which puts it in cell A1
End With
to put it in the last row try this:
VBA Code:
With Sheets(ans)
 .Range("A" & lastrowa) = .Name
End With
 
Upvote 0
where do you want the sheet name put?? It is very easy just add this one line of code after the With Sheets(ans)
VBA Code:
With Sheets(ans)
 .Range("A1") = .Name  ' add this line which puts it in cell A1
End With
to put it in the last row try this:
VBA Code:
With Sheets(ans)
 .Range("A" & lastrowa) = .Name
End With
I did as you said but there was no change. Normally the code begins to writte on cell A3 so I wrote .Range("L3") = .Name. But nothing happened.
 
Upvote 0
My mistake, I have just realised that you want it on the summary sheet so change it to:
VBA Code:
With Sheets(ans)
Sheets("Summary").Range("L3") = .Name
End With
 
Upvote 1
My mistake, I have just realised that you want it on the summary sheet so change it to:
VBA Code:
With Sheets(ans)
Sheets("Summary").Range("L3") = .Name
End With
This works somehow but does not do exactly what I need. It only brings one namesheet but I copy various rows from 2 to 4 sheets each time so I need in every change to get the name of the sheet the data where copied from. Is this possible?
 
Upvote 0
You must have put the last code outside the loop, if ans is the sheet name as in the original code, just add the lines below
Rich (BB code):
With Sheets(ans)
 .Range("e93:o93").Copy
 Sheets("Summary").Cells(Lastrowa, 1).PasteSpecial xlPasteValues
 Sheets("Summary").Cells(Lastrowa, 12).Value = ans
  Lastrowa = Sheets("Summary").Cells(Rows.Count, "A").End(xlUp).Row + 1
 End With
 With Sheets(ans)

 .Range("e141:o141").Copy
  Sheets("Summary").Cells(Lastrowa, 1).PasteSpecial xlPasteValues
  Sheets("Summary").Cells(Lastrowa, 12).Value = ans
   Lastrowa = Sheets("Summary").Cells(Rows.Count, "A").End(xlUp).Row + 1
  End With
  Next
  Application.ScreenUpdating = True
 Exit Sub
 
Upvote 0
Solution
With Sheets(ans) .Range("e93:eek:93").Copy Sheets("Summary").Cells(Lastrowa, 1).PasteSpecial xlPasteValues Sheets("Summary").Cells(Lastrowa, 12).Value = ans Lastrowa = Sheets("Summary").Cells(Rows.Count, "A").End(xlUp).Row + 1 End With With Sheets(ans) .Range("e141:eek:141").Copy Sheets("Summary").Cells(Lastrowa, 1).PasteSpecial xlPasteValues Sheets("Summary").Cells(Lastrowa, 12).Value = ans Lastrowa = Sheets("Summary").Cells(Rows.Count, "A").End(xlUp).Row + 1 End With Next Application.ScreenUpdating = True Exit Sub
Perfect!!!! Exactly what I was looking for. Thank you so much!!
 
Upvote 0
In future please mark the post the contains the solution, not your post saying it works. Thanks
 
Upvote 0

Forum statistics

Threads
1,223,885
Messages
6,175,180
Members
452,615
Latest member
bogeys2birdies

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