How to import from CSV and add containing numbers together? - arrays

I'm having issues reading a CSV file to then add containing numbers together.
main.au3
$billsRawData1 = FileReadLine(#AppDataDir & "\testDir\test.csv", 2)
$billsArray1 = StringSplit($billsRawData1, ",")
$billsRawData2 = FileReadLine(#AppDataDir & "\testDir\test.csv", 3)
$billsArray2 = StringSplit($billsRawData2, ",")
$billsRawData3 = FileReadLine(#AppDataDir & "\testDir\test.csv", 4)
$billsArray3 = StringSplit($billsRawData3, ",")
$billsRawData4 = FileReadLine(#AppDataDir & "\testDir\test.csv", 5)
$billsArray4 = StringSplit($billsRawData4, ",")
$billsRawData5 = FileReadLine(#AppDataDir & "\testDir\test.csv", 6)
$billsArray5 = StringSplit($billsRawData5, ",")
$billsRawData6 = FileReadLine(#AppDataDir & "\testDir\test.csv", 7)
$billsArray6 = StringSplit($billsRawData6, ",")
$billsRawData7 = FileReadLine(#AppDataDir & "\testDir\test.csv", 8)
$billsArray7 = StringSplit($billsRawData7, ",")
$billsRawData8 = FileReadLine(#AppDataDir & "\testDir\test.csv", 9)
$billsArray8 = StringSplit($billsRawData8, ",")
$billsRawData9 = FileReadLine(#AppDataDir & "\testDir\test.csv", 10)
$billsArray9 = StringSplit($billsRawData9, ",")
$billsRawData10 = FileReadLine(#AppDataDir & "\testDir\test.csv", 11)
$billsArray10 = StringSplit($billsRawData10, ",")
$billsRawData11 = FileReadLine(#AppDataDir & "\testDir\test.csv", 12)
$billsArray11 = StringSplit($billsRawData11, ",")
$billsRawData12 = FileReadLine(#AppDataDir & "\testDir\test.csv", 13)
$billsArray12 = StringSplit($billsRawData12, ",")
$billsRawData13 = FileReadLine(#AppDataDir & "\testDir\test.csv", 14)
$billsArray13 = StringSplit($billsRawData13, ",")
$billsRawData14 = FileReadLine(#AppDataDir & "\testDir\test.csv", 15)
$billsArray14 = StringSplit($billsRawData14, ",")
$billsRawData15 = FileReadLine(#AppDataDir & "\testDir\test.csv", 16)
$billsArray15 = StringSplit($billsRawData15, ",")
$billsRawData16 = FileReadLine(#AppDataDir & "\testDir\test.csv", 17)
$billsArray16 = StringSplit($billsRawData16, ",")
$billsRawData17 = FileReadLine(#AppDataDir & "\testDir\test.csv", 18)
$billsArray17 = StringSplit($billsRawData17, ",")
$billsRawData18 = FileReadLine(#AppDataDir & "\testDir\test.csv", 19)
$billsArray18 = StringSplit($billsRawData18, ",")
$billsRawData19 = FileReadLine(#AppDataDir & "\testDir\test.csv", 20)
$billsArray19 = StringSplit($billsRawData19, ",")
$billsRawData20 = FileReadLine(#AppDataDir & "\testDir\test.csv", 21)
$billsArray20 = StringSplit($billsRawData20, ",")
$total = $billsArray1[3] + $billsArray2[3]; + $billsArray3[3] + $billsArray4[3] + $billsArray5[3] + $billsArray6[3] + $billsArray7[3] + $billsArray8[3] + $billsArray9[3] + $billsArray10[3] + $billsArray11[3] + $billsArray12[3] + $billsArray13[3] + $billsArray14[3] + $billsArray15[3]; + $billsArray16[3] + $billsArray17[3] + $billsArray18[3] + $billsArray19[3] + $billsArray20[3]
MsgBox(0, "", $total)
test.csv
,,,
10/04/2015, Internet,$40 , Monthly
10/07/2015, Gas,$80 , Monthly
10/01/2015, Cable,$60 , Monthly
10/27/2015, Storage,$50 , Monthly
10/30/2015,School,$150 , Monthly
10/18/2015,Rent,$750 ,Monthly
test.csv contains variable amount of rows. I'm trying to add the value in the third column. I read each line into a variable and split that into an array ($billsArray1[3] returns $40). I then add these variables together, but it returns 0 for $total.
1) Why is $total returning 0 instead of the total?
2) How to create a loop to assign the variables (so I don't need to create new variables for each line, as I don't know how many lines there will be)? Note* it always skips the first line so it starts at line 2.
3) How to create another loop to add the data in the third column together?

I wouldn't create a separate array. I'd just loop over the file, grab the 3rd entry, strip everything that isn't a number (like $) and then add that to the total...
$file = FileOpen(#AppDataDir & "\testDir\test.csv", 0)
$total = 0
While 1
$line = FileReadLine($file)
If #error = -1 Then ExitLoop
;Get the 3rd "token" and strip everything that is not a number.
$bill = StringRegExpReplace(StringSplit($line, ',')[3], "[^\d]", "")
;Add current bill to total.
$total = $total + $bill
WEnd
FileClose($file)
MsgBox(0, "Total", $total)

Related

Add items to an array rather than replacing

I have some code which uses arrays to get data from one worksheet (Cost Data) and essentially copy it to another worksheet (Compare Tool) in a side-by-side, comparison format. I got everything to work only to find out that in some cases, there are multiple lines of data on the Cost Data worksheet which meet the criteria. When these lines of data are assigned to my outarr array, it overwrites what was in there rather than adding to it.
I tried using the ReDim Preserve; however, I'm still getting an error. Any suggestions?
Sub Compare_Projects_Arrays()
Dim Toolary As Variant, Data_ary As Variant, PrjTitle_ary As Variant, CurrentAry As Variant, outarr As Variant
Dim r As Long, nr As Long, x As Long, c As Long, CurrentCostCod As Long
Dim Cl As Range
Dim Project1 As String, Project2 As String, Project3 As String, Project4 As String, Project5 As String, Project6 As String, Project7 As String, Project8 As String
Application.ScreenUpdating = False
On Error Resume Next
Sheets("Compare Tool").ShowAllData
Sheets("Cost Data").ShowAllData
Sheets("Compare Tool").Range("Clear_Cells").SpecialCells(xlConstants).ClearContents
Sheets("Compare Tool").Range("AD15:AD16,AD19,AQ15:AD16,AQ19,BD15:BD16,BD19,BQ15:BQ16,BQ19,CD15:CD16, CD19,CQ15:CQ16, CQ19,DD15:DD16, DD19,DQ15:DQ16,DQ19").ClearContents
Sheets("Compare Tool").Range("X23:DW24").ClearContents
On Error GoTo 0
With Sheets("Setup Page")
Typology = .Range("L18")
Project1 = .Range("U11").Value
Project2 = .Range("U12").Value
Project3 = .Range("U13").Value
Project4 = .Range("U14").Value
Project5 = .Range("U15").Value
Project6 = .Range("U16").Value
Project7 = .Range("U17").Value
Project8 = .Range("U18").Value
End With
With Sheets("Compare Tool")
Set SearchRangeTool = .Range("E:E").Find(What:="Last Row")
LastRowTool = SearchRangeTool.Row
If Project1 <> "" Then
.Range("X23") = Project1
.Range("X24") = "Typology: " & Typology
End If
If Project2 <> "" Then
.Range("AK23") = Project2
.Range("AK24") = "Typology: " & Typology
End If
If Project3 <> "" Then
.Range("AX23") = Project3
.Range("AX24") = "Typology: " & Typology
End If
If Project4 <> "" Then
.Range("BK23") = Project4
.Range("BK24") = "Typology: " & Typology
End If
If Project5 <> "" Then
.Range("BX23") = Project5
.Range("BX24") = "Typology: " & Typology
End If
If Project6 <> "" Then
.Range("CK23") = Project6
.Range("CK24") = "Typology: " & Typology
End If
If Project7 <> "" Then
.Range("CX23") = Project7
.Range("CX24") = "Typology: " & Typology
End If
If Project8 <> "" Then
.Range("DK23") = Project8
.Range("DK24") = "Typology: " & Typology
End If
End With
'Put data into the arrarys (Toolary & Data_ary)
Data_ary = Sheets("Cost Data").Range("A1").CurrentRegion.Value2
With Sheets("Compare Tool")
Toolary = .Range("A28:DV" & .Range("U" & Rows.Count).End(xlUp).Row).Value2
End With
'Project 1
'Check if Project field is blank
If Sheets("Setup Page").Range("U11") = "" Then GoTo Project2
With Sheets("Cost Data")
FirstRowDB = .Range("A:A").Find(What:=Project1, LookIn:=xlValues, SearchDirection:=xlNext).Row 'xlNext starts from top
GSFPrj = .Cells(FirstRowDB, 13)
GSFTypology = .Cells(FirstRowDB, 18)
End With
'Copy the GSF area & Total Project cost and paste into the top of the "Compare Tool" tab
Sheets("Prj Info").Select
FindPrj = Application.Match(Project1, Range("A:A"), 0)
Total_Prj_Cost = Sheets("Prj Info").Cells(FindPrj, 16)
Sheets("Compare Tool").Range("AD19") = GSFTypology
Sheets("Compare Tool").Range("AD16") = GSFPrj
Sheets("Compare Tool").Range("AD15") = Total_Prj_Cost
lastrow = UBound(Toolary)
outarr = Worksheets("Compare Tool").Range("X28:AI" & lastrow)
'The following will put the formulas from the subtotals lines into the "toolfrom" array and then put it into the "outarr" array
With Sheets("Compare Tool")
toolfrom = .Range("X28:AI" & lastrow).formula
End With
For i = 1 To UBound(outarr, 1)
For j = 1 To UBound(outarr, 2)
If Left(toolfrom(i, j), 1) = "=" Then 'erroring out at i=1 and j=10
outarr(i, j) = toolfrom(i, j)
End If
Next j
Next i
For r = 1 To lastrow
If Toolary(r, 5) = "Single" Or Toolary(r, 5) = "T2 Head" Then
CurrentCostCode = Toolary(r, 21)
CurrentT0 = Toolary(r, 9)
ReDim Preserve outarr(r) 'This is where the error happens
For x = 2 To UBound(Data_ary)
If Data_ary(x, 1) = Project1 And Data_ary(x, 34) = CurrentCostCode And Data_ary(x, 22) = CurrentT0 And Data_ary(x, 17) = Typology Then
outarr(r, 1) = Data_ary(x, 37) 'This is where the data is getting overwritten
outarr(r, 2) = Data_ary(x, 38) 'This is where the data is getting overwritten
outarr(r, 3) = Data_ary(x, 39) 'This is where the data is getting overwritten
outarr(r, 4) = Data_ary(x, 40) 'This is where the data is getting overwritten
outarr(r, 5) = Data_ary(x, 41) 'This is where the data is getting overwritten
outarr(r, 6) = Data_ary(x, 42) 'This is where the data is getting overwritten
outarr(r, 7) = Data_ary(x, 43) 'This is where the data is getting overwritten
If Data_ary(x, 44) <> "" Then
outarr(r, 8) = Data_ary(x, 44) 'This is where the data is getting overwritten
outarr(r, 9) = Data_ary(x, 45) 'This is where the data is getting overwritten
End If
End If
Next x
End If
Next r
Worksheets("Compare Tool").Range("X28:AF" & lastrow) = outarr
'Project 2
Project2:
Application.ScreenUpdating = True
End Sub

why does VBA exit my for loop early and why aren't cell values being stored in my array properly?

I wrote this macro to pull data from a separate workbook to do some string manipulation. (Not shown) Eventually the code uses the data stored in the arrays created to create work instructions. Code shown below with one section working as intended (bend array creation), the second section will not (csk array creation). When I step through the code it will not go to the 'next i' and exits the if statement entirely on the first iteration.
nbends = Application.WorksheetFunction.CountIf(Range("M6:M" & lastrow), "BEND RADIUS")
ncsksf = Application.WorksheetFunction.CountIf(Range("M6:M" & lastrow), "CSK FARSIDE")
ncsksn = Application.WorksheetFunction.CountIf(Range("M6:M" & lastrow), "CSK NEARSIDE")
ReDim Bends(1 To nbends, 1 To 5)
ReDim Csksn(1 To nscksn, 1 To 3)
'' Bend array creation
On Error Resume Next
Set r = Range("M6:M" & lastrow).Find(what:="BEND RADIUS", LookIn:=xlValues)
If Not r Is Nothing Then
firstaddress = r.Address
addressrow = Right(firstaddress, 2)
Bends(1, 1) = Range("A" & addressrow)
Bends(1, 2) = Range("C" & addressrow)
Bends(1, 3) = Range("A" & addressrow + 1)
Bends(1, 4) = Range("M" & addressrow + 1)
Bends(1, 5) = Range("B" & addressrow + 1)
Do
For i = LBound(Bends) + 1 To UBound(Bends)
Set r = Range("M" & addressrow & ":M" & lastrow).Find(what:="BEND RADIUS", LookIn:=xlValues)
firstaddress = r.Address
addressrow = Right(firstaddress, 2)
Bends(i, 1) = Range("A" & addressrow)
Bends(i, 2) = Range("C" & addressrow)
Bends(i, 3) = Range("A" & addressrow + 1)
Bends(i, 4) = Range("M" & addressrow + 1)
Bends(i, 5) = Range("B" & addressrow + 1)
Next i
Loop While Not r Is Nothing And r.Address <> firstaddress
End If
'' Csks array creation
On Error Resume Next
Set r = Range("M6:M" & lastrow).Find(what:="CSK NEARSIDE", LookIn:=xlValues)
If Not r Is Nothing Then
firstaddress = r.Address
addressrow = Right(firstaddress, 2)
Csksn(1, 1) = Range("A" & addressrow)
Csksn(1, 2) = Range("B" & addressrow)
Csksn(1, 3) = Range("M" & addressrow)
Debug.Print addressrow
Do
For i = LBound(Csksn) + 1 To UBound(Csksn)
Set r = Range("M" & addressrow & ":M" & lastrow).Find(what:="CSK NEARSIDE", LookIn:=xlValues)
firstaddress = r.Address
addressrow = Right(firstaddress, 2)
Csksn(i, 1) = Range("A" & addressrow)
Csksn(i, 2) = Range("B" & addressrow)
Csksn(i, 3) = Range("M" & addressrow)
Debug.Print r
Debug.Print firstaddress
Debug.Print addressrow
Next i
Loop While Not r Is Nothing And r.Address <> firstaddress
End If
I fixed this by changing the for loop parameters to
For i = 2 To ncsksn
Set r = Range("M" & addressrow & ":M" & lastrow).Find(what:="CSK NEARSIDE", LookIn:=xlValues)
firstaddress = r.Address
addressrow = Right(firstaddress, 2)
Csksn(i, 1) = Range("A" & addressrow)
Csksn(i, 2) = Range("B" & addressrow)
Csksn(i, 3) = Range("M" & addressrow)
Debug.Print r
Debug.Print firstaddress
Debug.Print addressrow
Next i
Loop While Not r Is Nothing And r.Address <> firstaddress
End If
and it will print the values for each variable (r, firstaddress, addressrow) at each iteration of the for loop; but when i go to print the array using this loop nothing appears.
For i = LBound(Csksn) To UBound(Csksn)
For j = LBound(Csksn) To UBound(Csksn)
Debug.Print i, j, Csksn(i, j)
Next j
Next i
What am I missing here?
Thank you all for your knowledge

Excel VBA - Issues with For Each Loop with an Array

Hoping I can find some help with this one. I am pulling in information from Excel from 2 columns (Hostname, IP). The declarations of importance are
Dim HWSWArray() As Variant
Dim v As Variant.
My HWSWArray is loaded and as an example I have HWSWArray(0)(0) -> Hostname and HWSWArray(0)(1) -> IP
I want to loop through these and do an external comparison to another string so I have my loop written as such
For Each v In HWSWArray
If v(0) = POAMHost Then
DoThings
ElseIf v(1) = POAMHost Then
Do Things
End If
Next
I am getting a type mismatch as soon as it evaluates the first if statement. I am confused because they are both variants v and HWSWArray and shouldn't v as a variant match whatever type it is matching against?
Maybe I am missing the forest for the trees? I can post the full code if necessary but will need to redact some stuff.
To add some more information with code that I can easily share, I have done something similar in the past that has worked and I cant tell what the difference is. I will look at implementing your suggestions for the For i loop if I can't get this to work but I am trying to see why it wont work in its current capacity.
'Load BArray
Do While Counter <> B1930Rows + 1
IPCell = Assets.Range("X" & Counter)
hostCell = Assets.Range("W" & Counter)
If IsEmpty(Assets.Range("W" & Counter)) = True And IsEmpty(Assets.Range("X" & Counter)) = True Then
Counter = Counter + 1
ElseIf IsEmpty(Assets.Range("W" & Counter)) = True And IsEmpty(Assets.Range("X" & Counter)) = False Then
BArray(i) = Array("Null", UCase(IPCell))
i = i + 1
Counter = Counter + 1
ElseIf IsEmpty(Assets.Range("W" & Counter)) = False And IsEmpty(Assets.Range("X" & Counter)) = True Then
BArray(i) = Array(UCase(hostCell), "Null")
i = i + 1
Counter = Counter + 1
ElseIf IsEmpty(Assets.Range("W" & Counter)) = False And IsEmpty(Assets.Range("X" & Counter)) = False Then
BArray(i) = Array(UCase(IPCell), UCase(hostCell))
i = i + 1
Counter = Counter + 1
Else
Counter = Counter + 1
End If
Loop
'MsgBox ("Here")
'Setting up script to handle the POAM Analysis Portion
Counter = 2
Dim statusCell As String
'Dim hostCell As String
Do While Counter <> POAMRows + 1
statusCell = POAM.Range("M" & Counter)
hostCell = POAM.Range("AE" & Counter)
If statusCell = "Ongoing" Then
For Each v In BArray
If v(0) = hostCell Then
Output.Range("A" & SummaryCounter) = POAM.Range("A" & Counter)
Output.Range("B" & SummaryCounter) = POAM.Range("C" & Counter)
Output.Range("C" & SummaryCounter) = POAM.Range("D" & Counter)
Output.Range("D" & SummaryCounter) = POAM.Range("E" & Counter)
Output.Range("E" & SummaryCounter) = POAM.Range("F" & Counter)
Output.Range("F" & SummaryCounter) = POAM.Range("G" & Counter)
Output.Range("G" & SummaryCounter) = POAM.Range("R" & Counter)
Output.Range("H" & SummaryCounter) = POAM.Range("V" & Counter)
Output.Range("I" & SummaryCounter) = POAM.Range("AB" & Counter)
Output.Range("J" & SummaryCounter) = POAM.Range("AE" & Counter)
SummaryCounter = SummaryCounter + 1
ElseIf v(1) = hostCell Then
Output.Range("A" & SummaryCounter) = POAM.Range("A" & Counter)
Output.Range("B" & SummaryCounter) = POAM.Range("C" & Counter)
Output.Range("C" & SummaryCounter) = POAM.Range("D" & Counter)
Output.Range("D" & SummaryCounter) = POAM.Range("E" & Counter)
Output.Range("E" & SummaryCounter) = POAM.Range("F" & Counter)
Output.Range("F" & SummaryCounter) = POAM.Range("G" & Counter)
Output.Range("G" & SummaryCounter) = POAM.Range("R" & Counter)
Output.Range("H" & SummaryCounter) = POAM.Range("V" & Counter)
Output.Range("I" & SummaryCounter) = POAM.Range("AB" & Counter)
Output.Range("J" & SummaryCounter) = POAM.Range("AE" & Counter)
SummaryCounter = SummaryCounter + 1
End If
Next
Counter = Counter + 1
Else
Counter = Counter + 1
End If
Loop
For Each and multidimensional arrays are tricky. Try this instead:
For i = LBound(HWSWArray, 1) To UBound(HWSWArray, 1)
If HWSWArray(i, 0) = POAMHost Then
'Do Things
ElseIf HWSWArray(i, 1) = POAMHost Then
'Do Things
End If
Next
LBound and UBound will give to the lower and upper bound for a given dimension, in this case the first.
Assuming you have equal length columns:
Dim HWSWArray() As Variant
HWSWArray = Range(Cells(startrow, startcolumn), Cells(endrow, endcolumn)).value
Dim i As Long
For i = lbound(HWSWArray) to ubound(HWSWArray)
If HWSWArray(v,1) = POAMHost Then
'DoThings
ElseIf HWSWArray(v,2) = POAMHost Then
'Do other Things
End If
Next i
the lowerbound (typically 0) and upper bound of the HWSWArray will be the row counts for the specific column.
To anyone looking at this in the future, I had incorrectly specified the dimensions of my array. I had extended my array to hold 3521 values but when it was loading the array, it only loaded to 3520.
When my code was running, it was comparing a variable to an empty array so I was getting a type mismatch.
I fixed the dimensions and everything worked fine.

Trying to compare a value to multiple, multiple dimension arrays

I have a spreadsheet of data in multiple columns. In VBA, I am trying to set a value in each row depending on the data in any one of three of the other columns. It will always be an OR comparison between those data and will need to return one of five values.
So I have created five arrays (they are multidimensional due to reading them in as ranges, however, they could be one dimensional if needed, I suppose), and I was going to compare the three columns to those arrays using a bunch of IF-THEN statements to return the necessary value.
Creating the arrays was easy enough, but I have no idea how to create the IF-THEN process correctly. An example of the process would be something like:
IF A1 is in ArrayA THEN
D1="Dog"
ELSEIF A1 is in ArrayB THEN
D1="Cat"
ELSEIF B1 is in ArrayC THEN
D1="Bird"
ELSEIF B1 is in ArrayD THEN
D1="Monkey"
ELSEIF C1 is in ArrayE THEN
D1="Blue"
ELSE
D1="Other"
I am not sure if this is the most efficient way to accomplish what I am trying to do, so I am definitely open to suggestions for a different approach. Thank you.
Ok, here is how I got it working. Most of this came from another person, but they deleted their comment so I can't thank them.
arrCols = Array(arrA, arrB, arrC, arrD, arrE)
arrVals = Array("Dog", "Cat", "Bird", "Monkey", "Blue")
For i = 2 To rCnt
pID = Cells(i, 2).Value
pName = Cells(i, 3).Value
pGroup = Cells(i, 4).Value
ans = ""
For j = LBound(arrCols) To UBound(arrCols)
If Not IsError(Application.Match(pGroup, arrCols(j), 0)) Then
ans = arrVals(j)
Exit For
ElseIf Not IsError(Application.Match(pName, arrCols(j), 0)) Then
ans = arrVals(j)
Exit For
ElseIf Not IsError(Application.Match(pID, arrCols(j), 0)) Then
ans = arrVals(j)
Exit For
End If
Next j
Cells(i, 5) = IIf(ans = "", "Other", ans)
Next i
I created an array of arrays to search through. The i loop cycles through all rows. The j loop cycles through the arrays within the main array. The three IF statements are needed for checking the three different columns. The returned value is defaulted to "Other" unless it is found in one of the arrays.
The only thing I was able to add to this code was the stuff needed to check three different columns in the row.
Here is an example of how to do it without looping
If Len("," & Join(ArrayA, ",") & ",") <> Len("," & Replace(Join(ArrayA, ","), "," & Range("A1").Value & ",", "") & ",") Then
Range("D1").Formula = "Dog"
ElseIf Len("," & Join(ArrayB, ",") & ",") <> Len("," & Replace(Join(ArrayB, ","), "," & Range("A1").Value & ",", "") & ",") Then
Range("D1").Formula = "Cat"
ElseIf Len("," & Join(ArrayC, ",") & ",") <> Len("," & Replace(Join(ArrayC, ","), "," & Range("A1").Value & ",", "") & ",") Then
Range("D1").Formula = "Bird"
ElseIf Len("," & Join(ArrayD, ",") & ",") <> Len("," & Replace(Join(ArrayD, ","), "," & Range("A1").Value & ",", "") & ",") Then
Range("D1").Formula = "Monkey"
ElseIf Len("," & Join(ArrayE, ",") & ",") <> Len("," & Replace(Join(ArrayE, ","), "," & Range("A1").Value & ",", "") & ",") Then
Range("D1").Formula = "Blue"
Else
Range("D1").Formula = "Other"
End If
Join the array to a string then check the len against the len of the string with the keyword (Range("A1")) replaced with nothing. Put a comma on either side to make sure it is the full word (Don't want something like Catfish to return as a Cat)
And the same thing using InStr instead:
If InStr(1, "," & Join(ArrayA, ",") & ",", Range("A1").Value) > 0 Then
Range("D1").Formula = "Dog"
ElseIf InStr(1, "," & Join(ArrayB, ",") & ",", Range("A1").Value) > 0 Then
Range("D1").Formula = "Cat"
ElseIf InStr(1, "," & Join(ArrayC, ",") & ",", Range("A1").Value) > 0 Then
Range("D1").Formula = "Bird"
ElseIf InStr(1, "," & Join(ArrayD, ",") & ",", Range("A1").Value) > 0 Then
Range("D1").Formula = "Monkey"
ElseIf InStr(1, "," & Join(ArrayE, ",") & ",", Range("A1").Value) > 0 Then
Range("D1").Formula = "Blue"
Else
Range("D1").Formula = "Other"
End If

Runtime error 91

I'm receiving the error Runtime 91 error on the line r = Bcell.Row. How do I fix the error?
Trying to define a range to perform a few checks.
Dim LastRow, SECTYPE, Bcell, r As Range
LastRow = ActiveSheet.Range("D65536").End(xlUp).Row()
Rows("1:1").Select
Selection.AutoFilter
ActiveSheet.Range("A1:L" & LastRow).AutoFilter Field:=10, Criteria1:="X"
If LastRow > 1 Then
Set SECTYPE = ActiveSheet.Range("D2:D" & LastRow)
For Each Bcell In SECTYPE
r = Bcell.Row
If Trim(Bcell.Value) = "CO" Or Trim(Bcell.Value) = "PO" Then
If (Trim(Bcell.Offset(0, -1).Value) = "SWAPOPT") And UCase(Trim(ActiveSheet.Range("i" & r).Value)) = "X" Or UCase(Trim(ActiveSheet.Range("i" & r).Value)) = "" Then
ActiveSheet.Range("J" & r).Value = "-"
ActiveSheet.Range("J" & r).Interior.ColorIndex = xlNone
End If
ElseIf Trim(Bcell.Value) = "FU" Then
If (Trim(Bcell.Offset(0, 1).Value) = 1 And InStr(Trim(Bcell.Offset(0, -2).Value), "IB") <> 0) Then
ActiveSheet.Range("J" & r).Value = "-"
ActiveSheet.Range("J" & r).Interior.ColorIndex = xlNone
End If
ElseIf Trim(Bcell.Value) = "OS" Then
If (Trim(Bcell.Offset(0, 2).Value) <> "AUD") Then
ActiveSheet.Range("J" & r).Value = "-"
ActiveSheet.Range("J" & r).Interior.ColorIndex = xlNone
End If
End If
Next Bcell
End If
You need to Set the variable like so:
Set r = Bcell.Row

Resources