I'm having an issue with a memory leak being caused by a particular file interacting with this simple block of code. The macro simply opens all .xlsx files in a particular directly, copies the sheets into my active workbook, then closes them. I've run this on a number of different .xlsx as well as .csv files with no issues whatsoever. However, when I run it on the pertinent data that needs to be processed, it results in my excel memory usage spiking to 4GB and then steadily climbing (clearly a leak of some sort).
Option Explicit
Sub Combine_Zoho_Gusto()
Dim Path As String
Dim Filename As String
Dim Sheet As Worksheet
Path = "C:\Users\XXXX\Desktop\Payroll Analysis" & "\"
Filename = Dir(Path & "*.xlsx")
Do While Filename <> ""
Workbooks.Open Filename:=Path & Filename, ReadOnly:=True
For Each Sheet In ActiveWorkbook.Sheets
Sheet.Copy After:=ThisWorkbook.Sheets(1)
Next Sheet
Workbooks(Filename).Close
Filename = Dir()
Loop
End Sub
The .xlsx file I'm trying to get this to run on is one that is using Power Query, which points to a folder which contains a simple .csv file. The Power Query simply filters and organizes this data into two columns of data with headers which it then displays on its sole Sheet1.
Has anyone encountered an issue like this, attempting to import sheets from a Power Query'd file, causing memory issues? I'm Quite new to VBA, so I'm not sure how else to diagnose this. The particular command that causes the memory to spike is:
Sheet.Copy After:=ThisWorkbook.Sheets(1)
Any and all help would be appreciated, thanks!
Update: I've been doing a bit more exploring of my data in the power query and I've noticed I even encounter this issue when trying to manually copy-paste the data from the power query to another excel file. If I mouse over Values it has no issue displaying the preview of the raw data, but as soon as I mouse over any of the other functions, it immediately spikes my memory to nearly 8GB usage.
[Copy-Paste Dialoge][1] [1]: https://i.stack.imgur.com/mjZLX.png