1

My VBA code loops through all subfolders and all files to collect 7 pieces of information from each file, and writes these pieces of information into an Excel spreadsheet.

The number of file attributes is fixed at 7, however I have an unknown number of files inside an unknown number of folders. After running it a few times, I know that I have nearly 10,000 rows in my spreadsheet, and each time I run my macro it takes a very long time. Performance is an issue as I am doing nearly 10,000 write operations and it writes the array to the file each time.

Instead, I would like to create a 2-D array that can add a new row to this 2-D array for each new file it comes across. Then, once it has iterated through all the files, it uses just one write operation to write the entire 2-D array to the spreadsheet.

I am well aware of VBA ReDim Preserve. However, this would require me to still call ReDim Preserve nearly 10,000 times which is also an expensive operation. I also need to iterate through each file individually to do some processing on the data belonging to it.

And so I was wondering if there it is possible for VBA 2-D arrays to work something like

dim file_data(, 7) as String 

I have read this question here, but clearly this can't work as the highest voted answer still prescribed printing for each file.

8
  • Can you define the array off the row count, and then use the row number as the point to insert, so 5 rows a is defined as a(rows.count)=a(5) and then add, row 1 a(1) or a(0) Commented May 16, 2022 at 16:06
  • 2
    One option would be to programmatically determine the number of files first, before reading them. Commented May 16, 2022 at 16:08
  • 1
    Collect all of the file paths before you start processing them, so you know exactly what size array you need. Alternatively, you can resize your output array in "chunks", or just start with an array which is larger than you're going to need (eg. 20k/30k rows)... Commented May 16, 2022 at 16:11
  • @Nathan_sav the thing is that this folder will grow and shrink over time, therefore it's not constant and I can't hardcode it. Commented May 16, 2022 at 16:17
  • 1
    Sheet1.Range("A1").Resize(actualNumberOfLines, 7).value = file_data (for example) would only write the first actualnumberOfLines rows from your array to the sheet Commented May 16, 2022 at 16:25

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.