0

I keep getting a runtime Error 424 when I try to access arrayCount.Length. I think this might have to do with the fact that arrayCount was declared as a Public Variant. How do I resolve this bug?

' Initialize variables
Private counter As Integer
Private Account As String
Private chartSize As Integer

Public arrayCount As Variant
Public arrayAccounts As Variant

' Iterate over each entry row, determining the corresponding Account

Sub RowInsert()

' Initialize ArrayCount with starting values of -1
arrayCount = Array(-1, -1, -1, -1, -1, -1, -1, -1, -1)
arrayAccounts = Array("Cash", "Equipment", "Prepaid Rent", "Inventory", "Marketable Securities", "Accounts Recievable", "Accounts Payable", "Bonds Payable", "Common Stock")

' BUG HERE
chartSize = arrayAccounts.Length

' Continued...

End Sub

'Continued...
cpage
  • 119
  • 6
  • 27
  • 2
    VBA arrays aren't objects, they don't have a `.Length` member. In fact, they don't have *any* members. Try `UBound(arrayAccounts)` instead – Mathieu Guindon Oct 20 '16 at 03:09
  • I read your other post( [Global Array that Function Can Edit in VBA](http://stackoverflow.com/questions/40133757/global-array-that-function-can-edit-in-vba) ) earlier today and there are better ways to do what you want to do. Can you provide a screenshots of the worksheets? –  Oct 20 '16 at 03:21
  • @ThomasInzina, thank you for your help. The code has changed a bit since then. Note that this one only works for Assets. Here is a link to the downloadable Excel file with Macros enabled: https://drive.google.com/file/d/0B-S_Oyu8bMqySFU4MjEwdHZaRTg/view?usp=sharing – cpage Oct 20 '16 at 03:39
  • The formatting gave me a little trouble but I think that I got it. With some modifications to my could you could add a new worksheet create the report on it. In this way you wouldn't need to maintain a template ledger. It also wouldn't matter if you added categories to the Journal. –  Oct 20 '16 at 05:28

2 Answers2

0

As an alternate approach I compiled all the information using a Dictionary to group the data. Each key in the Dictionary has an ArrayList associated with it. Each element in the ArrayList is an 1 dimensional array of data that holds the Date, Debit and Credit information.

The Ledger is then searched for each Key in the Dictionary. If found the array that the Dictionary's ArrayList is extracted and transposed twice to convert it to a standard 2 dimensional array. The array is then inserted into worksheet.

enter image description here

Sub CompileData()
    Application.ScreenUpdating = False
    Dim x As Long
    Dim Data, Key
    Dim r As Range
    Dim dLedger As Object, list As Object

    Set dLedger = CreateObject("Scripting.Dictionary")
    With Worksheets("Journal")
        For x = 2 To .Range("B" & .Rows.Count).End(xlUp).Row
            Key = Trim(.Cells(x, 2))
            If Not dLedger.Exists(Key) Then
                Set list = CreateObject("System.Collections.ArrayList")
                dLedger.Add Key, list
            End If

            dLedger(Key).Add Array(.Cells(x, 1).Value, .Cells(x, 3).Value, .Cells(x, 4).Value)

        Next
    End With

    With Worksheets("Ledger")

        For Each Key In dLedger
            Set r = Intersect(.Columns("A:C"), .UsedRange).Find(What:=Key)

            If Not r Is Nothing Then
                Set list = dLedger(Key)
                Data = list.ToArray
                Data = Application.Transpose(Data)

                x = dLedger(Key).Count
                With r.Offset(2).Resize(x, 3)
                    .Insert Shift:=xlDown, CopyOrigin:=r.Offset(1)
                    .Offset(-x).Value = Application.Transpose(Data)
                    .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]),""$#,###"")"
                    r.Offset(1).EntireRow.Delete
                End With
            End If
        Next

    End With
    Application.ScreenUpdating = True

End Sub
  • Thank you so much. Unfortunately, this code won't compile since I am using Microsoft Excel 2016 for Mac and apparently Microsoft Scripting Runtime Library doesn't work on Macs. I found this solution but have been unable to make it work since I am not sure how to import the .cls files: http://stackoverflow.com/a/24422819/6819862 – cpage Oct 20 '16 at 16:40
  • Could this work using a collection instead of dictionary? – cpage Oct 20 '16 at 22:40
0

I coerced my previous answer to use collections instead of a Dictionary and Arraylists; so that it would be Mac compatible.

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)

        Next
    End With

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

            If r <> "" Then

            On Error Resume Next
            Key = Trim(r.Text)
            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
                    .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]),""$#,###"")"
                    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
  • Wow! Thank you, I removed the third line of the program and added `Worksheets("Journal").Cells(x, 5).Value = ChrW(&H2713)` before the Next in the first For loop to make the checkmarks under the Posted Column. Now onto generating Trial Balances for all of the accounts! – cpage Oct 21 '16 at 02:56
  • Lol, I forgot to remove that line. –  Oct 21 '16 at 02:58
  • how is `Set list = cLedger(Key) x` used in this program? I don't see `list` referenced anywhere else in it. – cpage Oct 22 '16 at 14:25
  • CLedger is a Collection of Collection. List is a Collection stored in cLedger and accessed by it's key, CLedger(Key) . –  Oct 22 '16 at 15:51