从单元格引用中减去

Subtract from cell reference

我只需要一种方法从单元格引用中删除两个单元格(即将 =Y36 更改为 =Y34)这需要在列末尾和我最接近它的两个单元格上完成是

Sub Update_Reference()

     Dim myCell  As Range

    Range("A" & Rows.Count).End(xlUp).Offset(-2).Resize(2).Select
    For Each myCell In Selection
        If myCell.HasFormula Then myCell.Formula = Left(myCell.Formula, _
                                                Len(myCell.Formula) - 1) & 4
    Next myCell
 
End Sub

但这只是看起来有效;它似乎所做的只是将相同的公式复制到每个额外的单元格

试试这个:

Sub Update_Reference()

     Dim myCell  As Range, s As String, i As Long, N As Long
     Dim FirstPart As String

    Range("A" & Rows.Count).End(xlUp).Offset(-2).Resize(2).Select
    
    For Each myCell In Selection
        If myCell.HasFormula Then
            s = myCell.Formula
            For i = Len(s) To 1 Step -1
                If IsNumeric(Mid(s, i, 1)) Then
                '
                Else
                    Exit For
                End If
            Next i
            
            FirstPart = Mid(s, 1, i)
            N = Mid(s, i + 1) - 2
            myCell.Formula = FirstPart & N
        End If
    Next myCell
 
End Sub

备注:

代码将公式分成两部分;公式末尾的数字部分 (N) 和其他所有内容 (FirstPart) 。它减去 2 从数字部分将公式放回原处。