Multiply selection of cells by x, whilst keeping formulas and formats, using VBA

jaykay1

New Member
Joined
Nov 4, 2014
Messages
7
Hi,

I constantly have to change the factor of a large series of random cells, usually by a thousand or a hundred.

I can for example
  • enter "100" in a random cell,
  • click copy,
  • highlight all the cells I need to change,
  • paste special - formulas - multiply.
  • But, when doing this as frequently as I do, it takes a while!

Does anyone know how I can do this in VBA, so that I can just highlight the cells, run the macro and it will automatically paste special, formulas, multiply by say "100" for every cell?

Any help would be greatly appreciated!
Jay
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
Assuming all the cells you select have numeric constants (not formulas) in them:
Code:
Sub Multiply()
Const multiplier As Double = 100
Dim c As Range
For Each c In Selection
    c.Value = multiplier * c.Value
Next c
End Sub
 
Upvote 0
I believe this is what you are looking for.

A slightly modified macro from using the Macro Recorder.

Code:
Sub MultiplyRange()
    'Change C1 in the line below to whichever cell has the factor you would like to multiply by
    Range("C1").Copy
    Selection.PasteSpecial Paste:=xlPasteFormulas, Operation:=xlMultiply, _
        SkipBlanks:=False, Transpose:=False
End Sub
 
Last edited:
Upvote 0
Change A1 to the cell that contains the multiplier value.

Code:
[COLOR=darkblue]Sub[/COLOR] PasteSpecial_Multiply()
    [COLOR=darkblue]Dim[/COLOR] rngArea [COLOR=darkblue]As[/COLOR] Range
    Range("A1").Copy
    [COLOR=darkblue]For[/COLOR] [COLOR=darkblue]Each[/COLOR] rngArea [COLOR=darkblue]In[/COLOR] Selection.Areas
        rngArea.PasteSpecial Paste:=xlPasteFormulas, Operation:=xlPasteSpecialOperationMultiply
    [COLOR=darkblue]Next[/COLOR] rngArea
    Application.CutCopyMode = [COLOR=darkblue]False[/COLOR]
[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]Sub[/COLOR]

Note: Paste Special multiply doesn't keep the formula in the destination cells.
 
Last edited:
Upvote 0
Change A1 to the cell that contains the multiplier value.

Code:
[COLOR=darkblue]Sub[/COLOR] PasteSpecial_Multiply()
    [COLOR=darkblue]Dim[/COLOR] rngArea [COLOR=darkblue]As[/COLOR] Range
    Range("A1").Copy
    [COLOR=darkblue]For[/COLOR] [COLOR=darkblue]Each[/COLOR] rngArea [COLOR=darkblue]In[/COLOR] Selection.Areas
        rngArea.PasteSpecial Paste:=xlPasteFormulas, Operation:=xlPasteSpecialOperationMultiply
    [COLOR=darkblue]Next[/COLOR] rngArea
    Application.CutCopyMode = [COLOR=darkblue]False[/COLOR]
[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]Sub[/COLOR]

Note: Paste Special multiply doesn't keep the formula in the destination cells.

I submit that you don't need to iterate through the range areas for PasteSpecial operation. Apparently, Excel handles it automatically throught he PasteSpecial method.
 
Upvote 0
I submit that you don't need to iterate through the range areas for PasteSpecial operation. Apparently, Excel handles it automatically throught he PasteSpecial method.

PasteSpecial throws an error for noncontinuous ranges (Cntrol+Click on two ranges). The For-Next loop iterates through the selection areas and PasteSpecial for each.
 
Upvote 0
Thanks very much. I should have said though, i don't want to change any of the cells in the sheet, i.e. I don't want to have the put the multiplier into A1 for instance. I just want the number it's multiplied by to be a constant - always 100 (I can then manipulate the macro for 1000 as well and create another macro). I also want to keep the formulas in the cells that I select to remain the same just with x100 at the end. Is this possible??
 
Upvote 0
JayKay, based on your response, it seems the answer JoeMo gave is going to be your best bet.



PasteSpecial throws an error for noncontinuous ranges (Cntrol+Click on two ranges). The For-Next loop iterates through the selection areas and PasteSpecial for each.

I am using XL2010 and not having that issue. (i.e. I can run my code on Range("A1:A2,A4:A5,A7:A8"), etc). Perhaps with earlier versions. Either way, your approach will definitely work.
 
Upvote 0
Thanks BiocideJ, it does work but the problem is that it makes the formulas constants. So if I had =sum(c1,b1) and then ran the macro on it, it would become a constant as opposed to =sum(c1,b1)*100. Sometime I have formulas and sometimes I have constants and would like them to remain intact. Any ideas?
 
Upvote 0
Thanks everyone but someone has helped me out with this which appears to work.

Sub MultiplySelectionBy100()
Dim Cell As Range
For Each Cell In Selection
If Len(Cell.Value) > 0 And Application.IsNumber(Cell.Value) Then
If Cell.HasFormula Then
Cell.Formula = Replace(Cell.Formula, "=", "=100*(") & ")"
Else
Cell.Value = 100 * Cell.Value
End If
End If
Next
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,240
Members
452,621
Latest member
Laura_PinksBTHFT

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