【问题标题】:Evaluate and Store Complex Expression in Excel VBA在 Excel VBA 中计算和存储复杂表达式
【发布时间】:2016-10-22 15:41:38
【问题描述】:

我正在开发一个会计 VBA 程序,该程序会将日记帐分录发布到分类帐中,然后生成试算表(即在分类帐中“Bal.”之后的新表上打印出值)。为此,我需要一种将平衡单元的数字部分分配给变量或集合的方法。不幸的是,当我使用 Debug.Print 时,我看到存储的唯一值是 0(我正在使用 Common Stock 进行测试)。我的表达是:y = Application.Evaluate("=SUM(R[-" & x & "]C:R[-1]C)-SUM(R[-" & x & "]C[1]:R[-1]C[1])") 其中y 代表普通股的余额。如何正确地将余额值存储在变量中?

' TODO BE ABLE TO RUN MULTIPLE TIMES
' CHECK FOR POSTED MARK & START WRITING WHEN
' r = "one of the keys", or just creates new Ledger Worksheet every time

Sub MacCompileData()
Application.ScreenUpdating = False
Dim lastRow As Long, x As Long
Dim data, Key
Dim r As Range
Dim cLedger As Collection, cList As Collection
Set cLedger = New Collection

With Worksheets("Journal")
    lastRow = .Range("B" & .Rows.Count).End(xlUp).Row

    For x = 2 To lastRow
        Key = Trim(.Cells(x, 2))
        On Error Resume Next
        Set cList = cLedger(Key)
        If Err.Number <> 0 Then
            Set cList = New Collection
            cLedger.Add cList, Key
        End If
        On Error GoTo 0

        cLedger(Key).Add Array(.Cells(x, 1).Value, .Cells(x, 3).Value, .Cells(x, 4).Value)
        Worksheets("Journal").Cells(x, 5).Value = ChrW(&H2713)

    Next
End With

With Worksheets("Ledger")

    Dim IsLiability As Boolean
    Dim y As Integer

    For Each r In .Range("A1", .Range("A" & .Rows.Count).End(xlUp))

        If r <> "" Then

        On Error Resume Next
        Key = Trim(r.Text)

        If Key = "LIABILITIES" Then
            IsLiability = True
        End If

        data = getLedgerArray(cLedger(Key))

        If Err.Number = 0 Then
            Set list = cLedger(Key)
            x = cLedger(Key).Count
            With r.Offset(2).Resize(x, 3)
                .Insert Shift:=xlDown, CopyOrigin:=r.Offset(1)
                .Offset(-x).Value = data

                If IsLiability Then
                    .Offset(0, 2).Resize(1, 1).FormulaR1C1 = "=""Bal. "" & TEXT(SUM(R[-" & x & "]C:R[-1]C)-SUM(R[-" & x & "]C[1]:R[-1]C[1]),""$#,###"")"

                    ' LOOK HERE FOR Y
                    y = Application.Evaluate("=SUM(R[-" & x & "]C:R[-1]C)-SUM(R[-" & x & "]C[1]:R[-1]C[1])")

                    Debug.Print "Common Stock Balance Equals "; y

                Else
                    .Offset(0, 1).Resize(1, 1).FormulaR1C1 = "=""Bal. "" & TEXT(SUM(R[-" & x & "]C:R[-1]C)-SUM(R[-" & x & "]C[1]:R[-1]C[1]),""$#,###"")"
                End If


                r.Offset(1).EntireRow.Delete
            End With
        End If

        On Error GoTo 0
        End If
    Next

End With
Application.ScreenUpdating = True

End Sub

Function getLedgerArray(c As Collection)
Dim data
Dim x As Long
ReDim data(1 To c.Count, 1 To 3)

For x = 1 To c.Count
    data(x, 1) = c(x)(0)
    data(x, 2) = c(x)(1)
    data(x, 3) = c(x)(2)
Next
getLedgerArray = data
End Function

【问题讨论】:

    标签: vba excel


    【解决方案1】:

    这是我能够弄清楚的一个解决方案,但我不确定它是否是最有效的。在设置公式之前的行中,我将一个名为BalanceCellRange 设置为将写入公式的单元格。然后我使用Mid函数在将公式放入BalanceCell后从单元格中获取字符串数值(因为“Bal。”的长度始终为5个字符)。

    If IsLiability Then
                        Set BalanceCell = .Offset(0, 2).Resize(1, 1)
                        BalanceCell.FormulaR1C1 = "=""Bal. "" & TEXT(SUM(R[-" & x & "]C:R[-1]C)-SUM(R[-" & x & "]C[1]:R[-1]C[1]),""$#,###"")"
    
                        y = Mid(BalanceCell.Value, 6, Len(BalanceCell.Value))
    
                        Debug.Print "Common Stock Balance is "; y
    

    【讨论】:

      猜你喜欢
      • 2019-07-08
      • 2012-03-17
      • 2010-10-25
      • 1970-01-01
      • 2017-07-17
      • 2017-06-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多