VBA to copy another excel file contents to current workbook - vba

This is what I want to achieve:
I want to copy the contents of the entire first sheet in the most recently modified excel file in a specified directory. I then want to paste the values of this copy operation to the first sheet of the current workbook.
I am aware there are macros to get the last modified file in a directory but I am unsure of a quick and clean way to implement this.

See below. This will use the current active workbook and look in C:\Your\Path for the Excel file with the latest modify date. It will then open the file and copy contents from the first sheet and paste them in your original workbook (on the first sheet):
Dim fso, fol, fil
Dim wkbSource As Workbook, wkbData As Workbook
Dim fileData As Date
Dim fileName As String, strExtension As String
Set wkbSource = ActiveWorkbook
Set fso = CreateObject("Scripting.FileSystemObject")
Set fol = fso.GetFolder("C:\Your\Path")
fileData = DateSerial(1900, 1, 1)
For Each fil In fol.Files
strExtension = fso.GetExtensionName(fil.Path)
If Left$(strExtension, 3) = "xls" Then
If (fil.DateLastModified > fileData) Then
fileData = fil.DateLastModified
fileName = fil.Path
End If
End If
Next fil
Set wkbData = Workbooks.Open(fileName, , True)
wkbData.Sheets(1).Cells.Copy
wkbSource.Sheets(1).Range("A1").PasteSpecial Paste:=xlValues
Application.CutCopyMode = False
wkbData.Close
Set fso = Nothing
Set fol = Nothing
Set flc = Nothing
Set wkbData = Nothing

I had nothing better to do on my lunch - so here goes.
To fire it use: getSheetFromA()
Put this in the current file:
Dim most_recent_file(1, 2) As Variant
Sub getSheetFromA()
' STEP 1 - Delete first sheet in this workbook
' STEP 2 - Look through the folder and get the most recently modified file path
' STEP 3 - Copy the first sheet from that file to the start of this file
' STEP 1
' Delete the first sheet in the current file (named incase if deleting the wrong one..)
delete_worksheet ("Sheet1")
' STEP 2
' Now look for the most recent file
Dim folder As String
folder = "C:\Documents and Settings\Chris\Desktop\foldername\"
Call recurse_files(folder, "xls")
' STEP 3
Dim most_recently_modified_sheet As String
most_recently_modified_sheet = most_recent_file(1, 0)
getSheet most_recently_modified_sheet, 1
End Sub
Sub getSheet(filename As String, sheetNr As Integer)
' Copy a sheet from an external sheet to this workbook and put it first in the workbook.
Dim srcWorkbook As Workbook
Set srcWorkbook = Application.Workbooks.Open(filename)
srcWorkbook.Worksheets(sheetNr).Copy before:=ThisWorkbook.Sheets(1)
srcWorkbook.Close
Set srcWorkbook = Nothing
End Sub
Sub delete_worksheet(sheet_name)
' Delete a sheet (turn alerting off and on again to avoid prompts)
Application.DisplayAlerts = False
Sheets(sheet_name).Delete
Application.DisplayAlerts = True
End Sub
Function recurse_files(working_directory, file_extension)
With Application.FileSearch
.LookIn = working_directory
.SearchSubFolders = True
.filename = "*." & file_extension
.MatchTextExactly = True
.FileType = msoFileTypeAllFiles
If .Execute() > 0 Then
number_of_files = .FoundFiles.Count
For i = 1 To .FoundFiles.Count
vFile = .FoundFiles(i)
Dim temp_filename As String
temp_filename = vFile
' the next bit works by seeing if the current file is newer than the one in the array, if it is, then replace the current file in the array.
If (most_recent_file(1, 1) <> "") Then
If (FileLastModified(temp_filename) > most_recent_file(1, 1)) Then
most_recent_file(1, 0) = temp_filename
most_recent_file(1, 1) = FileLastModified(temp_filename)
End If
Else
most_recent_file(1, 0) = temp_filename
most_recent_file(1, 1) = FileLastModified(temp_filename)
End If
Next i
Else
MsgBox "There were no files found."
End If
End With
End Function
Function FileLastModified(strFullFileName As String)
' Taken from: http://www.ozgrid.com/forum/showthread.php?t=27740
Dim fs As Object, f As Object, s As String
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.GetFile(strFullFileName)
s = f.DateLastModified
FileLastModified = s
Set fs = Nothing: Set f = Nothing
End Function

