08 November 2010

(MS Excel) Adjusting Values With Formulas

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.

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.

---

"Manage the OUTCOME of your INCOME"

No comments: