Automatically multiply abn in input in a cell with another set value

Feli

New Member
Joined
Nov 17, 2021
Messages
2
Office Version
  1. 365
Platform
  1. Windows
Hi!

I am in the process of creating a model for calculation CO2 emission by a company, on a yearly basis. So what i am trying to do is automatically mulitply the inputed value in a cell with a set emissionfactor, and then in the same cell get the output with the new "scaled value" value. I would like this multiplication to be set for the whole row as this mulitplication should be able to be used for future yearly inputs...I have tried to show what i mean in the table below, where the red coloured text would be numbers that will vary year to year, but the emission factor would be the constant.... So my goal is to let the user only have to but in the collected data (like 500 000 KWh of windpower electricity) and the output in 2018 should be 500 000 * 0,005? Makes sence?

Thanks in advance for help!


SourceunitEM (emission factor)2018201920202021
Windpower electricityKWh0,005EM(windpower)* used electricity KWh 2018EM(windpower)* used electricityKWh 2019EM(windpower)* used electricity KWh 2020EM(windpower)* used electricity KWh 2021
Constructionm^24
EM(construction)* m^2 construction 2018
EM(construction)* m^2 construction 2019
EM(construction)* m^2 construction 2020
EM(construction)* m^2 construction 2021
Petrolliter0,028EM(petrol)* litter used petrol 2018
EM(petrol)* liter used petrol 2019EM(petrol)* liter used petrol 2020EM(petrol)* liter used petrol 2021
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
Assuming that the 2018 windpower is cell D2.
  • The cell D2 is empty (or contains a prompt such as: "Enter EM(Windpower)")
  • The user enters 500000 into D2
  • Excel changes this to 500000 * C2

Is that correct?
 
Upvote 0
Here is the code. To make it work you need to place the code in the VBA module for the sheet.
Right click on the tab of the sheet and select "View Code"

Then copy paste the following code into the module that opens.
Then read through the comments to understand what is happening. YOU HAVE TO CHECK the line where the comments start with <<<<<<<<<

Now save the file and test it.

VBA Code:
Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)
    'multiply emission factor with entered number
    Dim vEF As Variant   'to store emission factor
   
    If Target.Cells.Count = 1 Then  ' Don't react to changes made by paste in several cells
   
        With Range("A1")    '<<<<<<<<<< assuming this is top left cell of table. Else adjust <<<<<<<<<<<<<<<<<<<
            If Not Intersect(Target, .Offset(1, 3).Resize(.CurrentRegion.Rows.Count - 1, .CurrentRegion.Columns.Count - 3)) Is Nothing Then
                ' the changed cell (target) is within the years columns
                vEF = .Offset(Target.Row - 1, 2)        'store the emission factor for the row
                If IsNumeric(vEF) And vEF <> 0 Then     'emission factor is a number and not 0
                    Application.EnableEvents = False    'stop excel reacting to changes (events), else will get into endless loop
                    Target = Target * vEF
                    Application.EnableEvents = True     'enable the events again
                End If
            End If
        End With
    End If
End Sub

The code module for the sheet is a special module where events happening on the sheet can be intercepted and dealt with. For instance if a user clicks on a cell, or enters a value. In the above code we use this last even: Worksheet_Change().
Excel passes the changed cell(s) to the event sub. It is called 'Target'.
Now we can do things with this cell. First we check if it is within the area where you want to modify the behaviour. If so then we multiply it with the emission factor on the same row.
But if we where to modify it, Excel would call this sub again with the new value. It would start an endless loop and Excel would hang. So we tell VBA to momentarily switch off the event handler while we enter the new value into the cell.
 
Upvote 0

Forum statistics

Threads
1,214,646
Messages
6,120,718
Members
448,986
Latest member
andreguerra

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