Converting For Each range to Lbound and Ubound

dbobo88

New Member
Joined
Apr 23, 2023
Messages
1
Office Version
  1. 365
Platform
  1. Windows
Hello, I have the below code (OptimumSpeed) which works well but is a bit slow when applied to 8760 rows. I was trying to change it using Lbound to Ubound but can't seem to make it work (see second code Optimum) when trying to associate it with a Do until....Loop. Appreciate if anybody could help me solve this.
Thanks in advance

VBA Code:
Sub OptimumSpeed()

Application.ScreenUpdating = False

Dim i As Long

Dim rng, rng1, rng2, rng3 As Range

Set rng = Range("A1:A8760")

Set rng1 = Range("B1") '' this cell has a formula linked to C1 value

Set rng2 = Range("C1")  '' this cell has a formula linked to D1 value

Set rng3 = Range("D1") 

i = 0

For Each cell In rng

Do Until rng1.Offset(i, 0) > rng2.Offset(i, 0)

rng3.Offset(i, 0) = rng3.Offset(i, 0) - 1

Loop

i = i + 1

Next cell

Application.ScreenUpdating = True

End Sub

VBA Code:
Sub optimum()

Dim arr(),

Dim j As Long



arr = Range("A1:A8760")



For i = LBound(arr, 1) To UBound(arr, 1)

Do Until Range("B1").Offset(j , 0) > Range("C1").Offset(j , 0)

arr(i, 1) = Range("Temp_CtSPeed1").Offset(j , 0) - 1

Loop

j = j + 1

Next i



Range("A1:A8760")= arr



End Sub
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
Try using row value and do the 3 cells in succession on the same row loop.
 
Upvote 0
Without seeing your formulas in cells B1 & C1 it's pretty difficult to offer suggestions. A sample of your sheet using the XL2BB - Excel Range to BBCode would also greatly assist (alternatively you can share your file via Dropbox, Google Drive or similar method).
 
Upvote 0

Forum statistics

Threads
1,214,954
Messages
6,122,461
Members
449,085
Latest member
ExcelError

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