EVANWIT84
New Member
- Joined
- Sep 25, 2020
- Messages
- 22
- Office Version
- 365
- Platform
- Windows
- MacOS
- Mobile
Hi All,
I am trying to loop thru each account in my account master, find the range of cells 7 rows over in the next tab, copy those cells into a calculation sheet with a multiple the values, then I want to paste the end result back into my account master sheet. I am able to get the 1st account to work in my list but then it stops after the 1st account and doesn't calculate any further. I'm thinking I need an outer loop where I state For Each Cell in AcctRng then let it loop thru the find, multiply, then copy and write a Next after. Any thoughts?
Sub FindAcctReturn()
'1. Store Accounts as Variable
Dim AcctNum As Long
Dim AcctRng As Range
Dim Cell As Range
Dim CompId As Range
Dim i As Byte
Dim FirstMatch As Variant
Dim CalcSheet As Worksheet
Dim h As Long
Dim lstCell As Long
Set CalcSheet = Sheet9
Set AcctRng = Sheet2.Range("A2:A1000")
Sheets("CalcSheet").Range("a1:i1000").ClearContents 'Clears Content in Calc Sheet Destination
'2 Lookup Accounts in Master Table
i = 3
Set CompId = Sheet1.Range("A:I").Find(AcctRng, LookIn:=xlValues, lookat:=xlWhole)
If Not CompId Is Nothing Then
Sheets("CalcSheet").Range("B" & i).Value = CompId.Offset(, 7).Value
FirstMatch = CompId.Address
Do
Set CompId = Sheet1.Range("A:A").FindNext(CompId)
If CompId.Address = FirstMatch Then Exit Do
i = i + 1
Sheets("CalcSheet").Range("B" & i).Value = CompId.Offset(, 7).Value
Loop
End If
'3 Perform Calc of cumulative returns in CalcSheet by acct
lstCell = CalcSheet.Range("b1048567").End(xlUp).Row
For h = 2 To lstCell
If h = lstCell Then Exit Sub
CalcSheet.Cells(h, "c") = 1 + CalcSheet.Cells(h, "B")
CalcSheet.Range("C1").Value = "=PRODUCT(R[1]C:R[999]C)-1"
Sheet2.Range("D2").Value = CalcSheet.Range("C1").Value '4 Paste Data from Variable into Account Master Tab
Next
End Sub
I am trying to loop thru each account in my account master, find the range of cells 7 rows over in the next tab, copy those cells into a calculation sheet with a multiple the values, then I want to paste the end result back into my account master sheet. I am able to get the 1st account to work in my list but then it stops after the 1st account and doesn't calculate any further. I'm thinking I need an outer loop where I state For Each Cell in AcctRng then let it loop thru the find, multiply, then copy and write a Next after. Any thoughts?
Sub FindAcctReturn()
'1. Store Accounts as Variable
Dim AcctNum As Long
Dim AcctRng As Range
Dim Cell As Range
Dim CompId As Range
Dim i As Byte
Dim FirstMatch As Variant
Dim CalcSheet As Worksheet
Dim h As Long
Dim lstCell As Long
Set CalcSheet = Sheet9
Set AcctRng = Sheet2.Range("A2:A1000")
Sheets("CalcSheet").Range("a1:i1000").ClearContents 'Clears Content in Calc Sheet Destination
'2 Lookup Accounts in Master Table
i = 3
Set CompId = Sheet1.Range("A:I").Find(AcctRng, LookIn:=xlValues, lookat:=xlWhole)
If Not CompId Is Nothing Then
Sheets("CalcSheet").Range("B" & i).Value = CompId.Offset(, 7).Value
FirstMatch = CompId.Address
Do
Set CompId = Sheet1.Range("A:A").FindNext(CompId)
If CompId.Address = FirstMatch Then Exit Do
i = i + 1
Sheets("CalcSheet").Range("B" & i).Value = CompId.Offset(, 7).Value
Loop
End If
'3 Perform Calc of cumulative returns in CalcSheet by acct
lstCell = CalcSheet.Range("b1048567").End(xlUp).Row
For h = 2 To lstCell
If h = lstCell Then Exit Sub
CalcSheet.Cells(h, "c") = 1 + CalcSheet.Cells(h, "B")
CalcSheet.Range("C1").Value = "=PRODUCT(R[1]C:R[999]C)-1"
Sheet2.Range("D2").Value = CalcSheet.Range("C1").Value '4 Paste Data from Variable into Account Master Tab
Next
End Sub