Please Note: This article is written for users of the following Microsoft Excel versions: 2010. If you are using an earlier version (Excel 2003 or earlier), this tip may not work for you. For a version of this tip written specifically for earlier versions of Excel, click here: Adjusting Values with Formulas.

Adjusting Values with Formulas

by Allen Wyatt
(last updated January 4, 2014)

8

There are times that you need to adjust the values stored in the cells of a worksheet. Most times, the tools provided by Paste Special will fit the bill just fine. For instance, you can use Paste Special to multiply or divide the values in a range of cells, as described in other issues of ExcelTips.

There is a drawback to using Paste Special, however—it changes the actual value, which you might not want to happen. Why? Because four months after making the adjustment to the values, you might not remember exactly what you did, or what the starting values were.

For this reason, you may find it more desirable to replace values with formulas that indicate what was done with your adjustment. For instance, you may have the value of 100 in cell B3, and you want to increase it by 10%. Using Paste Special you can easily change it to 110, but you may instead want to replace the value with the formula =100*1.1. With such a formula, there would be no question four months from now about the starting value or what you did to it.

The only way to adjust values with formulas is to use a macro, such as the following one:

Sub Adjust()
    Dim Target As Range
    Dim J As Integer
    Dim sForm As String
    Dim sMod As String
    
    Set Target = ActiveSheet.Range(ActiveWindow.Selection.Address)
    sMod = InputBox("Formula to add?")
    If sMod > "" Then
        For J = 1 To Target.Cells.Count
            If Target.Cells(J).HasFormula Then
                sForm = Target.Cells(J).Formula
                sForm = "=(" & Mid(sForm, 2, 500) & ")"
                sForm = sForm & sMod
                Target.Cells(J).Formula = sForm
            Else
                sForm = "=" & Target.Cells(J).Value & sMod
                Target.Cells(J).Formula = sForm
            End If
        Next J
    End If
End Sub

To use this macro, select the cells you want to adjust, and then run it. You are asked for a formula to add to the cells. As an example, if you wanted to multiply the cells by 1.1, you would enter *1.1 (the asterisk multiplication symbol, followed by 1.1). The macro then steps through each selected cell and makes the adjustments. If the cell contains a formula, then the formula is adjusted as you specified. If the cell contains anything else, then it is turned into a formula that includes your adjustment.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (9486) applies to Microsoft Excel 2010. You can find a version of this tip for the older menu interface of Excel here: Adjusting Values with Formulas.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He  is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Finding the Smallest Even Value

When processing data in a worksheet, you may have a need to know what the smallest (lowest) even value in a range is. You may ...

Discover More

Jumping to the Start or End of a Document

When creating macros, it is often necessary to move the insertion point around the document so that text can be processed in ...

Discover More

Converting Charts to GIF Files

You spent a lot of time getting your chart to look just the way you wanted. Now you want to create a graphic file from that ...

Discover More

Comprehensive VBA Guide Visual Basic for Applications (VBA) is the language used for writing macros in all Office programs. This complete guide shows both professionals and novices how to master VBA in order to customize the entire Office suite for their needs. Check out Mastering VBA for Office 2010 today!

More ExcelTips (ribbon)

Develop Macros in Their Own Workbook

If you develop macros and edit them quite a bit, you may be running the risk of causing problems with the macros or with your ...

Discover More

Converting Numbers Into Words

Write out a check and you need to include the digits for the amount of the check and the value of the check written out in ...

Discover More

Digital Signatures for Macros

The security features built into Excel allow you to digitally sign your macros so that users can rest assured that they ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is four less than 9?

2015-10-01 08:09:57

Barry

@Jason,

You don't say if the cells you want adjusting contain just a number or have a formula.

This macro will cope with both. It will loop through all the cells that are selected and adjust the formula to add 500 to it, or if just a value create a formula to add 500 to the original value:

Sub Add500()
Dim rng As Range
For Each rng In Selection
With rng
If .HasFormula Then
.Formula = .Formula & "+500"
Else
.Formula = "=" & .Value & "+500"
End If
End With
Next rng
End Sub

