Populate an array from a closed WB

vadius

Board Regular
Joined
Jul 5, 2011
Messages
70
Hi all,

I am trying to populate a array(40,1) with text data in the first column of a closed book ("CAC40.xls"). I built something fromn some codes I found,, but I have a "mismatch" problem on the line myarray(x, y) = GetData(FilePath, FileName, SheetName, Address)

Has anyone an idea ?

Thanks

Code:
Sub GetDataDemo()

      Dim FilePath$, Row&, Column&, Address$
   
      
      'change constants & FilePath below to suit
      '***************************************
      Const FileName$ = "CAC40.xls"
      Const SheetName$ = "Sheet1"
      Const NumRows& = 40
      Const NumColumns& = 1
      FilePath = "H:\Outlook\"
      '***************************************
      
      DoEvents
      Excel.Application.ScreenUpdating = False
      If Dir(FilePath & FileName) = Empty Then
            MsgBox "The file " & FileName & " was not found", , "File Doesn't Exist"
            Exit Sub
      End If
      
      
      
      Dim myarray(NumRows, NumColumns) As Long
      Dim x As Long
      Dim y As Long
      For x = 1 To NumRows
                For y = 1 To NumColumns
                Address = Cells(x, y).Address
                myarray(x, y) = GetData(FilePath, FileName, SheetName, Address)
                Next y
      Next x
      
      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
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
Try this:-
Code:
[COLOR="Navy"]Sub[/COLOR] MG18Aug37
[COLOR="Navy"]Dim[/COLOR] FilePath$, Row&, Column&, Address$, Ray(1 To 20, 1 To 1)
Const FileName$ = "CAC40.xls"
Const SheetName$ = "Sheet1"
Const NumRows& = 20
Const NumColumns& = 1
'[COLOR="Green"][B]Change Filepath to suit[/B][/COLOR]
FilePath = "C:\Documents and Settings\test\Desktop\"
DoEvents
Application.ScreenUpdating = False
[COLOR="Navy"]If[/COLOR] Dir(FilePath & FileName) = Empty [COLOR="Navy"]Then[/COLOR]
MsgBox "The file " & FileName & " was not found", , "File Doesn'[COLOR="Green"][B]t Exist"[/B][/COLOR]
[COLOR="Navy"]Exit[/COLOR] [COLOR="Navy"]Sub[/COLOR]
[COLOR="Navy"]End[/COLOR] If
[COLOR="Navy"]For[/COLOR] Row = 1 To NumRows
[COLOR="Navy"]For[/COLOR] Column = 1 To NumColumns
Address = Cells(Row, Column).Address
'[COLOR="Green"][B]Data sent to active sheet for test !!![/B][/COLOR]
Cells(Row, Column) = GetData(FilePath, FileName, SheetName, Address)
'[COLOR="Green"][B]Data sent to array[/B][/COLOR]
Ray(Row, Column) = GetData(FilePath, FileName, SheetName, Address)
Ray(Row, Column) = IIf(Ray(Row, Column) = 0, "", Ray(Row, Column))
Columns.AutoFit
[COLOR="Navy"]Next[/COLOR] Column
[COLOR="Navy"]Next[/COLOR] Row
Sheets("Sheet3").Range("A1").Resize(20, 1) = Ray
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]

Private Function GetData(Path, File, Sheet, Address)
[COLOR="Navy"]Dim[/COLOR] Data$
Data = "'[COLOR="Green"][B]" & Path & "[" & File & "]" & Sheet & "'!" & _[/B][/COLOR]
Range(Address).Range("A1").Address(, , xlR1C1)
GetData = ExecuteExcel4Macro(Data)
[COLOR="Navy"]End[/COLOR] Function
Regards Mick
 
Upvote 0

Forum statistics

Threads
1,224,521
Messages
6,179,291
Members
452,902
Latest member
Knuddeluff

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top