Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I have a column with a formula/function that has other cells as parameters. However in the column, every few rows I have a subtotal and blanks, for readability. Now I decided to change the formula/function but I can not do copy+paste on the whole column because of the subtotals/blank cells, and I can not do change+replace because there are parameters in the functions that uses other cells and thus it is different in each cell in the column.

For example, if the formula is =if(A5>24,1,"") and I want to change it to =AA_userfn(A5,B5)

Any idea how to change it easily? currently I copy+paste few rows at a time, but that is tedious.

Maybe a Sub that will go all over the column (the column number as an input parameter) and looks for =if(, and then replaces it with =aa_userfn(Ax, By) where Ax and By are calculated based on the position of the cell currently changed in reference to the previous values in that cell.

Any ideas will be welcomed

share|improve this question
add comment

1 Answer

Change to suit your needs

Public Sub UpdateFormula()
Dim r As Long, c As Long, n As Long, x As Long, a As Long
Dim lRow As Long
Dim rngSlct As Range
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual

'Select the column you want to update. This can be multiple columns
Set rngSlct = Selection
With rngSlct
    x = .Columns.count
'Loop through all the selected columns
For n = 1 To x
    a = rngSlct(1, n).Column
lRow = Cells.Find(What:="*", After:=[A1], SearchDirection:=xlPrevious).row
c = a

With ActiveCell
    For r = ActiveCell.row To lRow
        If Left(Cells(r, c).Formula= "=If(" Then
            Cells(r, c).Formula = 'Type your new formula here
        End If
    Next r
End With

Next n
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
End Sub
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.