Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Multiply a range of cells by a constant VBA

I tried to use the following code to multiply a range of cells by a constant, but it keeps giving me type mismatched. Can someone help please?

Sub Multiply()
 Dim rng As Range
 Dim myVal As Range
 
 Set rng = Range("B2:DR82")
 For Each myVal In rng
   myVal = Round(myVal.Value * 0.64, 2)
 Next myVal
End Sub

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Check the value before multiplying:


Sub Multiply()
 Dim rng As Range
 Dim myVal As Range
 
 Set rng = Range("B2:DR82")
 For Each myVal In rng
    If IsNumeric(myVal.Value) Then
        myVal.Value = Round(myVal.Value * 0.64, 2)
    End If
 Next myVal
End Sub

But it would be faster, writing the range to an array, doing the maths on the array and then write back to the sheet.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading