Hello friends, I am trying to create a code that is as efficient as possible.
What I need to do is Multiply a range of cells by a fixed value and then replace the value that each cell had with that new value.
For example:
In the "D5: 25" range I have different numerical values in each of the cells. Then I multiply each cell by a fixed value and that new value is replaced in each cell (I mean in the range "D5: 25").
I was able to solve it through two ways, but both are "inefficient", since when I run the code it shows that it takes a long time to perform all the multiplication in that range.
And also as I then need to do the same in other ranges too, I know that this code will not be efficient because it will take a long time.
I pass the two codes that do work but are inefficient.
Code 1:
Sub dolartopesos()
'Routine to pass to pesos all the prices of the products of each supplier that are in dollars.
dollars = Worksheets(3).Range("R1")
'---------------------------------------------------
With Worksheets(3)
'Currency exchange
For i = 5 To 25
.Cells(i, 4) = .Cells(i, 4) * dollars
Next i
End With
'---------------------------------------------------
End Sub
Code2:
Sub dolartopesos()
'Routine to pass to pesos all the prices of the products of each supplier that are in dollars.
dollars = Worksheets(3).Range("R1")
'---------------------------------------------------
''Currency exchange
Dim rng As Range: Set rng = Worksheets(3).Range("D5:25")
Dim cel As Range
For Each cel In rng.Cells
With cel
cel = Application.WorksheetFunction.Product(cel, dollars)
End With
Next cel
'---------------------------------------------------
End Sub
Both codes take too long to multiply in that range.
What could I do to speed up the multiplication?
Any contribution will be welcome. From already thank you very much.
Sebastian.
What I need to do is Multiply a range of cells by a fixed value and then replace the value that each cell had with that new value.
For example:
In the "D5: 25" range I have different numerical values in each of the cells. Then I multiply each cell by a fixed value and that new value is replaced in each cell (I mean in the range "D5: 25").
I was able to solve it through two ways, but both are "inefficient", since when I run the code it shows that it takes a long time to perform all the multiplication in that range.
And also as I then need to do the same in other ranges too, I know that this code will not be efficient because it will take a long time.
I pass the two codes that do work but are inefficient.
Code 1:
Sub dolartopesos()
'Routine to pass to pesos all the prices of the products of each supplier that are in dollars.
dollars = Worksheets(3).Range("R1")
'---------------------------------------------------
With Worksheets(3)
'Currency exchange
For i = 5 To 25
.Cells(i, 4) = .Cells(i, 4) * dollars
Next i
End With
'---------------------------------------------------
End Sub
Code2:
Sub dolartopesos()
'Routine to pass to pesos all the prices of the products of each supplier that are in dollars.
dollars = Worksheets(3).Range("R1")
'---------------------------------------------------
''Currency exchange
Dim rng As Range: Set rng = Worksheets(3).Range("D5:25")
Dim cel As Range
For Each cel In rng.Cells
With cel
cel = Application.WorksheetFunction.Product(cel, dollars)
End With
Next cel
'---------------------------------------------------
End Sub
Both codes take too long to multiply in that range.
What could I do to speed up the multiplication?
Any contribution will be welcome. From already thank you very much.
Sebastian.