Hi all,
I am building a legislation tracker to follow any bills that I manually enter into the spreadsheet. I have two tabs with data - RawData and CleanData. RawData has each instance of a bill; if a bill has progressed through the legislature, I will enter each update as a separate entry. I built a macro to take each unique state/chamber/bill number combination and copy it over to CleanData.
Because I didn't want to have to re-enter all of the information for updates of bills that I am already tracking, I built a macro to check if the first three columns (A,B,C) match the inputs on the userform (StateComboBox, ChamberComboBox, BillNumberTextBox). If there is a match (and I am already tracking the bill), it will copy columns D:I from the previous entry of the bill. If there is no match, it pulls up another user form to capture the rest of the information.
However, I have an issue when there are multiple bills from the same state. The macro will recognize that the first bill is in the tracker, but treats any other bills from that state as new (e.g. if NY H 1 and NY H 2 are in the tracker, the macro sees NY H 1 as an existing bill but thinks NY H 2 is new).
I think my problem is that For Each loops don't select each value in the loop; I tried to account for that by selecting A2 before using INDEX(MATCH()) but apparently that'll only return the first match for each state name.
Any advice or guidance would be a lifesaver!
I am building a legislation tracker to follow any bills that I manually enter into the spreadsheet. I have two tabs with data - RawData and CleanData. RawData has each instance of a bill; if a bill has progressed through the legislature, I will enter each update as a separate entry. I built a macro to take each unique state/chamber/bill number combination and copy it over to CleanData.
Because I didn't want to have to re-enter all of the information for updates of bills that I am already tracking, I built a macro to check if the first three columns (A,B,C) match the inputs on the userform (StateComboBox, ChamberComboBox, BillNumberTextBox). If there is a match (and I am already tracking the bill), it will copy columns D:I from the previous entry of the bill. If there is no match, it pulls up another user form to capture the rest of the information.
However, I have an issue when there are multiple bills from the same state. The macro will recognize that the first bill is in the tracker, but treats any other bills from that state as new (e.g. if NY H 1 and NY H 2 are in the tracker, the macro sees NY H 1 as an existing bill but thinks NY H 2 is new).
Code:
Dim found As Boolean: found = FalseDim chamb, bill As Variant
Dim statename As Variant
Dim findrow, lastrow As Long
'Make RawData A2 active
Worksheets("RawData").Activate
Worksheets("RawData").Cells(2, 1).Select
'Define findrow
findrow = ActiveCell.Row
'Define LastRow
lastrow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row
'Define statename
statename = Range("A" & findrow & ":A" & lastrow).Value
'Loop through the state names of all bills
For Each statename In Range("A" & findrow & ":A" & lastrow)
'Create INDEX(MATCH()) formulas to find chamber and bill number
chamb = Application.Index(Range("B" & findrow & ":B" & lastrow), Application.Match(statename.Value, Range("A" & findrow & ":A" & lastrow), 0), 1)
bill = Application.Index(Range("C" & findrow & ":C" & lastrow), Application.Match(statename.Value, Range("A" & findrow & ":A" & lastrow), 0), 1)
'Determine if bill is already in tracker
If statename.Value = StateComboBox.Value And chamb = ChamberComboBox.Value And bill = CDbl(BillNumberTextBox.Value) Then
found = True
Exit For
End If
Next statename
I think my problem is that For Each loops don't select each value in the loop; I tried to account for that by selecting A2 before using INDEX(MATCH()) but apparently that'll only return the first match for each state name.
Any advice or guidance would be a lifesaver!