bottom
Great ExcelTips!
         
Your e-mail address is safe!
Close Note

Tips.Net > ExcelTips Home > Macros > VBA Examples > Adjusting Values with Formulas

Adjusting Values with Formulas

Summary: You can adjust values by using the Paste Special feature, but you may want to do it by applying a formula. This tip describes the rationale for such an approach, as well as a way in which it can be accomplished. (This tip works with Microsoft Excel 97, Excel 2000, Excel 2002, Excel 2003, and Excel 2007.)

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 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.

Tip #2592 applies to Microsoft Excel versions: 97 | 2000 | 2002 | 2003 | 2007


Step Up and Take Control! Subscribers to ExcelTips know just how valuable a resource it is. ExcelTips Premium provides twice the number of exceptional, easy-to-understand tips every week in an ad-free newsletter, as well as substantial discounts on ExcelTips archives and e-books.
 
Check out ExcelTips Premium today!

Helpful Links

Ask an Excel Question
Make a Comment

Tips.Net Home

ExcelTips FAQ
ExcelTips Premium

Learn Access Now

Beauty Tips
Car Tips
Cleaning Tips
College Tips
Cooking Tips
Excel2007 Tips
ExcelTips
Family Tips
Gardening Tips
Health Tips
Home Tips
Money Tips
Organizing Tips
Pest Tips
Pet Tips
Word2007 Tips
WordTips

Advertise on the
ExcelTips Site

 

Great Info!

Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your e-mail address and click "Subscribe."
     
(Your e-mail address will never be shared with anyone, ever.)