See attached file where I added below macro.
Macro will automatically insert formula in column 'E' of customer sheets. Initially you need only to put first theree rows (titles and row with Opening Balance).
Private Sub CommandButton1_Click()
Dim myPath As String
Dim r As Long, lastRow As Long
Dim custId As String, elem As Variant
Dim myRow As Variant
Dim destRow As Long, nr As Integer
Dim otherWb As Workbook
Dim otherSh As Worksheet
Dim sh1 As Worksheet
Dim myDic As Object
myPath = "C:\test\TestSubFolder\"
Set sh1 = ThisWorkbook.Sheets(1)
Set myDic = CreateObject("scripting.dictionary")
lastRow = sh1.Range("a2").CurrentRegion.Rows.Count + sh1.Range("a2") _
.CurrentRegion.Row - 1
For r = 2 To lastRow
custId = sh1.Cells(r, "b")
If Not myDic.exists(custId) Then
myDic.Add Item:=r, key:=custId
Else
myDic(custId) = myDic(custId) & "," & r
End If
Next r
'store rows for each customer so macro opens only one time customer workbook
For Each elem In myDic.keys
'open customer workbook
Set otherWb = Workbooks.Open(myPath & elem & ".xls")
Set otherSh = otherWb.ActiveSheet
lastRow = otherSh.Cells(Rows.Count, "a").End(xlUp).Row
destRow = lastRow
nr = UBound(Split(myDic(elem), ",")) + 1
'macro copies data for each row customer from primary wb to customer sheet
For Each myRow In Split(myDic(elem), ",")
destRow = destRow + 1
otherSh.Cells(destRow, "a") = sh1.Cells(myRow, "a")
otherSh.Cells(destRow, "b") = sh1.Cells(myRow, "c")
otherSh.Cells(destRow, "d") = sh1.Cells(myRow, "d")
Next myRow
'apply formula on 'E' column
otherSh.Cells(lastRow + 1, "e").Resize(nr, 1).Formula = "=e" & lastRow _
& "+c" & lastRow + 1 & "-d" & lastRow + 1
otherSh.Range("a:a").NumberFormat = "d-mmm-yy"
'close and save customer workbook.Close True
Next elem
'clean data for next day
sh1.Range("a2:d2").Resize(sh1.Range("a2").CurrentRegion.Rows.Count).ClearContents
End Sub
Regards,
Antonio
Bookmarks