ArtsAutosBooksBusinessEducationEntertainmentFamilyFashionFoodGamesGenderHealthHolidaysHomeHubPagesPersonal FinancePetsPoliticsReligionSportsTechnologyTravel
  • »
  • Technology»
  • Computers & Software

VBA Excel Import Data From a Workbook To Another

Updated on August 3, 2011

Import data from workbook to another

More examples here

A simple way to import or copy data from a closed book ("Livro1") for the active workbook ("Livro2"), given a set of conditions.

This example shows how we can import or copy data from one workbook to another without requiring the use of copy and paste.
Simply put this code in a VBA module and run the macro. We can also associate the macro to a button, allowing the execution of the process through a single click.

VB Code

Sub ImportData()
Workbooks.Open Filename:="C:\...\Livro1.xls"
Dim Origem As Worksheet
Dim Destino As Worksheet
Set Origem = Workbooks("Livro1.xls").Worksheets("Folha1")
Set Destino = Workbooks("Livro2.xls").Worksheets("Folha1")
k_linhas = 0
For rw = 11 To k_linhas
If Origem.Cells(rw, 1).Value > 0 And Origem.Cells(rw, 5) > 0 And Destino.Cells(rw, 17).Value =_
_Origem.Cells(rw, 1).Value Then
Destino.Cells(rw, 17).Value = Origem.Cells(rw, 1).Value
k_linhas = k_linhas + 1
End If
Next rw
Workbooks("Livro1.xls").Close SaveChanges:=False
MsgBox "Done!"
End Sub

Excel is part of my daily work routines. Almost every day, I create new spreadsheets with macros and new templates to meet the needs and requirements of quality management, budgeting and management of health and safety at work. This is my role for many years.
This purpose is simply to share my knowledge in some areas related to my work, in particular the creation of macros in Excel. Many of the examples are trivial but also many of the questions that are presented by my colleagues and friends are equally basic. I hope to be modestly useful.


    0 of 8192 characters used
    Post Comment

    • profile image

      Kumarapush 3 years ago

      Thanks for this article. The explanation is simple and also useful on importing data from another workbook.

      Apart from VBA, Data can be pulled into an excel from another excel through External reference or through Data Import facility. For more info on these techniques, refer