Related

Is there method similar to 'Find' available when we Loop through folder (of files) using Dir Function in excel vba?

As we know, we use Find() method to find whether a string or any Microsoft Excel data type exists in an excel.
(Usually we do it on set of data)
I want to know if any such method available when we loop through folder(of files) using Dir function.
Situation:
I have an excel - 'FileNames.xlsx' in which 'Sheet1' has names of files having extensions .pdf/.jpg/.jpeg/.xls/.xlsx/.png./.txt/.docx/ .rtf in column A.
I have a folder named 'Folder' which has most(or all) of the files from 'FileNames.xlsx'.
I have to check whether all the file-names mentioned in the 'FileNames.xlsx' exist in 'Folder'.
For this I have written the below VBScript(.vbs):
strMessage =Inputbox("Enter No. of Files in Folder","Input Required")
set xlinput = createobject("excel.application")
set wb123 =xlinput.workbooks.Open("E:\FileNames.xlsx")
set sh1 =wb123.worksheets("Sheet1")
For i = 2 to strMessage +1
namei = sh1.cells(i,1).value
yesi = "E:\Folder"+ namei +
If namei <> yesi Then
sh1.cells(i,1).Interior.Color = vbRed
Else
End If
Next
msgbox "Success"
xlinput.quit
As I wasn't able to get the required Output I tried it recording a small Excel VBA Macro. (Changed FileNames.xlsx to FileNames.xlsm)
Sub LoopThroughFiles()
Dim lastRow As Long
lastRow = Sheets("Sheet1").UsedRange.Rows.Count
Dim MyFolder As String
Dim filename As Range
Dim MyFile As String
MyFolder = "E:\Folder"
For Each filename In Worksheets("Sheet1").Range("A2A:" & lastRow)
MyFile = Dir(MyFolder & "\*.xlsx")
'Here I actually need to pass all file extensions to Dir
Do While MyFile <> ""
If filename = MyFile Then
'Do Nothing
Else
filename.Interior.Color = vbRed
MyFile = Dir
Next
End Sub
The above is a failed attempt.
I thought of trying it with method similar to Find()
Sub LoopThroughFiles()
Dim lastRow As Long
'Dim LastFile As Long
'Is there need of it (LastFile variable)? I kept this variable
'to save (prior known) count of files in folder.
lastRow = Sheets("Sheet1").UsedRange.Rows.Count
'LastFile = 'Pass count of Files in folder to this variable.
Dim fileName As Range
For Each fileName In Worksheets("Sheet1").Range("A2:A" & lastRow)
Dim rngFnder As Range
On Error Resume Next
'Error at below line.
Set rngFnder = Dir("E:\Folder\").Find(filename)
'This line gives me error 'Invalid Qualifier'
'I am trying to use method similar to Find()
If rngFnder Is Nothing Then
filename.Interior.Color = vbRed
End If
Next
End Sub
But, I couldn't achieve the result. Can anyone tell me is there any such function available to 'Find' whether all filenames in an excel exist in a folder after looping through folder using Dir?
As per my knowledge, Dir function works with only one file extension at a time.
Is it possible to use Dir function for multiple file extensions at a time?
Expected Output:
Assume I have 8 filenames in 'FileNames(.xlsx/.xlsm)'. Out of which Arabella.pdf and Clover.png are not found in 'Folder', Then I want to color cells for these filenames in red background in excel as in below image.
Sub LoopThroughFiles()
Dim lastRow As Long
lastRow = Sheets("Sheet1").UsedRange.Rows.Count
Dim MyFolder As String
Dim filename As Range
Dim MyFile As String
MyFolder = "E:\Folder"
For Each filename In Worksheets("Sheet1").Range("A2:A" & lastRow)
MyFile = MyFolder & "\" & filename
If Not FileExists(MyFile) Then
filename.Interior.Color = vbRed
End If
Next
End Sub
Public Function FileExists(strFullpathName As String) As Boolean
If Dir(strFullpathName) <> "" Then
FileExists = True
Else
FileExists = False
End If
End Function
You can output a list of the files that are contained in the folder. I found a really helpful tutorial on that here: http://software-solutions-online.com/2014/03/05/list-files-and-folders-in-a-directory/#Jump1
If you then loop through both the original and the output lists and look for a match. Easiest is to first colour them all red, and un-colour the matches. Else you would need an additional if-statement that states: When you reach the last element in the original list, and no match has been found, then colour red.
Edit: For continuity's sake I copied the code bits of the link I mentioned above:
Getting all file names form within 1 folder:
Sub Example1()
Dim objFSO As Object
Dim objFolder As Object
Dim objFile As Object
Dim i As Integer
'Create an instance of the FileSystemObject
Set objFSO = CreateObject("Scripting.FileSystemObject")
'Get the folder object
Set objFolder = objFSO.GetFolder("D:StuffFreelancesWebsiteBlogArraysPics")
i = 1
'loops through each file in the directory and prints their names and path
For Each objFile In objFolder.Files
'print file name
Cells(i + 1, 1) = objFile.Name
'print file path
Cells(i + 1, 2) = objFile.Path
i = i + 1
Next objFile
End Sub

Rows are repeating for a single XML in Excel using VBA

I am suing the below macro that I found on the net to extract the data from XML to Excel.
Sub ListFiles()
'LISTFILES AND LISTMYFILES MODIFIED FROM
'--------------------------------------------------------------------
'DECLARE AND SET VARIABLEs
Dim ShellApplication As Object
Set ShellApplication = CreateObject("Shell.Application").BrowseForFolder(0, "Please choose a folder", 0, OpenAt)
Path = ShellApplication.self.Path
Set ShellApplication = Nothing
[a3] = "XML"
[b3] = "Files"
'--------------------------------------------------------------------
'DEFAULT PATH FROM HIDDEN SHEET
Call ListMyFiles(Path, True)
End Sub
Sub ListMyFiles(mySourcePath, IncludeSubfolders)
Set MyObject = New Scripting.FileSystemObject
Set mySource = MyObject.GetFolder(mySourcePath)
'With SearchXML
Application.ScreenUpdating = False
'--------------------------------------------------------------------
'FIND XML FILES ONLY, APPLY SEARCH CRIERIA, DISPLAY MATCHES ONLY
For Each myfile In mySource.Files
If Right(myfile.Name, 3) = "XML" Or Right(myfile.Name, 3) = "xml" Then 'IS XML?
LastRow = ActiveSheet.Cells(Rows.Count, 2).End(xlUp).Row
'-------------------------------------------------------------
'IMPORT XML FILE
ActiveWorkbook.XmlImport URL:=mySource & "\" & myfile.Name, _
ImportMap:=Nothing, Overwrite:=True, Destination:=Range("$A$" & LastRow + 1)
'------------------------------------------------------------
'DELETE MAPS
maps = ActiveWorkbook.XmlMaps.Count
For I = 1 To maps
ActiveWorkbook.XmlMaps(1).Delete
Next I
End If
Next
If IncludeSubfolders Then 'SEARCH SUBFOLDERS FOR SAME CRITERIA
For Each MySubFolder In mySource.SubFolders
Call ListMyFiles(MySubFolder.Path, True)
Next
End If
'End With
Application.ScreenUpdating = True
End Sub
Public Sub ClearSheet()
Cells.Select
Selection.ClearContents
[a1].Select
End Sub
However, it is generating multiple rows for some XMLs, but I want a single row for a single XML.
It may depend on the XML that you import, but there is a part that may need correction :
maps = ActiveWorkbook.XmlMaps.Count
For I = 1 To maps
ActiveWorkbook.XmlMaps(1).Delete
Next I
You should go through it backward, by adding Step -1
maps = ActiveWorkbook.XmlMaps.Count
For i = 1 To maps Step -1
ActiveWorkbook.XmlMaps(i).Delete
Next i
Hope that helped! ;)

