Currently i am new on studying VBA for reporting and im still learning from it. moving on, may i ask a help on this one? :), my scenario is this.
- i have data on 20 workbooks (POLY, BAYO, PROPO, TIPAS, CITRO....etc) with sheet name (Sheet1)
- i have a single workbook for summary with many sheets, its sheet name is based on 20 workbook file name but not in alphabetical order. (Sheet name = CITRO, BAYO, PROPO, POLY, TIPAS....etc)
- i want to copy the data on each workbook and paste it to their respective sheet name based on file name and specific cell ("B2:F2")
- is it doable?
here's the code im trying to work on, the problem is, it is creating its own sheet instead of pasting it to my desire sheet.
Private Sub CommandButton1_Click()
Dim SourceBook As Workbook   
Dim CurrentBook As Workbook
application.screenupdating = false
Set CurrentBook = ThisWorkbook
Set SourceBook = Workbooks.Open("C:\CITRO.xlsx")
SourceBook.Sheets("Sheet1").Range("B2:F2").Copy Destination:=CurrentBook.Sheets("CITRO").Range("R2:V2")
Set SourceBook = Workbooks.Open("C:\BAYO.xlsx")
SourceBook.Sheets("Sheet1").Range("B2:F2").Copy Destination:=CurrentBook.Sheets("BAYO").Range("R2:V2")
Set SourceBook = Workbooks.Open("C:\PROPO.xlsx")
SourceBook.Sheets("Sheet1").Range("B2:F2").Copy Destination:=CurrentBook.Sheets("PROPO").Range("R2:V2")
MsgBox "Completed"
Application.Workbooks("CITRO").Close
Application.Workbooks("BAYO").Close
Application.Workbooks("PROPO").Close
'SourceBook.Close
'Set SourceBook = Nothing
'Set CurrentBook = Nothing
'ThisWorkbook.Activate
'Application.Worksheets("Summary").Activate
'Application.Worksheets("Summary").Range("B2:F2").Select
End Sub
SourceBook.Sheets("Sheet1").Copycopies the entire sheet. You need to copy only a rangeSourceBook.Sheets("Sheet1").Range("A1:B10").Copy Destination:=CurrentBook.Sheets("Summary").Range("A1")‹~~ all in one line. Adjust the range addresses to your need. - Pᴇʜ