Slow workbook resulting in slow vba

PrashanthKumar123

New Member
Joined
May 1, 2021
Messages
38
hi All ! I have a VBA code (provided below) that allows user to upload a sheet from another workbook. This loads all sheets pretty fast, except for one category of files. The size is 300kb only with 1500 rows, 10 columns. But the vba code surprisingly takes 5 minutes+ to load this worksheet.

I suspect this is because the sheet to load has excessive formatting. This takes me almost the same time even when I double click the file and create a fresh workbook (instead of using the VBA)

Any change in my VBA code that speed this up? Ideal will be to only open a "values-only" FileToOpen and copy sheet

Sub Upload()
Dim FileToOpen As Variant
Dim OpenBook As Workbook
Dim lastRow As Integer
Dim LastColumn As Integer

Application.Calculation = xlCalculationManual
Application.ScreenUpdating = False
Application.EnableEvents = False
FileToOpen = Application.GetOpenFilename(Title:="Browse for your File & Import Range", FileFilter:="Excel Files (*.xls*),*xls")
If FileToOpen <> False Then
Set OpenBook = Application.Workbooks.Open(FileToOpen)
Set src = OpenBook.Sheets(1)
src.Copy Before:=ThisWorkbook.Sheets(1)
OpenBook.Close False
End If

Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
Application.EnableEvents = True

End Sub
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
It could be that you have some rows/columns that appear to be empty that are not, or column-lvel formatting that makes that file a lot bigger than it truly is. Open the egregious file, and do Control + End. See if it takes you to some wild row you weren't expecting.
 
Upvote 0
It could be that you have some rows/columns that appear to be empty that are not, or column-lvel formatting that makes that file a lot bigger than it truly is. Open the egregious file, and do Control + End. See if it takes you to some wild row you weren't expecting.
hi Richh, I tried ctrl + end and it takes to me F1500 cell - so this doesn't seem to be the issue!
 
Upvote 0
Another reason might be because the excel opens in "Protected View".

When I try to open the file normally (without VBA), it opens in "Protected View" by default and when I change the trust settings etc., it takes fair bit of time to load.

Anyway the VBA code can circumvent this and speed this up?
 
Upvote 0
I think it is unlikely that it will make a difference but based on your last comment you could try using this instead of your current OpenBook line:

VBA Code:
Set OpenBook = Application.Workbooks.Open(Filename:=FileToOpen, ReadOnly:=True)
 
Upvote 0
I think it is unlikely that it will make a difference but based on your last comment you could try using this instead of your current OpenBook line:

VBA Code:
Set OpenBook = Application.Workbooks.Open(Filename:=FileToOpen, ReadOnly:=True)
Hi Alex, unfortunately, it still takes the same amount of time.
 
Upvote 0
Is it worth considering importing the data using Power Query ?
hi Alex, my existing tool can only accept upload this way, so I'm constrained that.

I ran a few checks and I'm near certain that the issue is because the user upload has many merged cells. Of 1500 rows, about 50% are merged across 3 columns. I'm now checking if there is a way for VBA to import and still circumvent this issue
 
Upvote 0

Forum statistics

Threads
1,214,631
Messages
6,120,645
Members
448,974
Latest member
DumbFinanceBro

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top