提问人:Cealizar Serrano 提问时间:10/10/2023 最后编辑:Cealizar Serrano 更新时间:10/11/2023 访问量:57
如何构建引用另一个单元格作为公式但可以被手动输入覆盖的代码
How can I build a code that references another cell as a formula but can be overridden by manual inputs
问:
因此,我正在尝试构建代码,其中 AE 中的单元格可以通过手动输入进行覆盖,但会根据给定条件引用不同的范围。我已经在几个小时了,但我似乎想不通。任何帮助都是值得赞赏的。
我得到的错误是: 公式对象失败的方法范围
突出显示此代码行
** .范围(“AE”和 i)。公式 = BMformula & i **
Public Sub BedoverrideND(Bathmat As Worksheet, DSMaster As Worksheet, DSMformula As String, BMformula As String)
Dim i As Long
For i = 7 To getcostcoderange
Select Case i
Case 21, 22, 23, 24, 25
' Do nothing
Case Else
If Bathmat.Range("B" & i).Value = 0 Or Bathmat.Range("B" & i).Value = "" Or _
Not WorksheetFunction.IsFormula(Bathmat.Range("AE" & i)) _
And Not IsEmpty(Bathmat.Range("AE" & i)) Then
Else
With Bathmat
If Not WorksheetFunction.IsFormula(.Range("E" & i)) And Not IsEmpty(.Range("E" & i)) Then
.Range("AE" & i).Formula = BMformula & i
ElseIf DSMaster.Range("T" & i).Value + DSMaster.Range("U" & i).Value <> 0 Then
.Range("AE" & i).Formula = DSMformula & i
Else
.Range("AE" & i).Formula = BMformula & i
End If
End With
End If
End Select
Next i
End Sub
这些是触发潜艇的代码:
Public Sub BedOverrideSun()
BedoverrideND shBathMatSun, shDSMasterSun, "='DS Master Sun'!V", "='Bath Mat Sun'!E"
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("E:E, AE:AE, BE:BE")) Is Nothing Then
Call BedOverrideSun
我尝试了一下,并设法找到了我正在寻找的解决方案:
With Bathmat
If Not WorksheetFunction.IsFormula(.Range("E" & i)) And Not IsEmpty(.Range("E" & i)) Or _
DSMaster.Range("T" & i).Value + DSMaster.Range("U" & i).Value = 0 Then
If .Range("AE" & i).Formula <> BMFormula & i Then
.Range("AE" & i).Formula = BMFormula & i
End If
ElseIf DSMaster.Range("T" & i).Value + DSMaster.Range("U" & i).Value <> 0 Then
If .Range("AE" & i).Formula <> DSMaster & i Then
.Range("AE" & i).Formula = DSMaster & i
End If
End If
End With
答:
0赞
Cealizar Serrano
10/11/2023
#1
将代码的 with 部分更改为 this,它按预期工作。
With Bathmat
If Not WorksheetFunction.IsFormula(.Range("E" & i)) And Not IsEmpty(.Range("E" & i)) Or _
DSMaster.Range("T" & i).Value + DSMaster.Range("U" & i).Value = 0 Then
If .Range("AE" & i).Formula <> BMFormula & i Then
.Range("AE" & i).Formula = BMFormula & i
End If
ElseIf DSMaster.Range("T" & i).Value + DSMaster.Range("U" & i).Value <> 0 Then
If .Range("AE" & i).Formula <> DSMFormula & i Then
.Range("AE" & i).Formula = DSMFormula & i
End If
End If
End With
评论
shBathMatSun
Bathmat.Range("B" & i).Value