Calculation takes too much time in VBA

mehidy1437

Active Member
Joined
Nov 15, 2019
Messages
348
Office Version
  1. 365
  2. 2016
  3. 2013
Platform
  1. Windows
  2. Mobile
  3. Web
Hi There,

Please see below the code, it's taking too much time to calculate the cell's value.
I'm trying to reduce 10% of the selected cell value & show the result in the same cells for the range.
It's working fine, but taking more time to complete the task.

For general formatted cells, it takes a bit less time than the currency formatted cells.
For 2000 cells, it takes more than 10 minutes.

How can I speed this code?
Please help.

VBA Code:
Dim rng As Range
Dim myVal As Range, percentage As Double

percentage = Application.InputBox("Eneter the Percentage Value Ex:10 for 10%", "Percentage", 10, , , , , 2)


    Set rng = Selection
    For Each myVal In rng

     If myVal.Value > 0 Then
          

    myVal = myVal.Value - ((myVal.Value * percentage) / 100)
    myVal = Application.Round(myVal, 2)

    End If

    Next myVal
 
the initial code = 2,000 times reading of one cell at a time slows down the process.
minimize the number of interactions with your sheet !
Read the whole range in 1 time to an array, process the array in memory and write that array to the sheet again = 2 interactions !

VBA Code:
Set rng = Selection
    For Each myVal In rng

i'm not sure how to look at my macro in #9, 2nd part.
That's an evaluate within that range.
Is that reading and writing simultaniously, so 1 interaction ?
 
Upvote 0

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
Another point, i'm not sure about.
OP uses 3 times myVal.value, isn't that 6,000 reads ?
if it was directly assigned to a variable myvalue, would that have made a big difference ?
"the taste of the pudding is in the eating"
 
Upvote 0
the initial code = 2,000 times reading of one cell at a time slows down the process.
Of course, that's true - but 10 minutes? That is ridiculous. It took about 3 seconds on my machine, the exact same code.
 
Upvote 0
Try the following whitout loop.

It works for multiple columns. For 8 thousand cells the result is immediate.

VBA Code:
Sub test2()
  Dim percent As Double
  percent = 1 - Application.InputBox("Enter the Percentage Value Ex:10 for 10%", "Percentage", 10, , , , , 2) / 100
  With Selection
    .Value = Evaluate("=IF({1},ROUND(" & .Address & "*" & percent & ",2))")
  End With
End Sub
 
Upvote 0
#9 = 1.5 sec for 1,000,000 cells
#14 = immediate for 8,000 cells
means immediate = 0.012 sec (y)

i don't understand the construction if({1}, ... in "=IF({1},ROUND("
 
Upvote 0
How about:
VBA Code:
Option Explicit
Sub test()
Dim arr(), i&, j&, percent As Double
ReDim arr(1 To Selection.Rows.Count, 1 To Selection.Columns.Count)
percent = 1- Application.InputBox("Enter the Percentage Value Ex:10 for 10%", "Percentage", 10, , , , , 2) / 100
    For i = 1 To Selection.Rows.Count
        For j = 1 To Selection.Columns.Count
            If IsNumeric(Selection.Cells(i, j)) Then
                arr(i, j) = round(Selection.Cells(i, j).Value * percent, 2)
            Else
                arr(i, j) = Selection.Cells(i, j).Value
            End If
        Next
    Next
        Selection.Value = arr
End Sub

How can I add a condition to run this code on visible cells only ?
 
Upvote 0

Forum statistics

Threads
1,214,819
Messages
6,121,729
Members
449,049
Latest member
MiguekHeka

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