Right off the bat, if this should be broken out into 2 separate threads, I apologize and will adjust accordingly (if possible). I just see the two issues somewhat related and so thought that perhaps I could get them both done in one swing.
Part 1:
The two workbooks I am using are:
WorkbookA & WorkbookB
WorkbookA contains the code in a module.
WorkbookA only has one sheet, titled/named "Summary".
WorkbookB is referenced in WorkbookA's code, as SourceFile.
Thus SourceFile = WookbookB's full path & file name, e.g. C:\Location\Of\Excel\Files\WorkbookB.xlsx
WorkbookB has multiple sheets, anywhere from 2 - 100+
The very first sheet of WorkbookB is titled/named "Summary - X","X" being the number of sheets present in WorkbookB, not counting Summary.
e.g. "Summary - 102"
"X" can change as sheets are added/removed from WorkbookB.
The purpose of the code is to copy specific cell values from the various sheets in WorkbookB over to specific columns in the "Summary" sheet in WorkbookA. However, I do not want to copy anything from the "Summary - X" sheet in WorkbookB.
I figured I could use Like with a wildcard, but I think I have my logic a bit backwards.
Here is the code:
I need to exclude the "Summary - X" sheet data from being copied, but can not reference it as Summary because the name can continually change due to how many sheets there are.
Any thoughts?
Part 2:
How can I go about cleaning up my code? Is there a simpler way to achieve what I am aiming for over all? I want to make it more efficient if possible.
Thanks!!
-Spydey
Part 1:
The two workbooks I am using are:
WorkbookA & WorkbookB
WorkbookA contains the code in a module.
WorkbookA only has one sheet, titled/named "Summary".
WorkbookB is referenced in WorkbookA's code, as SourceFile.
Thus SourceFile = WookbookB's full path & file name, e.g. C:\Location\Of\Excel\Files\WorkbookB.xlsx
WorkbookB has multiple sheets, anywhere from 2 - 100+
The very first sheet of WorkbookB is titled/named "Summary - X","X" being the number of sheets present in WorkbookB, not counting Summary.
e.g. "Summary - 102"
"X" can change as sheets are added/removed from WorkbookB.
The purpose of the code is to copy specific cell values from the various sheets in WorkbookB over to specific columns in the "Summary" sheet in WorkbookA. However, I do not want to copy anything from the "Summary - X" sheet in WorkbookB.
I figured I could use Like with a wildcard, but I think I have my logic a bit backwards.
Here is the code:
Code:
Private Sub CopyData()
Dim ws As Worksheet
Dim LR1 As Long
Application.ScreenUpdating = False
Workbooks.Open filename:=SourceFile, ReadOnly:=True
For Each ws In ActiveWorkbook.Worksheets
If ws.Name <> "Summary" Then
LR1 = ThisWorkbook.Sheets("Summary").Range("A" & Rows.Count).End(xlUp).Row + 1
With ws
.Range("B2").Copy
ThisWorkbook.Sheets("Summary").Range("A" & LR1).PasteSpecial xlValues
.Range("B6").Copy
ThisWorkbook.Sheets("Summary").Range("B" & LR1).PasteSpecial xlValues
.Range("B12").Copy
ThisWorkbook.Sheets("Summary").Range("E" & LR1).PasteSpecial xlValues
.Range("B13").Copy
ThisWorkbook.Sheets("Summary").Range("C" & LR1).PasteSpecial xlValues
.Range("B4").Copy
ThisWorkbook.Sheets("Summary").Range("D" & LR1).PasteSpecial xlValues
.Range("B5").Copy
ThisWorkbook.Sheets("Summary").Range("F" & LR1).PasteSpecial xlValues
.Range("B9").Copy
ThisWorkbook.Sheets("Summary").Range("G" & LR1).PasteSpecial xlValues
.Range("G3").Copy
ThisWorkbook.Sheets("Summary").Range("H" & LR1).PasteSpecial xlValues
.Range("G4").Copy
ThisWorkbook.Sheets("Summary").Range("J" & LR1).PasteSpecial xlValues
.Range("G6").Copy
ThisWorkbook.Sheets("Summary").Range("I" & LR1).PasteSpecial xlValues
.Range("J4").Copy
ThisWorkbook.Sheets("Summary").Range("K" & LR1).PasteSpecial xlValues
.Range("J5").Copy
ThisWorkbook.Sheets("Summary").Range("L" & LR1).PasteSpecial xlValues
.Range("J6").Copy
ThisWorkbook.Sheets("Summary").Range("M" & LR1).PasteSpecial xlValues
.Range("J7").Copy
ThisWorkbook.Sheets("Summary").Range("N" & LR1).PasteSpecial xlValues
.Range("J8").Copy
ThisWorkbook.Sheets("Summary").Range("O" & LR1).PasteSpecial xlValues
.Range("J9").Copy
ThisWorkbook.Sheets("Summary").Range("P" & LR1).PasteSpecial xlValues
.Range("J10").Copy
ThisWorkbook.Sheets("Summary").Range("Q" & LR1).PasteSpecial xlValues
.Range("J11").Copy
ThisWorkbook.Sheets("Summary").Range("R" & LR1).PasteSpecial xlValues
.Range("J12").Copy
ThisWorkbook.Sheets("Summary").Range("S" & LR1).PasteSpecial xlValues
End With
End If
Next ws
ActiveWorkbook.Close (False)
End Sub
I need to exclude the "Summary - X" sheet data from being copied, but can not reference it as Summary because the name can continually change due to how many sheets there are.
Any thoughts?
Part 2:
How can I go about cleaning up my code? Is there a simpler way to achieve what I am aiming for over all? I want to make it more efficient if possible.
Thanks!!
-Spydey