使用 ParamArray 分配宏:公式太复杂而无法添加到对象
Assigning Macro with ParamArray: Formula is Too Complex to add to the Object
我有一个宏(如下),它使用 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 上或仅在我的电脑上完成,并且设置会继续?
我想做的只是接受 2 个命名范围,然后是以下范围只是用前一个范围的行数抵消这些范围,所以如果 Range2 = Sheets().Range("A1:A10")
那么 Range3 = Range2.Offset(Range2.Rows.Count,0)
那么评估输入只需要 Range1 as string, Range2 as string, NumberOfExtraRanges as integer
我需要至少两个范围的原因是因为范围 1 之后的每个范围都在不同的选项卡上,并且本质上是第一个选项卡中所有付费信息时间等的原始数据版本将是 Range1_EmployeeList
我将在等待回复时使用它。
TIA
不是一个完整的答案,但我确实发现在 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"'
我有一个宏(如下),它使用 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 上或仅在我的电脑上完成,并且设置会继续?
我想做的只是接受 2 个命名范围,然后是以下范围只是用前一个范围的行数抵消这些范围,所以如果 Range2 = Sheets().Range("A1:A10")
那么 Range3 = Range2.Offset(Range2.Rows.Count,0)
那么评估输入只需要 Range1 as string, Range2 as string, NumberOfExtraRanges as integer
我需要至少两个范围的原因是因为范围 1 之后的每个范围都在不同的选项卡上,并且本质上是第一个选项卡中所有付费信息时间等的原始数据版本将是 Range1_EmployeeList
我将在等待回复时使用它。
TIA
不是一个完整的答案,但我确实发现在 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"'