Solved

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

Posted on 2016-07-30
5
51 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 30

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 30

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 30

Expert Comment

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

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

809 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