gpt4 book ai didi

excel - 从单元格引用中减去

转载 作者:行者123 更新时间:2023-12-04 22:19:38 25 4
gpt4 key购买 nike

我只需要一种从单元格引用中删除两个的方法(即,将 =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 并将公式重新组合在一起。

关于excel - 从单元格引用中减去,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65289354/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com