I am trying to loop through a column of dates and search through only dates that fall into a user specified range. From there I have a nested loop that I want to select rows that match a certain ID number. Finally, once it finds a row that matches the date range and the ID number it needs to take the value of the cell corresponding to the row and column E. I want it to add up all of these cells as it loops through to find the dates and IDs that match.
Sub searchdate()
Dim wb As Workbook 'used for active workbook Dim wsSrc As Worksheet 'name of the source sheet Dim wsDest As Worksheet 'destination worksheet Dim i As Long 'will be used to index Dim fRow As Long 'will be used to place information in proper row Dim startDate As Date 'user input variable for storage Dim endDate As Date 'user input variable Dim n As Long 'used to gauge how many rows in source sheet Dim lastRow As Long 'used to track row in the destination sheet Set wb = ActiveWorkbook 'sets the active workbook Set wsSrc = wb.Sheets("Policy Table") 'assigns the name of the source sheet Set wsDest = wb.Sheets("Nicks Test") 'assigns the name of the destination sheet Set wsReport = wb.Sheets("Monthly Report") 'assigns the name of the report sheet n = wsSrc.Range("G:G").Find(what:="*", searchdirection:=xlPrevious).row Sheets("Nicks Test").Cells.Clear 'deletes all old entries in sheet before pasting 'searches column G to see how many rows since G in the 'source should not be empty. Please note that if column G is empty that this code will not run
fRow = 2 'fRow is used to index which row we are placing data into in the destination sheet 'This loop searches for the policies that began during a user input date range 'it copies and pastes just those rows into a new sheet For i = 2 To n 'runs from row 2 to n since row 1 is left for column titles If wsSrc.Range("BB" & i).Value >= startDate And wsSrc.Range("BB" & i).Value <= endDate Then 'This is to see if a cell in column BB has a date that falls into our desired range wsSrc.Range("BB" & i).EntireRow.Copy wsDest.Cells(fRow, 1) 'copies entire row from source and pastes into row fRow lastRow = fRow + 1 'will keep track of the last row that we put data into when pasting fRow = fRow + 1 'only increments in the loop so that we have no empty rows in the destination sheet End If Next fRow = lastRow 'starts next loop where the last one left off 'this loop looks for policies that have terminated during the user input date range For i = 2 To n 'runs from row 2 to n since row 1 is left for column titles If wsSrc.Range("BC" & i).Value >= startDate And wsSrc.Range("BC" & i).Value <= endDate Then 'This is to see if a cell in column BB has a date that falls into our desired range wsSrc.Range("BC" & i).EntireRow.Copy wsDest.Cells(fRow, 1) 'copies entire row from source and pastes into row fRow fRow = fRow + 1 End If Next Dim LH_NDB_In, Barings_NDB_In, CM_NBD_In, Elek_NDB_In, PG3_NDB_In, GenRe_NDB_In, HPC_NDB_In, Ress_NDB_In, Fort_NDB_In, TS_NDB_In, Rams_NDB_In As Double LH_NDB_In = 0 Barings_NDB_In = 0 CM_NDB_In = 0 Elek_NDB_In = 0 PG3_NDB_In = 0 GenRe_NDB_In = 0 HPC_NDB_In = 0 Ress_NDB_In = 0 Fort_NDB_In = 0 TS_NDB_In = 0 Rams_NDB_In = 0
For i = 2 To n If wsSrc.Range("BB" & i).Value >= startDate And wsSrc.Range("BB" & i).Value <= endDate Then If wsSrc.Range("BM" & i).Value = "1294346" Then LH_NDB_In = LH_NDB_In + Worksheets("Policy Table").Cells(5, i).Value ElseIf wsSrc.Range("BM" & i).Value = "2572512" Then Barings_NDB_In = Barings_NDB_In + Worksheets("Policy Table").Cells(5, i).Value
ElseIf wsSrc.Range("BM" & i).Value = "100864" Then CM_NDB_In = CM_NDB_In + Worksheets("Policy Table").Cells(5, i).Value ElseIf wsSrc.Range("BM" & i).Value = "2653931" Then Elek_NDB_In = Elek_NDB_In + Worksheets("Policy Table").Cells(5, i).Value ElseIf wsSrc.Range("BM" & i).Value = "2655210" Then PG3_NDB_In = PG3_NDB_In + Worksheets("Policy Table").Cells(5, i).Value ElseIf wsSrc.Range("BM" & i).Value = "100869" Then GenRe_NDB_In = GenRe_NDB_In + Worksheets("Policy Table").Cells(5, i).Value ElseIf wsSrc.Range("BM" & i).Value = "746142" Then HPC_NDB_In = HPC__NDB_In + Worksheets("Policy Table").Cells(5, i).Value ElseIf wsSrc.Range("BM" & i).Value = "629478" Then Ress_NDB_In = Ress_NDB_In + Worksheets("Policy Table").Cells(5, i).Value ElseIf wsSrc.Range("BM" & i).Value = "260572" Then Fort_NDB_In = Fort_NDB_In + Worksheets("Policy Table").Cells(5, i).Values ElseIf wsSrc.Range("BM" & i).Value = "2278316" Then TS_NDB_In = TS_NDB_In + Worksheets("Policy Table").Cells(5, i).Values ElseIf wsSrc.Range("BM" & i).Value = "1107357" Then Rams_NDB_In = Rams_NDB_In + Worksheets("Policy Table").Cells(5, i).Values End If End If
Aucun commentaire:
Enregistrer un commentaire