multiply multiple cells by a single cell but retain formula

sheetspread

Well-known Member
Joined
Sep 19, 2005
Messages
5,160
Lets say I want to highlight any cell or cells and multiply them all by the value in cell I7. Paste special multiply will leave me with the correct number but not the formula.

I7= 1.05

Say A4 = 100

Showing 105 is good but in the cell formula I'd like =100*1.05 to appear

Is there a VBA code or other technique that works?
 
Hello,

I am trying to multiply multiple tables with a factor. I want to be able change the factor so I like to keep the formula.

I tried the following:

Sub insertmultiplier()
For Each cell In Selection
cell.Formula = "=B2*(" & Mid(cell.Formula, 2, 255) & ")"
Next cell
End Sub

But some how it changes the values in the cells instead of multiplying the values with B2.

i.e.:

B2 = 2

Values after macro0000
values before marco1000200030004000

<colgroup><col><col span="4"></colgroup><tbody>
</tbody>

I hope someone can help.
 
Upvote 0

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
try this, which will cope with values and existing formulas:

Code:
Sub insertmultiplier()
    Dim cCell As Range
    For Each cCell In Selection
        If Left(cCell.Formula, 1) = "=" Then
            cCell.Formula = "=B2*" & Right(cCell.Formula, Len(cCell.Formula) - 1)
        Else
            cCell.Formula = "=B2*" & cCell.Formula
        End If
    Next cCell
End Sub

HTH
 
Last edited:
Upvote 0
Hi Oshiren,

Firstly thanks for the feedback and delighted that it worked.

With regard to your last comment, I've seen far worse starting points for the code. A couple of specific points:

1. Dim cCell As Range it is simply good practice to formally declare variables. Better still is to set Option Explicit in the VBA preferences as this will force you to do so, which helps to avoid errors caused through inventing variables 'on-the-fly.'
2. Changed your cell variable to cCell since its not good practice to use variables with names of functions or objects (in this case I don't think cell is protected, but just seems to be a good idea).
3. This code "=B2*(" & Mid(cell.Formula, 2, 255) & ")" changed to "=B2*" & cCell.Formula" since without an '=' sign your code actually stripped off the initial digit (which is why you returned a 0 as the answers). The IF statement simply tests the left hand character of the cCell.Formula and if it is '=' removes it. Thinking about this now I think you could easily replace my Right(cCell.Formula, Len(cCell.Formula) - 1) with your Mid(cell.Formula, 2, 255).

Hope this helps.
 
Upvote 0
your code actually stripped off the initial digit (which is why you returned a 0 as the answers)


Thinking about this now I think you could easily replace my Right(cCell.Formula, Len(cCell.Formula) - 1) with your Mid(cell.Formula, 2, 255).


Yes, the numbers 1000,2000,3000,4000 without the first digit become zero, which multiplied times anything equal zero. Peter's IF statement corrects for that (my previous one assumes an equal sign is already present), you can also write:

Code:
Sub insertmultiplier()
For Each ccell In Selection
ccell.Formula = "=B2*(" & Replace(ccell.Formula, "=", "") & ")"
Next ccell
End Sub
 
Last edited:
Upvote 0
Sorry Sheetspread, I hadn't realised that you were the original poster of the macro - I'd assumed it was linked to the original thread, but not really linked to it!!.

Just a thought, but I'd always indent my macros - it makes them so much easier to read, helps avoid missing endif type errors and significantly improves debugging.

All the best.
 
Upvote 0
I hadn't realised that you were the original poster of the macro

Yes, I had asked for what oshiren also wanted: cell ref * value, then years later someone asked for cell ref * formula, so I just wrote the first code that came to mind.

I'd always indent my macros - it makes them so much easier to read, helps avoid missing endif type errors and significantly improves debugging.

you're right!

Till next time.........
 
Upvote 0

Forum statistics

Threads
1,215,537
Messages
6,125,398
Members
449,222
Latest member
taner zz

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