To make it more generic you could add code to prompt for either the value you want added, a Named cell/formula/value to be added, or a cell reference itself (I'd recommend using absolute addressing in this case e.g. $A$1).

Sub AddAny()
Dim rng As Range
Dim tmp as String

tmp=Inputbox("Enter Value or Reference to be Added","AddAny Macro")
If tmp="" Then Exit Sub

For Each rng In Selection
With rng
If .HasFormula Then
.Formula = .Formula & "+" & tmp
Else
.Formula = "=" & .Value & "+" & tmp
End If
End With
Next rng
End Sub


2015-10-01 07:46:05

Barry

@Tim,

If you exclude cells which begin with "IF" this will catch all other cells including those that do not begin with "SUM"; so better to be exclicit and only catch those cell which explicitly begin with "SUM" if that is what you want.

This onlu catches those cells that begin with "SUM":
Sub CopyFormula()
Dim rCl As Range
For Each rCl In Range("C1:C17")
With rCl
If .HasFormula And Left(.Formula, 4) ="=SUM" Then
.copy .Offset(, 1).Resize(, 7)
End If
End With
Next rCl
End Sub

This will catch all cells that do not begin with "IF":
Sub CopyFormula()
Dim rCl As Range
For Each rCl In Range("C1:C17")
With rCl
If .HasFormula And Left(.Formula, 3) <>"=IF" Then
.copy .Offset(, 1).Resize(, 7)
End If
End With
Next rCl
End Sub


2015-09-30 13:11:50

Jason

Some great comments here. I need a Formula or Macro to Add 500 to a cell which is used as part of a calculation to arrive at a final number.

For example I quote homes and the builder needs to make X amount I need to adjust the list price that is used to calculate taxes and commission etc and will give the final profit number. I am at loss.

Thanks


2015-08-25 12:11:14

Tim

Dear all,

I am not used in creating macros but found following which I need to have modified a bit.

Option Explicit

Sub CopyFormula()
Dim rCl As Range
For Each rCl In Range("C1:C17")
With rCl
If .HasFormula Then .copy .Offset(, 1).Resize(, 7)
End With
Next rCl
End Sub


Is it possible to exclude formulas begin with "If". I just want to copy those ones starting with "Sum".

Many thanks for your help
Regards
Tim


2014-01-11 11:18:03

Willy Vanhaelen

I agree with Bryan, the macro does what is promised in the tip but I am stunned to see how the looping through the selected cells is done in such a complex way, while there is a very simple alternative in VB which produces almost self explanatory code: For Each … Next which I use in the following simple alternative:

Sub Adjust()
Dim cell As Range, sMod As String
sMod = InputBox("Formula to add?")
If sMod = "" Then Exit Sub
For Each cell In Selection
If cell.HasFormula Then
cell.Formula = "=(" & Mid(cell.Formula, 2) & ")" & sMod
Else
cell.Formula = "=" & cell.Value & sMod
End If
Next cell
End Sub


2014-01-07 14:13:02

Bryan

I don't get any errors when using the macro, and I can't see any reason why you would. It's a bit clunkily written, but will transform =SUM(A1:A3) to =(SUM(A1:A3))*1.1, just as promised on the tin.


2014-01-06 14:55:31

Glenn Case

Actually, there is another way to add an adjustable "fudge factor" term to a range of numbers or formulae. It also uses Paste Special, but results in the adjustment you're after:

If I have a range A2:A100 which all contain discrete values or formulae, and I want to increase the entire range by 10% but retain the record of the adjustment as noted, I can place 1.1 in cell C1, and "=$C$1" (without the quotes) into cell C2. Note, you need to have the absolute reference ($s) for this to work. I then copy cell C2 (Ctrl-C) and select the range A2:A100, and use Paste/Special/Multiply to paste. The result is that every cell in the range is now multiplied by $C$2, so if cell A2 contained 100 initially, after the operation, it will contain "=(100 * ($C$2))", which will result in 110 displayed in the cell. If the cell contained a formula, say "= A1*pi()", it will contain "=(A1*pi())*($C$2)" after the paste operation.

This quick trick makes is very easy to add an adjustment factor to a range.


2014-01-05 08:26:55

Peter Moran

Hi Allen,

Your macro needs to exclude formulas which begin with "=SUM", "=(SUM(", "=SUBTOTAL(" and =(SUBTOTAL("
as they will create errors if the designated Adjustment is applied to these formulas.


This Site

Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.