Solved

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

Posted on 2016-07-30
5
43 Views
Last Modified: 2016-08-01
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
0
Comment
Question by:DAVID131
  • 3
  • 2
5 Comments
 
LVL 28

Expert Comment

by:Subodh Tiwari (Neeraj)
ID: 41735897
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
1
 

Author Comment

by:DAVID131
ID: 41735951
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
0
 
LVL 28

Accepted Solution

by:
Subodh Tiwari (Neeraj) earned 500 total points
ID: 41735954
Thanks for the feedback.
To copy only values, 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).PasteSpecial xlPasteValues
         Application.CutCopyMode = 0
      End If
   End If
Next i
dws.Range("C5").CurrentRegion.Borders.Color = vbBlack
Application.ScreenUpdating = True
MsgBox "Data has been copied to Summary Sheet successfully.", vbInformation, "Done!"
End Sub

Open in new window

Test-1.xlsm
0
 

Author Comment

by:DAVID131
ID: 41736543
Thank you very much
This is exactly matches my requirement
0
 
LVL 28

Expert Comment

by:Subodh Tiwari (Neeraj)
ID: 41736562
You're welcome David! Glad to help.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Approximate matching with VLOOKUP and MATCH seems to me to be a greatly under-used technique, and one which is vital for getting good performance out of large lookups. Until recently I would always have advised using an exact match for simplicity an…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
The viewer will learn how to simulate a series of coin tosses with the rand() function and learn how to make these “tosses” depend on a predetermined probability. Flipping Coins in Excel: Enter =RAND() into cell A2: Recalculate the random variable…
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now