Tuesday, 3 January 2017

excel - VBA copy cell range to public 2D array

I'm using VBA with AutoCAD, and need to extract some data from an Excel Sheet.



Basically I want to:




  • Declare a public 2D string array: FULL_TABLE.

  • At initialization open the excel document, get a cell range, then
    close the document.

  • Put all the values from the cell range to the 2D string array
    FULL_TABLE



What I have so far:



Declared in Module 1:



 Public FULL_TABLE() As String


Different module:



Private Sub UserForm_Initialize()

'Excel Path
Dim excelPath As String
excelPath = "O:\myData.xls"

'Open Excel File
Set exapp = CreateObject("Excel.Application")
exapp.Visible = False
exapp.Workbooks.Open (excelPath)
Set wsheet = exapp.ActiveSheet

'Get cell values
'privateFULL_TABLE is not public and can only be accessed here
'If I try to add values to FULL_TABLE (public) I get errors

Dim privateFULL_TABLE As Variant
privateFULL_TABLE = wsheet.Range("B5:AV50")

'here I can access values correctly from privateFULL_TABLE

'Close File
exapp.Quit
Set exapp = Nothing

End Sub


Question:



How can I convert a range of cells to public 2D string array?

No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...