Results 1 to 3 of 3

Thread: [RESOLVED] Excel Macro To Import Data from another Excel File

  1. #1
    New Member
    Join Date
    Jun 12
    Location
    Houston
    Posts
    4

    Resolved [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.

  2. #2
    PowerPoster
    Join Date
    Dec 04
    Posts
    18,532

    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:
    1. for rw 12 to 15
    2.    ThisWorkbook.Sheets(3).Range("B" & rw).Value = Wb1.Sheets(1).Range("AT" (rw + 1) * 2).Value
    3. 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
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  3. #3
    New Member
    Join Date
    Jun 12
    Location
    Houston
    Posts
    4

    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •