Untitled

 avatar
Greg
plain_text
5 months ago
6.9 kB
3
Indexable
Sub UpdateLedgersWithValues()
    Dim reconWorkbook As Workbook
    Dim editedOutputWorkbook As Workbook
    Dim accountsSheet As Worksheet
    Dim ledgersSheet As Worksheet
    Dim pasteSheet As Worksheet
    Dim portiaWorkbook As Workbook
    Dim portiaSheet As Worksheet
    Dim folderPath As String
    Dim folderName As String
    Dim editedOutputFilename As String
    Dim portiaFilename As String
    Dim accountCell As Range
    Dim lastCol As Long
    Dim fundNumber As String
    Dim cusip As String
    Dim rValue As Variant
    Dim pasteRange As Range
    Dim lastRow As Long
    Dim foundRow As Range
    Dim firstAddress As String
    Dim portiaValue As Variant
    Dim accountRow As Range

    ' Set folder path and extract folder name
    folderPath = ThisWorkbook.Path & "\"
    folderName = Mid(ThisWorkbook.Path, InStrRev(ThisWorkbook.Path, "\") + 1)

    ' Construct the edited output filename
    editedOutputFilename = folderPath & folderName & ".SS Daily Cash Transactions_Edited_Output.xlsx"

    ' Open the Edited Output workbook
    Set editedOutputWorkbook = Workbooks.Open(editedOutputFilename)
    Set accountsSheet = ThisWorkbook.Sheets("accounts")
    Set ledgersSheet = ThisWorkbook.Sheets("ledgers")
    Set pasteSheet = editedOutputWorkbook.Sheets("SS holdings-paste from SS file")

    ' Find the last column in Row 1 of the ledgers sheet
    lastCol = ledgersSheet.Cells(1, ledgersSheet.Columns.Count).End(xlToLeft).Column

    ' Open the Portia workbook
    portiaFilename = folderPath & folderName & ".Portia Settled Cash Balances.xlsx"
    On Error Resume Next
    Set portiaWorkbook = Workbooks.Open(portiaFilename)
    On Error GoTo 0

    If portiaWorkbook Is Nothing Then
        MsgBox "The Portia Settled Cash Balances file was not found!", vbCritical
        Exit Sub
    End If

    Set portiaSheet = portiaWorkbook.Sheets("prior day settled cash")

    ' Loop through each account in Row 1
    For Each accountCell In ledgersSheet.Range("B1:" & ledgersSheet.Cells(1, lastCol).Address)
        If accountCell.Value <> "" Then
            ' Find account in the accounts sheet
            Set accountRow = accountsSheet.Columns("B").Find(What:=accountCell.Value, LookIn:=xlValues, LookAt:=xlWhole)

            If Not accountRow Is Nothing Then
                fundNumber = accountRow.Offset(0, -1).Value ' Column A
                cusip = accountRow.Offset(0, 1).Value ' Column C

                ' Debugging: Output fund number and CUSIP
                Debug.Print "Fund Number: " & fundNumber & ", CUSIP: " & cusip

                ' Update Row 2 (Invested Cash)
                lastRow = pasteSheet.Cells(pasteSheet.Rows.Count, 1).End(xlUp).Row
                Set pasteRange = pasteSheet.Range("A1:R" & lastRow)
                Set foundRow = pasteRange.Find(What:=fundNumber, LookIn:=xlValues, LookAt:=xlWhole)

                If Not foundRow Is Nothing Then
                    If foundRow.Offset(0, 10).Value = cusip Then ' Column K
                        If Not IsError(foundRow.Offset(0, 17).Value) Then ' Column R
                            rValue = foundRow.Offset(0, 17).Value
                            ledgersSheet.Cells(2, accountCell.Column).Value = rValue
                            Debug.Print "R Value: " & rValue
                        Else
                            ledgersSheet.Cells(2, accountCell.Column).Value = "Error"
                            Debug.Print "Error in R Value for Fund Number: " & fundNumber
                        End If
                    End If
                End If

                ' Update Row 3 (Uninvested Cash)
                Set foundRow = pasteRange.Find(What:=fundNumber, LookIn:=xlValues, LookAt:=xlWhole)
                Dim yValues As Collection
                Set yValues = New Collection

                If Not foundRow Is Nothing Then
                    firstAddress = foundRow.Address
                    Do
                        If Right(foundRow.Offset(0, 5).Value, 4) = "/000" And foundRow.Offset(0, 6).Value = "US DOLLAR" Then
                            On Error Resume Next
                            yValues.Add foundRow.Offset(0, 24).Value - foundRow.Offset(0, 25).Value ' Column Y - Column Z
                            On Error GoTo 0
                        End If
                        Set foundRow = pasteRange.FindNext(foundRow)
                    Loop While Not foundRow Is Nothing And foundRow.Address <> firstAddress
                End If

                ' Calculate the most common Y-Z value for Row 3
                If yValues.Count > 0 Then
                    Dim dict As Object
                    Set dict = CreateObject("Scripting.Dictionary")
                    Dim value As Variant
                    Dim maxCount As Long
                    Dim mostCommonValue As Variant

                    For Each value In yValues
                        If Not dict.Exists(value) Then
                            dict(value) = 1
                        Else
                            dict(value) = dict(value) + 1
                        End If
                    Next value

                    ' Find the most common value
                    For Each value In dict.Keys
                        If dict(value) > maxCount Then
                            maxCount = dict(value)
                            mostCommonValue = value
                        End If
                    Next value

                    ledgersSheet.Cells(3, accountCell.Column).Value = mostCommonValue
                Else
                    ledgersSheet.Cells(3, accountCell.Column).Value = 0 ' Default to 0 if no matches
                End If

                ' Update Row 8 (Portia Values)
                lastRow = portiaSheet.Cells(portiaSheet.Rows.Count, 1).End(xlUp).Row
                Set foundRow = portiaSheet.Range("A1:C" & lastRow).Find(What:=fundNumber, LookIn:=xlValues, LookAt:=xlWhole)

                If Not foundRow Is Nothing Then
                    firstAddress = foundRow.Address
                    Do
                        If CStr(foundRow.Offset(0, 1).Value) Like "*-CASH-*" Then
                            portiaValue = foundRow.Offset(0, 2).Value ' Column C
                            ledgersSheet.Cells(8, accountCell.Column).Value = portiaValue
                            Debug.Print "Portia Value: " & portiaValue
                        End If
                        Set foundRow = portiaSheet.Range("A1:C" & lastRow).FindNext(foundRow)
                    Loop While Not foundRow Is Nothing And foundRow.Address <> firstAddress
                End If
            Else
                Debug.Print "Account not found for: " & accountCell.Value
            End If
        End If
    Next accountCell

    ' Close the workbooks
    editedOutputWorkbook.Close SaveChanges:=False
    portiaWorkbook.Close SaveChanges:=False

    MsgBox "Ledgers updated successfully!"
End Sub
Editor is loading...
Leave a Comment