Increment excel formula by 1

前端 未结 4 907
醉话见心
醉话见心 2021-01-19 15:44

I\'m trying to copy and paste a few cells keeping the format and them ebing linked to a table.

Currently i have a table but i am referencing it from another sheet e.

4条回答
  •  攒了一身酷
    2021-01-19 16:09

    Select the cells you want and run the following in VBA:

    Public Sub TestMe()
    
        Dim myCell  As Range
    
        For Each myCell In Selection
            If myCell.HasFormula Then myCell.Formula = myCell.Formula & "+1"
        Next myCell
    
    End Sub
    

    If you press it too many times, this is how to remove it the last +1:

    Public Sub UnTestMe()
    
        Dim myCell As Range
    
        For Each myCell In Selection
            If myCell.HasFormula Then myCell.Formula = Left(myCell.Formula, _
                                                    Len(myCell.Formula) - 2)
        Next myCell
    
    End Sub
    

    Concerning the comment of @SJR, if you want to change the reference address of the cell, this is one possible workaround:

    Public Sub TestMe()
    
        Dim myCell As Range
    
        For Each myCell In Selection
            If myCell.HasFormula Then myCell.Formula = Left(myCell.Formula, _
                                                    Len(myCell.Formula) - 1) & 2
        Next myCell
    
    End Sub
    

    Simply change the &2 to the number you want to refer to.

提交回复
热议问题