2016-10-22 74 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