Automatically multiply a value

Frantic

New Member
Joined
Oct 15, 2007
Messages
30
Hello

I want to be able to enter a value into a cell (within a specified range if necessary) and have it automatically multiply by a set value and overwrite the original entry with the result so making it all happen within one cell.

Is this possible?

Thanks
F
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Const WS_RANGE As String = "H1:H10"     '<== change to suit
Const SET_VALUE As Long = 23            '<== change to suit

    On Error GoTo ws_exit
    Application.EnableEvents = False

    If Not Intersect(Target, Me.Range(WS_RANGE)) Is Nothing Then
    
        With Target
            
            If IsNumeric(.Value) Then .Value = .Value * SET_VALUE
        End With
    End If

ws_exit:
    Application.EnableEvents = True
End Sub

This is worksheet event code, which means that it needs to be
placed in the appropriate worksheet code module, not a standard
code module. To do this, right-click on the sheet tab, select
the View Code option from the menu, and paste the code in.
 
Upvote 0
Thank you, this is really helpful. I've just got a bit stuck because the value I want to multiply by is 0.163 but when I put this in the code it just returns 0. The code works fine when I put in the value you gave me (23). Do I need to something different for a value that's less than 1?
 
Upvote 0
Are you sure your cell formatting is set to display decimal places?
 
Upvote 0
Actually, they weren't formatted for decimal places but it still isn't working! What even value I enter it now just shows 0.00. Any other ideas? :)

Update: if I enter a value with a decimal into the code it seems to round it up/down. It doesn't seem to cope with decimal values. Is there a fix for this?
 
Last edited:
Upvote 0
With your Const SET_VALUE As Long = 0.163

Provide a couple examples of What values you are entering, please.
 
Upvote 0
I've just been putting any old number at random, it doesn't make any difference what number I put in it still enters 0.00 when it's multiplying it by 0.163. It's rounding it down to 0, hence why the answer is 0.00.

If I change 0.163 to, for example 1.9, it's multiplys everything by 2 (not 1.9)
 
Upvote 0
Thank you, this is really helpful. I've just got a bit stuck because the value I want to multiply by is 0.163 but when I put this in the code it just returns 0. The code works fine when I put in the value you gave me (23). Do I need to something different for a value that's less than 1?

Just change the data type of that constant to Double not Long.
 
Upvote 0

Forum statistics

Threads
1,215,101
Messages
6,123,095
Members
449,095
Latest member
gwguy

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