import data from a closed work book

i am using the code below to import data from a closed workbook,it works fine however it seems to not work with dynamic ranges.Any suggestions to have it work with dynamic ranges.Thanks.
Private Sub GetData()
    Dim theActiveSheet As Worksheet
    Dim directory As String
    Dim fileName As String
    Dim sheetName As String
    Dim cellRange As String
   
    directory = "H:\"
    fileName = "TestFile.xls"
    sheetName = "Dummy"
    cellRange = "A1:C24"
   
    Set theActiveSheet = ActiveWorkbook.Worksheets(sheetName)
     
    With theActiveSheet.Range(cellRange)
        .FormulaArray = "='" & directory & _
                        "[" & fileName & "]" _
                        & sheetName & "'!" & _
                        cellRange
        .Value = .Value
    End With
End Sub
SvgmassiveAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Rgonzo1971Commented:
Hi,

2 Solutions open the work book because dynamic ranges do not work on closed ranges

or use this Workaround by hard-coding the dynamic Range

Private Sub Workbook_BeforeClose(Cancel As Boolean)
 With Me.Names("myDynamicRange")
 .Value = ("=Dummy!" & .RefersToRange.Address)
 End With
 End Sub

 Private Sub Workbook_Open()
 Me.Names("myDynamicRange").Value =
 "=OFFSET(Dummy!$A$1,0,0,COUNTA(Dummy!$A:$A),1)"
 End Sub

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
SvgmassiveAuthor Commented:
Rgonzo1971 i  took your advice created a sub my  import time has decrease significantly.Thanks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.