Avatar of DAVID131
DAVID131
 asked on

Help with VBA to copy and paste from sheets contained in a list

Hi
I am stuck trying to write code for a sequence of copy/paste values that requires reference to a list of worksheets.
The attached contains
1. The Admin sheet with a list of sheets that have to be referenced  (the number of sheets in the list changes weekly)
2. Sheets containing the data to be copied in H6:I? this list also changes weekly (sometimes there are blank cells)
3. Summary sheet where the copied values are pasted in C6:D? (the previous weeks list is cleared before the new data is brought across)

Any help would be appreciated
Test-1.xlsm
VBAMicrosoft Excel

Avatar of undefined
Last Comment
Subodh Tiwari (Neeraj)

8/22/2022 - Mon
Subodh Tiwari (Neeraj)

Hi David,

Please try this......
Sub CopyDataToSummary()
Dim ws As Worksheet, dws As Worksheet, admWs As Worksheet
Dim lr As Long, alr As Long, i As Long, dlr As Long
Dim x
Application.ScreenUpdating = False
Set dws = Sheets("Summary")
Set admWs = Sheets("Admin")
dws.Cells.Clear
dws.Range("C5:D5").Value = Array("Code", "Sales")
alr = admWs.Cells(Rows.Count, 2).End(xlUp).Row
If alr < 8 Then
   MsgBox "There are no sheets listed on Admin Sheet.", vbExclamation, "Sheet List Not Found!"
   Exit Sub
End If
x = admWs.Range("B8:B" & alr).Value
For i = 1 To UBound(x, 1)
   On Error Resume Next
   Set ws = Sheets(x(i, 1))
   If Not ws Is Nothing Then
      lr = ws.Cells(Rows.Count, 8).End(xlUp).Row
      If lr > 5 Then
         ws.Range("H6:I" & lr).Copy dws.Range("C" & Rows.Count).End(3)(2)
      End If
   End If
Next i
Application.ScreenUpdating = True
MsgBox "Data has been copied to Summary Sheet successfully.", vbInformation, "Done!"
End Sub

Open in new window


If you have issue opening the attachment due to a temporary bug in the forum, please save it on your system before opening it otherwise just copy the above code on a standard module like Module1.
You may click the button on the Summary Sheet to run the code.
Test-1.xlsm
DAVID131

ASKER
The rapid response is very much appreciated
The code works well
The only question is how do I get only the copied cells' values to be pasted as it currently brings across formulae
ASKER CERTIFIED SOLUTION
Subodh Tiwari (Neeraj)

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
DAVID131

ASKER
Thank you very much
This is exactly matches my requirement
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
Subodh Tiwari (Neeraj)

You're welcome David! Glad to help.