2016-10-22 75 views
0

我正在开发将日记帐分录发布到分类帐的会计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代表普通股的余额。如何正确地将平衡值存储在变量中?在Excel中评估和存储复杂表达式VBA

Screen Recording

' 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 

回答

0

这里是我能弄清楚,虽然我不知道这是否是最有效的解决方案。在公式设置之前,我将Range设置为BalanceCell到公式将被写入的单元格。然后我使用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 

ScreenRecording