Hi,
I have code below which works for me however it is very slow to run and I'm pretty sure this is to do with the number of lines in my code. My knowledge is very limited and wandered if there a way the below code could be shortened so it runs quicker? It put data on "input sheet" into the relevant table in "Chart of Accounts" based on the trigger word in column F of the input sheet.
thanks in advance.
I have code below which works for me however it is very slow to run and I'm pretty sure this is to do with the number of lines in my code. My knowledge is very limited and wandered if there a way the below code could be shortened so it runs quicker? It put data on "input sheet" into the relevant table in "Chart of Accounts" based on the trigger word in column F of the input sheet.
thanks in advance.
VBA Code:
For Each cell In Sheets("Input Sheet").Range("F:F")
If cell.Value = "Cash" Then
Dim config, itm, arr
Dim rw As Range, listCols As ListColumns
Set shtForm = Worksheets("Input Sheet") '<< data source
With Sheets("Chart of Accounts").ListObjects("Table1")
Set rw = .ListRows.Add.Range 'add a new row and get its Range
Set listCols = .ListColumns 'get the columns collection
End With
ElseIf cell.Value = "Accounts Receivable" Then
Set shtForm = Worksheets("Input Sheet") '<< data source
With Sheets("Chart of Accounts").ListObjects("Table2")
Set rw = .ListRows.Add.Range 'add a new row and get its Range
Set listCols = .ListColumns 'get the columns collection
End With
ElseIf cell.Value = "Pre Payments" Then
Set shtForm = Worksheets("Input Sheet") '<< data source
With Sheets("Chart of Accounts").ListObjects("Table3")
Set rw = .ListRows.Add.Range 'add a new row and get its Range
Set listCols = .ListColumns 'get the columns collection
End With
ElseIf cell.Value = "Inventory" Then
Set shtForm = Worksheets("Input Sheet") '<< data source
With Sheets("Chart of Accounts").ListObjects("Table4")
Set rw = .ListRows.Add.Range 'add a new row and get its Range
Set listCols = .ListColumns 'get the columns collection
End With
ElseIf cell.Value = "Vehicles" Then
Set shtForm = Worksheets("Input Sheet") '<< data source
With Sheets("Chart of Accounts").ListObjects("Table5")
Set rw = .ListRows.Add.Range 'add a new row and get its Range
Set listCols = .ListColumns 'get the columns collection
End With
ElseIf cell.Value = "Equipment" Then
Set shtForm = Worksheets("Input Sheet") '<< data source
With Sheets("Chart of Accounts").ListObjects("Table6")
Set rw = .ListRows.Add.Range 'add a new row and get its Range
Set listCols = .ListColumns 'get the columns collection
End With
ElseIf cell.Value = "Accounts" Then
Set shtForm = Worksheets("Input Sheet") '<< data source
With Sheets("Chart of Accounts").ListObjects("Table7")
Set rw = .ListRows.Add.Range 'add a new row and get its Range
Set listCols = .ListColumns 'get the columns collection
End With
ElseIf cell.Value = "Payroll" Then
Set shtForm = Worksheets("Input Sheet") '<< data source
With Sheets("Chart of Accounts").ListObjects("Table8")
Set rw = .ListRows.Add.Range 'add a new row and get its Range
Set listCols = .ListColumns 'get the columns collection
End With
ElseIf cell.Value = "Loan" Then
Set shtForm = Worksheets("Input Sheet") '<< data source
With Sheets("Chart of Accounts").ListObjects("Table9")
Set rw = .ListRows.Add.Range 'add a new row and get its Range
Set listCols = .ListColumns 'get the columns collection
End With
ElseIf cell.Value = "VAT" Then
Set shtForm = Worksheets("Input Sheet") '<< data source
With Sheets("Chart of Accounts").ListObjects("Table10")
Set rw = .ListRows.Add.Range 'add a new row and get its Range
Set listCols = .ListColumns 'get the columns collection
End With
ElseIf cell.Value = "Capital" Then
Set shtForm = Worksheets("Input Sheet") '<< data source
With Sheets("Chart of Accounts").ListObjects("Table11")
Set rw = .ListRows.Add.Range 'add a new row and get its Range
Set listCols = .ListColumns 'get the columns collection
End With
ElseIf cell.Value = "Drawings" Then
Set shtForm = Worksheets("Input Sheet") '<< data source
With Sheets("Chart of Accounts").ListObjects("Table12")
Set rw = .ListRows.Add.Range 'add a new row and get its Range
Set listCols = .ListColumns 'get the columns collection
End With
ElseIf cell.Value = "Sales" Then
Set shtForm = Worksheets("Input Sheet") '<< data source
With Sheets("Chart of Accounts").ListObjects("Table13")
Set rw = .ListRows.Add.Range 'add a new row and get its Range
Set listCols = .ListColumns 'get the columns collection
End With
ElseIf cell.Value = "Other Incomes" Then
Set shtForm = Worksheets("Input Sheet") '<< data source
With Sheets("Chart of Accounts").ListObjects("Table14")
Set rw = .ListRows.Add.Range 'add a new row and get its Range
Set listCols = .ListColumns 'get the columns collection
End With
ElseIf cell.Value = "Salaries" Then
Set shtForm = Worksheets("Input Sheet") '<< data source
With Sheets("Chart of Accounts").ListObjects("Table15")
Set rw = .ListRows.Add.Range 'add a new row and get its Range
Set listCols = .ListColumns 'get the columns collection
End With
ElseIf cell.Value = "Supplies" Then
Set shtForm = Worksheets("Input Sheet") '<< data source
With Sheets("Chart of Accounts").ListObjects("Table16")
Set rw = .ListRows.Add.Range 'add a new row and get its Range
Set listCols = .ListColumns 'get the columns collection
End With
ElseIf cell.Value = "Overhead" Then
Set shtForm = Worksheets("Input Sheet") '<< data source
With Sheets("Chart of Accounts").ListObjects("Table17")
Set rw = .ListRows.Add.Range 'add a new row and get its Range
Set listCols = .ListColumns 'get the columns collection
End With
ElseIf cell.Value = "Utilities" Then
Set shtForm = Worksheets("Input Sheet") '<< data source
With Sheets("Chart of Accounts").ListObjects("Table18")
Set rw = .ListRows.Add.Range 'add a new row and get its Range
Set listCols = .ListColumns 'get the columns collection
End With
ElseIf cell.Value = "Advertising" Then
Set shtForm = Worksheets("Input Sheet") '<< data source
With Sheets("Chart of Accounts").ListObjects("Table19")
Set rw = .ListRows.Add.Range 'add a new row and get its Range
Set listCols = .ListColumns 'get the columns collection
End With
config = Array("Date<>A6", "Company<>B6", "Reference<>C6", "Amount<>D6")
'loop over each item in the config array and transfer the value to the
' appropriate column
For Each itm In config
arr = Split(itm, "<>") ' split to colname and cell address
rw.Cells(listCols(arr(0)).Index).Value = shtForm.Range(arr(1)).Value
Next itm
End If
Next
Last edited by a moderator: