excel vba值粘贴“类型”的所有公式=X();

excel vba值粘贴“类型”的所有公式=X();,excel,array-formulas,vba,Excel,Array Formulas,Vba,我有一个数据库应用程序,通过UDF以数组公式存储数据 我希望有一个宏,它通过sheet/wbook,用给定单元格中的当前值替换udf数组公式,从而断开所有外部链接 挑战在于给定数组公式中的单元格不能单独写入。例如,下面这样的宏将导致在第一次写入时销毁整个数组 Public Sub breaklink() Dim c For Each c In ActiveSheet.Cells.SpecialCells(xlCellTypeFormulas) Debug.Print c.FormulaA

我有一个数据库应用程序,通过UDF以数组公式存储数据

我希望有一个宏,它通过sheet/wbook,用给定单元格中的当前值替换udf数组公式,从而断开所有外部链接

挑战在于给定数组公式中的单元格不能单独写入。例如,下面这样的宏将导致在第一次写入时销毁整个数组

Public Sub breaklink()
Dim c
For Each c In ActiveSheet.Cells.SpecialCells(xlCellTypeFormulas)
    Debug.Print c.FormulaArray
    If InStr(c.FormulaArray, "MYFORMULA(") Then
        Stop
        c.FormulaArray = c.Value
        'c.Value = c.Value     --THIS THROWS ERROR 1004 (Can't edit part of an array)
        Stop
    End If
Next
End Sub
如果有类似于
c.getArrayForMularRange
的单元格方法,那么我可以使用它创建一个值数组,然后写入数组公式


可以想象,我可以在相邻的单元格中循环,试图找到每个数组的边界,但这似乎相当麻烦(而且,我会在循环过程中更改循环的范围,这可能会引起问题)。是否有任何方法或对象属性可以帮助我识别给定数组公式占用的整个范围?

根据上面简单的人的建议,这是我的解决方案:

Public Sub breakLinks(scope As String)
Dim formula_tokens()
Dim c As Range, fa_range As Range
Dim ws As Worksheet
Dim token
formula_tokens = Array("MYFORMULA1(", "MYFORMULA2(", "OTHERFORMULA(", "OTHERFORMULA2(")
If scope = "sheet" Then
    For Each c In ActiveSheet.Cells.SpecialCells(xlCellTypeFormulas)
        For Each token In formula_tokens
            If InStr(UCase(c.FormulaArray), token) Then
                If c.HasArray Then
                    Set fa_range = c.CurrentArray
                    fa_range.FormulaArray = fa_range.Value
                Else
                    c.Formula = c.Value
                End If
            End If
        Next
    Next

ElseIf scope = "wbook" Then
    For Each ws In Worksheets
        For Each c In ws.Cells.SpecialCells(xlCellTypeFormulas)
            For Each token In formula_tokens
                If InStr(UCase(c.FormulaArray), token) Then
                    If c.HasArray Then
                        Set fa_range = c.CurrentArray
                        fa_range.FormulaArray = fa_range.Value
                    Else
                        c.Formula = c.Value
                    End If
                End If
            Next
        Next
    Next

End If

End Sub

按照上面简单的人的建议,这是我的解决方案:

Public Sub breakLinks(scope As String)
Dim formula_tokens()
Dim c As Range, fa_range As Range
Dim ws As Worksheet
Dim token
formula_tokens = Array("MYFORMULA1(", "MYFORMULA2(", "OTHERFORMULA(", "OTHERFORMULA2(")
If scope = "sheet" Then
    For Each c In ActiveSheet.Cells.SpecialCells(xlCellTypeFormulas)
        For Each token In formula_tokens
            If InStr(UCase(c.FormulaArray), token) Then
                If c.HasArray Then
                    Set fa_range = c.CurrentArray
                    fa_range.FormulaArray = fa_range.Value
                Else
                    c.Formula = c.Value
                End If
            End If
        Next
    Next

ElseIf scope = "wbook" Then
    For Each ws In Worksheets
        For Each c In ws.Cells.SpecialCells(xlCellTypeFormulas)
            For Each token In formula_tokens
                If InStr(UCase(c.FormulaArray), token) Then
                    If c.HasArray Then
                        Set fa_range = c.CurrentArray
                        fa_range.FormulaArray = fa_range.Value
                    Else
                        c.Formula = c.Value
                    End If
                End If
            Next
        Next
    Next

End If

End Sub

c.CurrentArray
将返回
FormulaArray
范围(如果
c
中有)。您可以使用
c.HasArray
检查
c
中是否存在数组。如果有,它将返回
True
。顺便说一句,您应该将
c
设置为
范围
,以访问适用于它的方法和属性列表。这非常完美。谢谢大家!
c.CurrentArray
将返回
FormulaArray
范围(如果
c
中有)。您可以使用
c.HasArray
检查
c
中是否存在数组。如果有,它将返回
True
。顺便说一句,您应该将
c
设置为
范围
,以访问适用于它的方法和属性列表。这非常完美。非常感谢。在这一行
Dim c,fa_range作为range
,只有
fa_range
将被视为
range
。如果要在同一行上声明,必须这样做:
Dim c As Range,fa_Range As Range
在这一行
Dim c,fa_Range As Range
,只有
fa_Range
将被视为
Range
。如果要在同一行上声明,必须这样做:
Dim c As Range,fa_Range As Range