Extract text from column D to txt files and name files based on content of column C

Apologies for a noob question but I've been fiddling around with this code:
https://stackoverflow.com/a/7151963/3672159
and can't seem to get it modified to do the following (very slight modifications of the code above):
Take as input a worksheet that is called "Export Data" (rather than "Sheet1" as in the existing code; the space seems to cause problems)
Automatically create an empty file for each cell of column D, which should have as its content the value of the respective D cell (same as with the "Disclaimer" data in the code above)
Name each file based on the values of the corresponding C cells (so for me it's name=column C, content=column D rather than B and A in the original code).
I've modified the code as follows:
Sub ExportFiles()
Dim sExportFolder, sFN
Dim rStoreId As Range
Dim rAbstract As Range
Dim oSh As Worksheet
Dim oFS As Object
Dim oTxt As Object
'sExportFolder = path to the folder you want to export to
'oSh = The sheet where your data is stored
sExportFolder = "my file path\txt"
Set oSh = Export Data
Set oFS = CreateObject("Scripting.Filesystemobject")
For Each rStoreId In oSh.UsedRange.Columns("D").Cells
Set rAbstract = rStoreId.Offset(, -1)
'Add .txt to the article name as a file name
sFN = rStoreId.Value & ".txt"
Set oTxt = oFS.OpenTextFile(sExportFolder & "\" & sFN, 2, True)
oTxt.Write rAbstract.Value
oTxt.Close
Next
End Sub
The only thing this does (as does the original code) is create one empty unnamed txt file.
Any help is greatly appreciated!
Try this...
Sub ExportFiles()
Dim sExportFolder, sFN
Dim rStoreId As Range
Dim rAbstract As Range
Dim oSh As Worksheet
Dim oFS As Object
Dim oTxt As Object
'sExportFolder = path to the folder you want to export to
'oSh = The sheet where your data is stored
sExportFolder = "C:\Users\Rich\Desktop"
Set oSh = ThisWorkbook.Sheets("Export Data")
Set oFS = CreateObject("Scripting.Filesystemobject")
For Each rStoreId In oSh.Columns("D").Cells
If IsEmpty(rStoreId.Value) Then
Exit For
End If
Set rAbstract = rStoreId.Offset(, -1)
'Add .txt to the article name as a file name
sFN = rStoreId.Value & ".txt"
Set oTxt = oFS.OpenTextFile(sExportFolder & "\" & sFN, 2, True)
oTxt.Write rAbstract.Value
oTxt.Close
Next
End Sub
You need to select the sheet correctly with (Assuming it is within the same workbook as the code)...
Set oSh = ThisWorkbook.Sheets("Export Data")
And I changed how you were iterating through the range...
For Each rStoreId In oSh.Columns("D").Cells
If IsEmpty(rStoreId.Value) Then
Exit For
End If
Next
This just goes through column D's cells until it hits an empty one, I couldn't quite get it working using UsedRange and this (more old skool) method works in my tests.
This works for me. It writes each value in cells in column D to a text file that is named based on the entry in column C and puts all text files in user specified folder:
Sub ExportFiles()
Dim exportFolder As String
Dim fso As FileSystemObject
Dim stream As TextStream
Dim cl As Range
exportFolder = "C:\User\ExportFolder" //Add you folder path here
Set fso = New FileSystemObject
For Each cl In Worksheets("Export Data").UsedRange.Columns("D").Cells
Set stream = fso.CreateTextFile(filepath & "\" & cl.Offset(0, -1).Value & ".txt", 2, True)
stream.Write cl.Value
stream.Close
Next
End Sub

Merge multiple excel files, while keeping the Header Row from first File only

I tried searching for this a lot, but could not find a satisfactory answer. Sorry if it's a repost.
What I basically want is to merge multiple excel files into one workbook. I only want to keep the header row from the first excel file and ignore header row of the remaining excel files (as they are all the same). So the end result should be the Header + data from the first excel file and from the remaining excel file I only need the data rows, not the first row which has column heading similar to the first file.
The below copy paste all the rows and columns from all the excel files. Thank you for helping me.
For wbCounter = 1 To UBound(books)
Set wbSource = oExcel.Workbooks.Open(books(wbCounter))
For wsCounter = 1 To wbSource.Sheets.Count
Set wsSource = wbSource.Sheets(wsCounter)
If wsSource.Name Like selectSheetStr Then
emptySheet = True
If cbOptionIgnoreEmpty.Value = True And wsSource.UsedRange.Address = "$A$1" Then
emptySheet = True
Else
emptySheet = False
End If
If emptySheet = False Then
mergedWorksheetName = wsSource.Name
sheetExist = SheetExists(mergedWorksheetName, wbResult)
If (cbOptionAppendData.Value = True And sheetExist = True) Then
Set wsMergeResult = wbResult.Sheets(mergedWorksheetName)
wsSource.UsedRange.Copy
wsMergeResult.Cells(wsMergeResult.UsedRange.Rows.Count + 1, 1).Resize(wsSource.UsedRange.Rows.Count, wsSource.UsedRange.Columns.Count).PasteSpecial Paste:=xlPasteAll, Operation:=xlNone
mergedWorksheetCount = mergedWorksheetCount + 1
'Name of Worksheet
wsResult.Cells(mergedWorksheetCount + 1, 1) = wsMergeResult.Name
'Fullpath of Workbook
wsResult.Cells(mergedWorksheetCount + 1, 2) = wbSource.FullName
Else
wsSource.Copy After:=wbResult.Sheets(wbResult.Sheets.Count)
mergedWorksheetCount = mergedWorksheetCount + 1
wsResult.Cells(mergedWorksheetCount + 1, 1) = .ActiveSheet.Name
wsResult.Cells(mergedWorksheetCount + 1, 2) = wbSource.FullName
End If
End If
End If
Next wsCounter
wbSource.Close SaveChanges:=False
Next wbCounter
Try replacing the line...
wsSource.UsedRange.Copy
...with:
wsSource.UsedRange.Resize(wsSource.UsedRange.Rows.Count - 1, wsSource.UsedRange.Columns.Count).Offset(1, 0).Copy
This should copy the used range minus the frist row.
This code will get you the path of all the files in a specific folder
Option Explicit
Sub CountRows()
Dim MyObject As Scripting.FileSystemObject
Set MyObject = New Scripting.FileSystemObject
Dim mySource As Folder
Dim myFile As Scripting.File
Dim strPath As String
Set mySource = MyObject.GetFolder("D:\") ' or any other folder
For Each myFile In mySource.Files
strPath = myFile.Path
Next
End Sub
this code will open those workbooks and modify their cells
dim wrkbook as workbook
set wrkbook = workbooks.open(strPath) ' the path comes from the code above
'modify the workbook
wrkbook.worksheets.item(1).cells(1, 1) = "something"

Copy data from closed workbook based on variable user defined path

I have exhausted my search capabilities looking for a solution to this. Here is an outline of what I would like to do:
User opens macro-enabled Excel file
Immediate prompt displays for user to enter or select file path of desired workbooks. They will need to select two files, and the file names may not be consistent
After entering the file locations, the first worksheet from the first file selection will be copied to the first worksheet of the macro-enabled workbook, and the first worksheet of the second file selection will be copied to the second worksheet of the macro-enabled workbook.
I've come across some references to ADO, but I am really not familiar with that yet.
Edit: I have found a code to import data from a closed file. I will need to tweak the range to return the variable results.
Private Function GetValue(path, file, sheet, ref)
path = "C:\Users\crathbun\Desktop"
file = "test.xlsx"
sheet = "Sheet1"
ref = "A1:R30"
' Retrieves a value from a closed workbook
Dim arg As String
' Make sure the file exists
If Right(path, 1) <> "\" Then path = path & "\"
If Dir(path & file) = "" Then
GetValue = "File Not Found"
Exit Function
End If
' Create the argument
arg = "'" & path & "[" & file & "]" & sheet & "'!" & _
Range(ref).Range("A1").Address(, , xlR1C1)
' Execute an XLM macro
GetValue = ExecuteExcel4Macro(arg)
End Function
Sub TestGetValue()
path = "C:\Users\crathbun\Desktop"
file = "test"
sheet = "Sheet1"
Application.ScreenUpdating = False
For r = 1 To 30
For C = 1 To 18
a = Cells(r, C).Address
Cells(r, C) = GetValue(path, file, sheet, a)
Next C
Next r
Application.ScreenUpdating = True
End Sub
Now, I need a command button or userform that will immediately prompt the user to define a file path, and import the data from that file.
I don't mind if the files are opened during process. I just didn't want the user to have to open the files individually. I just need them to be able to select or navigate to the desired files
Here is a basic code. This code asks user to select two files and then imports the relevant sheet into the current workbook. I have given two options. Take your pick :)
TRIED AND TESTED
OPTION 1 (Import the Sheets directly instead of copying into sheet1 and 2)
Option Explicit
Sub Sample()
Dim wb1 As Workbook, wb2 As Workbook
Dim Ret1, Ret2
Set wb1 = ActiveWorkbook
'~~> Get the first File
Ret1 = Application.GetOpenFilename("Excel Files (*.xls*), *.xls*", _
, "Please select first file")
If Ret1 = False Then Exit Sub
'~~> Get the 2nd File
Ret2 = Application.GetOpenFilename("Excel Files (*.xls*), *.xls*", _
, "Please select Second file")
If Ret2 = False Then Exit Sub
Set wb2 = Workbooks.Open(Ret1)
wb2.Sheets(1).Copy Before:=wb1.Sheets(1)
ActiveSheet.Name = "Blah Blah 1"
wb2.Close SaveChanges:=False
Set wb2 = Workbooks.Open(Ret2)
wb2.Sheets(1).Copy After:=wb1.Sheets(1)
ActiveSheet.Name = "Blah Blah 2"
wb2.Close SaveChanges:=False
Set wb2 = Nothing
Set wb1 = Nothing
End Sub
OPTION 2 (Import the Sheets contents into sheet1 and 2)
Option Explicit
Sub Sample()
Dim wb1 As Workbook, wb2 As Workbook
Dim Ret1, Ret2
Set wb1 = ActiveWorkbook
'~~> Get the first File
Ret1 = Application.GetOpenFilename("Excel Files (*.xls*), *.xls*", _
, "Please select first file")
If Ret1 = False Then Exit Sub
'~~> Get the 2nd File
Ret2 = Application.GetOpenFilename("Excel Files (*.xls*), *.xls*", _
, "Please select Second file")
If Ret2 = False Then Exit Sub
Set wb2 = Workbooks.Open(Ret1)
wb2.Sheets(1).Cells.Copy wb1.Sheets(1).Cells
wb2.Close SaveChanges:=False
Set wb2 = Workbooks.Open(Ret2)
wb2.Sheets(1).Cells.Copy wb1.Sheets(2).Cells
wb2.Close SaveChanges:=False
Set wb2 = Nothing
Set wb1 = Nothing
End Sub
The function below reads data from a closed Excel file and returns the result in an array. It loses formatting, formulas etc. You might want to call the isArrayEmpty function (at the bottom) in your main code to test that the function returned something.
Public Function getDataFromClosedExcelFile(parExcelFileName As String, parSheetName As String) As Variant
'see http://www.ozgrid.com/forum/showthread.php?t=19559
'returns an array (1 to nRows, 1 to nCols) which should be tested with isArrayEmpty in the calling function
Dim locConnection As New ADODB.Connection
Dim locRst As New ADODB.Recordset
Dim locConnectionString As String
Dim locQuery As String
Dim locCols As Variant
Dim locResult As Variant
Dim i As Long
Dim j As Long
On Error GoTo error_handler
locConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" _
& "Data Source=" & parExcelFileName & ";" _
& "Extended Properties=""Excel 8.0;HDR=YES"";"
locQuery = "SELECT * FROM [" & parSheetName & "$]"
locConnection.Open ConnectionString:=locConnectionString
locRst.Open Source:=locQuery, ActiveConnection:=locConnection
If locRst.EOF Then 'Empty sheet or only one row
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'''''' FIX: an empty sheet returns "F1"
'''''' http://support.microsoft.com/kb/318373
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
If locRst.Fields.Count = 1 And locRst.Fields(0).Name = "F1" Then Exit Function 'Empty sheet
ReDim locResult(1 To 1, 1 To locRst.Fields.Count) As Variant
For i = 1 To locRst.Fields.Count
locResult(1, i) = locRst.Fields(i - 1).Name
Next i
Else
locCols = locRst.GetRows
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'''''' FIX: an empty sheet returns "F1"
'''''' http://support.microsoft.com/kb/318373
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
If locRst.Fields.Count = 1 And locRst.Fields(0).Name = "F1" And UBound(locCols, 2) = 0 And locCols(0, 0) = "" Then Exit Function 'Empty sheet
ReDim locResult(1 To UBound(locCols, 2) + 2, 1 To UBound(locCols, 1) + 1) As Variant
If locRst.Fields.Count <> UBound(locCols, 1) + 1 Then Exit Function 'Not supposed to happen
For j = 1 To UBound(locResult, 2)
locResult(1, j) = locRst.Fields(j - 1).Name
Next j
For i = 2 To UBound(locResult, 1)
For j = 1 To UBound(locResult, 2)
locResult(i, j) = locCols(j - 1, i - 2)
Next j
Next i
End If
locRst.Close
locConnection.Close
Set locRst = Nothing
Set locConnection = Nothing
getDataFromClosedExcelFile = locResult
Exit Function
error_handler:
'Wrong file name, sheet name, or other errors...
'Errors (#N/A, etc) on the sheet should be replaced by Null but should not raise an error
If locRst.State = ADODB.adStateOpen Then locRst.Close
If locConnection.State = ADODB.adStateOpen Then locConnection.Close
Set locRst = Nothing
Set locConnection = Nothing
End Function
Public Function isArrayEmpty(parArray As Variant) As Boolean
'Returns false if not an array or dynamic array that has not been initialised (ReDim) or has been erased (Erase)
If IsArray(parArray) = False Then isArrayEmpty = True
On Error Resume Next
If UBound(parArray) < LBound(parArray) Then isArrayEmpty = True: Exit Function Else: isArrayEmpty = False
End Function
Sample use:
Sub test()
Dim data As Variant
data = getDataFromClosedExcelFile("myFile.xls", "Sheet1")
If Not isArrayEmpty(data) Then
'Copies content on active sheet
ActiveSheet.Cells(1,1).Resize(UBound(data,1), UBound(data,2)) = data
End If
End Sub