[RESOLVED] Excel Macro To Import Data from another Excel File
Hi all,
I am really new to this and I apologize if i am in the wrong section of the forum.
Basically i need help with something simple and very generic. I have two excel files and all i want to do is import data from wb2 to wb1 and put everything in a nice bar chart or pie chart.
Only thing to notice is that wb2 is really long and has more info than i need to import and the charts on wb1 have to update automatically every time wb2 updates.
I have written something really simple and i am sure i will be laughed at but its a hobby and i try to learn on my own for fun.
Below is the code:
Code:
Private Sub Button2_Click()
Dim Wb1 As Workbook
Application.ScreenUpdating = False
Set Wb1 = Workbooks.Open("C:\path\to\file.xls")
ThisWorkbook.Sheets(3).Range("B12").Value = Wb1.Sheets(1).Range("AT26").Value 'Monday Call Presented
ThisWorkbook.Sheets(3).Range("B13").Value = Wb1.Sheets(1).Range("AT28").Value 'Tuesday CP
ThisWorkbook.Sheets(3).Range("B14").Value = Wb1.Sheets(1).Range("AT30").Value 'Wednesday CP
ThisWorkbook.Sheets(3).Range("B15").Value = Wb1.Sheets(1).Range("AT32").Value 'Thursday CP
'ThisWorkbook.Sheets(3).Range("D10").Value = Wb1.Sheets(1).Range("AT34").Value 'Friday CP
and so on.
I am not asking for anyone to rewrite my code, all i am asking is if you could please point me to the write direction as of how to manage importing an excel file and only import specific fields instead of selecting each field (B15 to A56 and etc.).
If you could please point me to the write direction I will read and do my best to improve that ugly looking code above.
Thank you in advance and if you have any questions please let me know.
Re: Excel Macro To Import Data from another Excel File
for the example above, where the cells are in a single column b12 to b15 you could do like
vb Code:
for rw 12 to 15
ThisWorkbook.Sheets(3).Range("B" & rw).Value = Wb1.Sheets(1).Range("AT" (rw + 1) * 2).Value
next
alternatively you could store you cell addresses in an array or used named ranges
without knowing the extent of the ranges you wish to copy hard to give better example
Re: Excel Macro To Import Data from another Excel File
Yeap that is exactly what i was looking for. That makes much more sense and its a lot easier to keep track of large excel files.
Thank you westconn1