0

公式太复杂的消息

我有一个宏(如下),它使用 ParamArray 将新行插入到未定义数量的命名范围中,它工作正常,除了当我尝试为宏分配超过 5-6 个参数时,我收到一个消息框,上面写着“公式太复杂而无法分配给对象”(见上图)

(请参阅下面的分配字符串)

'InsertNewRow "ServiceCrewDay_EmployeeList"、"SAP_SCD_InPool"、"SAP_SCD_OutPool"、"SAP_SCD_SecondaryIn"、"SAP_SCD_SecondaryOut"、"SAP_SCD_ORD"、"SAP_SCD_THF"、"SAP_SCD_LH"、"SAP_SCD_LH"'

宏:

    Sub InsertNewRow(ParamArray args() As Variant)
    
    Dim ans: ans = MsgBox("WARNING: " & vbNewLine _
    & "Action Cannot be undone!" & vbNewLine & "Continue?", vbYesNo, "Warning!")
    If ans = vbNo Then: Exit Sub
    
    Call HaltOperations
    Call ActiveSheet.Unprotect()
    Call Sheets("SAP Timesheet").Unprotect()
    On Error GoTo OnError_Exit
    
       'Loop and Check All Named Ranges Exist Before Proceeding
        For Each a In args
            If RangeExists(a) = False Then
                MsgBox ("Named Range: " & a & " Not Defined!" & vbNewLine & "Operation Cancelled")
                Exit Sub
            End If
        Next a
    
    Dim rng As Range
    
    'ADD ROW TO EACH NAMED INPUT RANGE
    For Each a In args
        Set rng = Range(a)
        With rng
            .Rows(.Rows.count).EntireRow.Insert
            .Rows(.Rows.count - 2).EntireRow.Copy
            .Rows(.Rows.count - 1).EntireRow.PasteSpecial (xlPasteFormulasAndNumberFormats)
            On Error Resume Next: .Rows(.Rows.count - 1).EntireRow.PasteSpecial (xlPasteFormats)
        End With
    Next a
    On Error GoTo OnError_Exit
    
    'ADJUST HEIRACHY NUMBERS ON FIRST INPUT RANGE (MANNING TAB)
    Set rng = Range(args(0))
        Dim col As Integer
        col = rng.Column
        
        Cells(rng.Row + rng.Rows.count - 2, col).Offset(0, -1).Value _ 
            = Cells(rng.Row + rng.Rows.count - 3, col).Offset(0, -1).Value + 1

        Cells(rng.Row + rng.Rows.count - 1, col).Offset(0, -1).Value _
            = Cells(rng.Row + rng.Rows.count - 3, col).Offset(0, -1).Value + 2
    
    Call ResumeOperations
    Application.CutCopyMode = False
    Call ActiveSheet.Protect()
    Call Sheets("SAP Timesheet").Protect()
    Exit Sub
    
    OnError_Exit:
        Call ResumeOperations
        Application.CutCopyMode = False
        Call ActiveSheet.Protect()
        Call Sheets("SAP Timesheet").Protect()
    End Sub
    Private Function RangeExists(rng As Variant) As Boolean
        Dim Test As Range
        On Error Resume Next
        Set Test = Range(rng)
        RangeExists = Err.Number = 0
    End Function
    Private Sub HaltOperations()
        Application.ScreenUpdating = False
        Application.EnableEvents = False
        Application.DisplayAlerts = False
        Application.Calculation = xlCalculationManual
    End Sub
    Private Sub ResumeOperations()
    ResumeOps:
        Application.ScreenUpdating = True
        Application.EnableEvents = True
        Application.DisplayAlerts = True
        Application.Calculation = xlCalculationAutomatic
    End Sub

宏本身按预期运行,只是分配导致问题的命名范围。有一个更好的方法吗?

或者有没有办法绕过公式太复杂的方法?如果需要在所有最终用户 PC 上或仅在我的 PC 上完成,并且设置将继续存在?

我想做的只是接受 2 个命名范围,然后是以下范围 只是用前一个范围的行数来抵消这些范围,所以如果那样的Range2 = Sheets().Range("A1:A10")Range3 = Range2.Offset(Range2.Rows.Count,0),那么 assingment 输入只需要成为Range1 as string, Range2 as string, NumberOfExtraRanges as integer我需要至少两个范围的原因是因为范围 1 之后的每个范围都在不同的选项卡上,并且本质上是第一个选项卡中所有工资信息时间等的原始数据版本,这将是 Range1_EmployeeList

我会在等待回复时玩弄它。

TIA

4

1 回答 1

0

不是一个完整的答案,但我确实发现在 ParamArray 中我可以使用 , 分配一个输入范围来分隔每个定义的范围。我还没有测试过这样做的局限性,但它似乎至少让我使用了一些额外的输入。

示例(不工作): 注意:每个定义的范围都是一个单独的输入

'InsertNewRow "ServiceCrewDay_EmployeeList"、"SAP_SCD_InPool"、"SAP_SCD_OutPool"、"SAP_SCD_SecondaryIn"、"SAP_SCD_SecondaryOut"'

示例(工作): 注意每个定义的范围作为 1 个输入传递

'InsertNewRow "ServiceCrewDay_EmployeeList", "SAP_SCD_InPool, SAP_SCD_OutPool,SAP_SCD_SecondaryIn,SAP_SCD_SecondaryOut"'

于 2021-05-26T12:34:11.837 回答