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

私はそれが最も効率的であるかどうかわからないですけれども、私は、把握することができたソリューションです。数式が設定される前の行では、式が書き込まれるセルにBalanceCellという名前のRangeを設定します。私はその後関数を使用して、数式を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