如何构建引用另一个单元格作为公式但可以通过手动输入覆盖的代码

问题描述 投票:0回答:1

因此,我正在尝试构建代码,其中 AE 中的单元格可以通过手动输入覆盖,但根据给定条件引用不同的范围。我已经在 at 几个小时了,但我似乎无法弄清楚。如有任何帮助,我们将不胜感激。

我得到的错误是: 公式对象的方法范围失败

突出显示这行代码

** .Range("AE" & i).Formula = 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
excel vba worksheet
1个回答
0
投票

将代码的 with 部分更改为此,它按预期工作。

 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
© www.soinside.com 2019 - 2024. All rights reserved.