Hi I am new to VBA so apologies if my code looks bad. I have a macro that updates specific cells in one column in a master worksheet, pulling data from a specific workbook. However, I would like my macro to know that I am pulling data from another workbook without specifying workbooks.Open(FileName)
so I can automatically update my master sheet from any other open workbook.
The main problem is, since the same data are found in different cells in multiple workbooks, I am wondering how I can copy these data while referring to their different cells for each workbook.
Code:
Sub UpdateData()
Dim wb1 As Workbook
Dim wb2 As Workbook
Dim ws1 As Worksheet
Dim EMS As Worksheet
Dim TD As Worksheet
Dim JV1 As Worksheet
Application.ScreenUpdating = False
Set wb1 = ThisWorkbook
wb1.Application.Visible = True
Set ws1 = wb1.Sheets(" Master Data")
Set wb2 = Workbooks.Open("C:\Users\HONL120\Desktop\Sept HC Reports\HR Headcount Report 2018 Australia SEPTEMBER.XLSX")
wb2.Application.Visible = False
Set EMS = Sheets("Employee Movement Summary")
EMS.Range("J19").Copy
ws1.Range("J34").PasteSpecial xlPasteValues
Set TD = Sheets("Turnover Dashboard")
TD.Range("J44").Copy
ws1.Range("J2").PasteSpecial xlPasteValues
TD.Range("J47").Copy
ws1.Range("J3").PasteSpecial xlPasteValues
EMS.Range("J10").Copy
ws1.Range("J5").PasteSpecial xlPasteValues
EMS.Range("J11").Copy
ws1.Range("J6").PasteSpecial xlPasteValues
EMS.Range("J16").Copy
ws1.Range("J7").PasteSpecial xlPasteValues
EMS.Range("J17").Copy
ws1.Range("J8").PasteSpecial xlPasteValues
TD.Range("K3:K7").Copy
ws1.Range("J10:J14").PasteSpecial xlPasteValues
TD.Range("J32:J43").Copy
ws1.Range("J16:J27").PasteSpecial xlPasteValues
Set JV1 = Sheets("JV1")
JV1.Range("Q26").Copy
ws1.Range("J29").PasteSpecial xlPasteValues
JV1.Range("Q28:Q29").Copy
ws1.Range("J30:J31").PasteSpecial xlPasteValues
Application.ScreenUpdating = True
wb2.Application.Visible = True
End Sub
So for example, in this other workbook, the range to copy TD.Range("J44").Copy
is based on cell J44, but this may not be the case in other workbooks with the same type of data.
Likewise, I would like to copy from another open workbook no matter what is opened, and not specifying a file name as such Set wb2 = Workbooks.Open("C:\Users\HONL120\Desktop\Sept HC Reports\HR Headcount Report 2018 Australia SEPTEMBER.XLSX")
.
Is there a way to automate this? or must I find the specific cells to copy in each workbook manually? Thanks in advance!