Can't get a simple multiply macro working

Hyflex

New Member
Joined
Mar 28, 2011
Messages
40
Code:
Sub Test()
Range("J:S").SpecialCells(xlCellTypeConstants, xlNumbers).Select
        With Selection
            .Cells.Value = .Cells.Value * 2.5
        End With
End Sub

I want All cells with numbers in to be multiplied by 2.5 but if its a blank or zero to ignore the cells
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
How about something like this...

Code:
Sub Test()
Dim rng As Range
Dim c As Range
    Set rng = Range("J:S")
    For Each c In rng.Cells
      If isnumeric(c.value) then
        c.Value = c.Value * 2.5
      end if
    Next
End Sub
 
Last edited:
Upvote 0
Try
Code:
Sub Test()
Range("J:S").SpecialCells(xlCellTypeConstants, xlNumbers).Select
        For Each Cell In Selection
            Cell.Value = Cell.Value * 2.5
        Next Cell
End Sub
 
Upvote 0
A little update to mine...

It's may not be a good idea to do all of column J:S so using the LR variable would help narrow the scope of the cells the macro has to process.

Code:
Sub Test()
    Dim rng As Range
    Dim c As Range
    Dim LR As Long
    LR = Range("S" & Rows.Count).End(xlUp).Row
    Set rng = Range("S1:J" & LR)
    Application.ScreenUpdating = False
    For Each c In rng.Cells
        If c.Value <> "" Then
            c.Value = c.Value * 2.5
        End If
    Next
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Try
Code:
Sub Test()
Range("J:S").SpecialCells(xlCellTypeConstants, xlNumbers).Select
        For Each Cell In Selection
            Cell.Value = Cell.Value * 2.5
        Next Cell
End Sub


Both work wonderfully, thank you very very much :)

Cheers guys, this is much appreciated.

Note: jeffreybrown your second macro doesn't work :/ it says type mismatch...
 
Last edited:
Upvote 0
Glad you got it sorted.
Best option is Jeffreys.
It's not always a good idea to use Select.Selection when there are better options.
 
Upvote 0
Glad you got it sorted.
Best option is Jeffreys.
It's not always a good idea to use Select.Selection when there are better options.

I understand what he is saying about that, we have about 500rows at the moment, but they are all different data sets so it might be 10rows then 4 rows of blank space, then 5 rows of data and then again back to 4 rows of blank space.

Both of your first macros take about 1min to complete. We have some other macros what take just as long (all macros together take about 8mins to complete)
 
Upvote 0
I just ran the macro again and no error.

As far as the varying rows...no problem.

That is what the LR variable does. It looks for the last row no matter how many blank are interspersed throughout the dataset.
 
Upvote 0
Hmm, Jeffreys works fine for me in 2007
 
Upvote 0

Forum statistics

Threads
1,224,600
Messages
6,179,833
Members
452,947
Latest member
Gerry_F

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