Need some advice on the fastest way to load a column range from a closed workbook into an array.
Since the workbook is closed, I don't know the last row that I need. I only know the start row. It will always be 2.
I do know the sheet name in the closed workbook as well as the column I want values from, actually they are names ie. strings.
I have code that is lightning fast to get a column range from the closed workbook into a sheet, but since I have to guess on the lastrow of the column, the ending is filled with a bunch of zero values.
I am asking if anyone knows a fast way to load the range from the closed workbook directly into an array, thus bypassing the dump to a sheet first, otherwise the fastest way to get the sheet values into an array without all of the zero values that result from the unknown last row of column in the closed workbook ( A loop to check each cell for <> 0 I assume would be less than ideal).
The following is the lightning fast code I have to load the column range from a closed workbook to a sheet range:
Any advice will be most welcome!
Since the workbook is closed, I don't know the last row that I need. I only know the start row. It will always be 2.
I do know the sheet name in the closed workbook as well as the column I want values from, actually they are names ie. strings.
I have code that is lightning fast to get a column range from the closed workbook into a sheet, but since I have to guess on the lastrow of the column, the ending is filled with a bunch of zero values.
I am asking if anyone knows a fast way to load the range from the closed workbook directly into an array, thus bypassing the dump to a sheet first, otherwise the fastest way to get the sheet values into an array without all of the zero values that result from the unknown last row of column in the closed workbook ( A loop to check each cell for <> 0 I assume would be less than ideal).
The following is the lightning fast code I have to load the column range from a closed workbook to a sheet range:
VBA Code:
Sub GetRangeFromClosedWorkbook()
'
Dim SourceDirectory As String
Dim SourceFileName As String
Dim SourceRange As String
Dim SourceSheetName As String
Dim TempSheetName As String
Dim UserSelectedFile As String
'
SourceSheetName = "Sheet1" ' <--- set this to the sheet name in the closed workbook to get data from
SourceRange = "A2:A300" ' <--- Set this to the range in the closed workbook to get data from
TempSheetName = "OurScrapeSheet" ' <--- Set this to the temp sheet name used to store values from the closed workbook
'
UserSelectedFile = Application.GetOpenFilename(Title:="Please choose Mary's file to open", FileFilter:="Excel Files *.xlsx (*.xlsx),")
If UserSelectedFile = "False" Then Exit Sub ' If user cancelled then exit sub
'
SourceDirectory = Left(UserSelectedFile, InStrRev(UserSelectedFile, "\")) ' Get the path to the file
SourceFileName = Dir(UserSelectedFile) ' Get the file name and extention
'
Sheets.Add.Name = TempSheetName ' Add a temporary sheet to store values from the closed workbook
'
With ThisWorkbook.Sheets(TempSheetName).Range(SourceRange)
.FormulaArray = "='" & SourceDirectory & "[" & SourceFileName & "]" & SourceSheetName & "'!" & SourceRange ' Set Array Formula to range
.Value = .Value ' Remove formula from range, leave just the resulting value
End With
'
MsgBox "Done" ' Alert user that range from closed workbook has been loaded to sheet
'
'
' Some code here to quickly load all non zero, non blanks to Array ??? , that is, if a fast way to load from a closed workbook directly to an array is not known
'
'
Application.DisplayAlerts = False ' Turn off the alert warning for pending Sheet delete
Sheets(TempSheetName).Delete ' Delete the temporary sheet used to store values from the closed workbook
Application.DisplayAlerts = True ' Turn alert warnings back on
End Sub
Any advice will be most welcome!