Consulting

Results 1 to 2 of 2

Thread: Importing Data from Closed Workbook

  1. #1
    VBAX Newbie
    Joined
    Jan 2016
    Posts
    1
    Location

    Importing Data from Closed Workbook

    Hi,

    I am currently attempting to write a VBA code to import data from a workbook. My goal is to import the data into one list from different tabs from specific rows. I don't take credit for I started, I found it on another forum.

    What I need: I need it to include data from Obligations and Accruals tab. Currently, it is producing the data I need from the Disbursements tab. From the Obligations tab, I need Data from D3 down 146 rows. From the Accruals tab, I need the Data from D3 down 143 rows. I would like to combine all this data into one list from the import.

    Here is what I have:

    Sub TestCode()

    Dim FilePath$, Row&, Column&, Address$


    Const FileName$ = "Test Matrix"
    Const SheetName$ = "Disbursements"
    Const NumRows& = 338
    Const NumColumns& = 1
    FilePath = ActiveWorkbook.Path & "\"


    DoEvents
    Application.ScreenUpdating = False
    If Dir(FilePath & FileName) = Empty Then
    MsgBox "The file " & FileName & " was not found", , "File Doesn't Exist"
    Exit Sub
    End If
    For Row = 1 To NumRows
    For Column = 1 To NumColumns
    Address = Cells(Row, Column).Address
    Cells(Row, Column) = GetData(FilePath, FileName, SheetName, Address)
    Columns.AutoFit
    Next Column
    Next Row
    ActiveWindow.DisplayZeros = False
    End Sub


    Private Function GetData(Path, File, Sheet, Address)
    Dim Data$
    Data = "'" & Path & "[" & File & "]" & Sheet & "'!" & _
    Range(Address).Range("D3").Address(, , xlR1C1)
    GetData = ExecuteExcel4Macro(Data)
    End Function

    Thank You for any help!

    Best Regards,

    Chris

  2. #2
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,368
    Location
    Quote Originally Posted by cam5778 View Post
    ...I would like to combine all this data into one list from the import.
    Do you mean you want the data down one column?

    Also - while I have not tried your current code, I seem to remember only being able to return one cell's value at a time using this method. Is there any objection to simply opening the file(s) and returning the data en mass? I think it would be much faster. for that matter, we could probably plunk a formula into the destination range(s) and overwrite with .Value = .Value.

    Mark

Posting Permissions

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