marreco
Member
How import data from workbook, but get dynamic data?
Code:
Option Explicit
'you can extract data from a closed file by using an
'XLM macro. Credit for this technique goes to John
'Walkenback > http://j-walk.com/ss/excel/tips/tip82.htm
Sub ImportarAVAR()
Dim FilePath$, Row&, Column&, Address$
Dim LastRw As Long
'change constants & FilePath below to suit
'***************************************
Const FileName$ = "BD_Geral_AvaES.xlsx"
Const SheetName$ = "BD_AVAR"
Const NumRows& = 100 '->> I need dinamyc range <<-
Const NumColumns& = 16
FilePath = ActiveWorkbook.Path & "\"
'***************************************
'LastRw = how get lastrow from my close workbook?
DoEvents
Application.ScreenUpdating = False
If Dir(FilePath & FileName) = Empty Then
MsgBox "O arquivo " & FileName & " não foi encontrado", , "Arquivo não existe"
Exit Sub
End If
For Row = 1 To LastRw '<<- how do it?
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("A1").Address(, , xlR1C1)
GetData = ExecuteExcel4Macro(Data)
End Function