簡體   English   中英

將 VBA 中的數組中的公式粘貼到 excel 表中

[英]Pasting Formula from an Array in VBA into an excel table

所以我正在嘗試制作一個 VBA 腳本,將選擇中的所有間接公式更改為直接引用,目的是提高我的 excel 工作簿的性能。 下面是代碼:

Call manual
Dim continue As Integer
continue = MsgBox("This cannot be undone.  Continue anyway?", vbOKCancel)
If continue <> vbOK Then Exit Sub

Dim formula_array() As Variant

row_cnt = Selection.Rows.count
col_cnt = Selection.Columns.count

ReDim formula_array(1 To row_cnt, 1 To col_cnt)

If row_cnt = 1 And col_cnt = 1 Then
    formula_array(1, 1) = Selection.formula
Else
    formula_array = Selection.formula
End If
'for some reason formula_array = Selection.formula gives an error when I select only one cell
count = 0
Dim i As Integer, y As Integer
For i = 1 To row_cnt
    For y = 1 To col_cnt
        frmula = formula_array(i, y)
        oldfunc = find_full_formula(frmula, "indirect(")
        Do While (oldfunc <> "")
            newfunc = Application.Evaluate(oldfunc)
            If IsError(newfunc) Then
                newfunc = ""
            End If
            oldfunc = "indirect(" & oldfunc & ")"
            formula_array(i, y) = Replace(formula_array(i, y), oldfunc, newfunc, 1, -1, vbTextCompare)
            frmula = formula_array(i, y)
            oldfunc = find_full_formula(frmula, "indirect(")
            count = count + 1
        Loop
    Next y
Next i
Dim temp As String
Selection.formula = formula_array
MsgBox count
Call auto

Here the find_full_formula function gives arguments of any function, input is the start of that function and the whole formula. 因此,如果您有一個公式“間接(“A1:B2”)”,那么這個 function 的結果將是“A1:B2”。

整個腳本適用於正常范圍,除非我嘗試在 excel 表的列上運行,其中選擇還包括該列的第一個單元格(數據的第一個單元格,所以不是標題)然后結果是該列中的所有單元格都具有與第一個單元格相同的公式。 同樣有趣的是,如果我 select 表格中除第一個列之外的所有單元格,那么結果很好,但只有當第一個單元格也涉及時,才會出現問題。 它顯然看起來像一些自動填充功能,但我已經關閉了我能找到的所有此類設置,但這個問題仍然沒有解決。

好的,我在下面添加了一個更簡單的 VBA 代碼版本來突出我的問題:

Dim arr(1 To 4, 1 To 1) As Variant
arr(1, 1) = "2+2"
arr(2, 1) = "=3+2"
arr(3, 1) = "=4+2"
arr(4, 1) = "=5+2"
Range("A2:A5").Formula = arr

上面的代碼工作得很好,但是下面的代碼導致“= 2 + 2”作為我表格每個單元格的公式。

Dim arr(1 To 4, 1 To 1) As Variant
arr(1, 1) = "=2+2"
arr(2, 1) = "=3+2"
arr(3, 1) = "=4+2"
arr(4, 1) = "=5+2"
Range("A2:A5").Formula = arr

excel 中的表看起來像這樣: Excel 表

我找到了一個適用於我檢查的所有情況的解決方案,但它並不漂亮 - 將其視為一種解決方法:

  1. 設置Application.AutoCorrect.AutoFillFormulasInLists = False
  2. 通過循環將公式設置為單元格(一個接一個)

如果選擇與ListObject.DataBodyRange匹配,則這些都不會按預期設置公式。

Sub Test()
    ' select a range that fits
    ' the following arrays dimensions

    Dim arr(1 To 4, 1 To 2) As Variant
    arr(1, 1) = "=2+2": arr(1, 2) = "=12+2"
    arr(2, 1) = "=3+2": arr(2, 2) = "=13+2"
    arr(3, 1) = "=4+2": arr(3, 2) = "=14+2"
    arr(4, 1) = "=5+2": arr(4, 2) = "=15+2"

    ' deactivate AutoFillFormulasInLists; store setting to restore
    Dim bAutoFill As Boolean
    bAutoFill = Application.AutoCorrect.AutoFillFormulasInLists
    Application.AutoCorrect.AutoFillFormulasInLists = False

    Selection.ClearContents

    ' `Selection.FormulaR1C1 = arr` does NOT work in case of
    ' Selection = ListObject.DataBodyRange
    ' => loop cells (slower and more lines of code)

    Dim i As Long, j As Long
    For i = 1 To UBound(arr, 1)
        For j = 1 To UBound(arr, 2)
            Selection(i, j).FormulaR1C1 = arr(i, j)
        Next j
    Next i

    Application.AutoCorrect.AutoFillFormulasInLists = bAutoFill
End Sub

希望其他人會粘貼一個更直接的解決方案!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM