Looping very slow using Excel object

LEXCERM

Active Member
Joined
Jun 26, 2004
Messages
320
Office Version
  1. 365
Platform
  1. Windows
Hi all,

Here is a snippet of a loop code I'm currently using in an Access module:-


For Each myCell In xlApp.Range("A2:A" & xlApp.Cells(xlApp.Rows.Count, "C").End(xlUp).Row)

For ChkDate = 5 To xlApp.Cells(myCell.Row, xlApp.Columns.Count).End(xlToLeft).Offset(0, -2).Column
AA = myCell.Value
BB = myCell.Offset(0, 1).Value
CC = myCell.Offset(0, 2).Value
DD = myCell.Offset(0, 3).Value

xlApp.Sheets("Result").Cells(xlApp.Rows.Count, "A").End(xlUp).Offset(1, 0) = AA
xlApp.Sheets("Result").Cells(xlApp.Rows.Count, "A").End(xlUp).Offset(0, 1) = BB
xlApp.Sheets("Result").Cells(xlApp.Rows.Count, "A").End(xlUp).Offset(0, 2) = CC
xlApp.Sheets("Result").Cells(xlApp.Rows.Count, "A").End(xlUp).Offset(0, 3) = DD

Next ChkDate
Next myCell


If I run this code in Excel, it takes approx 10 seconds to complete 20,000 records. However, running this in Access seems to take forever.

Are there any tips I could use? Am I over stating the xlApp references?

Kind regards. :)
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
Why are you trying to run VBA code on Excel files from Access?
I prefer to avoid this whenever possible.
Since, as you pointed out, it runs much faster in Excel, why not just have an Excel macro, and call/run the Excel macro from Access (instead of trying to put the VBA code in Access)?
 
Upvote 0
Thanks for the reply Joe4. I did exactly that in the end. Kept the code in the Excel file and fired it through Access, then imported the data into Access. Cheers.
 
Upvote 0

Forum statistics

Threads
1,221,852
Messages
6,162,431
Members
451,765
Latest member
craigvan888

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