I am trying to search through these rows in my excel sheet and look to see if the cell says one of the things in my array. If it does match one of my values in my array, I want it to go into the other array to the correct number, then pull that value and put in a different cell.
So I want it finding the word in my first array, figuring out where that is in the array, then taking the corresponding value from Array2 and putting that value in the wanted cell. Here is what I have so far but I am getting an error message on
If ws1.Cells(lngRow, "F").Value = "" And _
InStr(1, LCase(Range("J" & lngRow)), LCase(v)) <> 0 Then
It gives me a "Run-time error '424': Object required" Error. So I am not sure what I am doing wrong. Maybe I am not doing the code correctly at all but I thought it was something like this.
The arrays may not have the same amount of info in them right now because I took info out to make it smaller but my real one has the same number of values.
Dim lngRow As Long
Dim lngRows As Long
Dim arr As Variant, v As Variant
'
' This function will search for all Transfers.
' If the reason for action is ERROR, it will leave it. If it part of the list that needs changed, then it will make it blank or ""
' Otherwise it will leave it as it is.
'
'
ActiveSheet.AutoFilterMode = False
arr = Array("Atlanta", "Chicago", "Crown Point", _
"Dallas", "DC", "Equipment Company", "Denver", _
"Detroit", "Home Office", "Houston", "Kansas", _
"Las Vegas", "Louisville")
arr2 = Array("US Distribution", "US Products", "US Distribution", "US Distribution", "US Distribution", "US Distribution", "Equipment", "US Distribution", "US Products", "US Distribution", "Corporate", "US Distribution", "US Products", "US Distribution", "US Distribution", "US Distribution", "US Products", "US Products")
lngRows = Range("C" & Rows.Count).End(xlUp).Row
For lngRow = lngRows To 2 Step -1
For Each v In arr
If ws1.Cells(lngRow, "F").Value = "" And _
InStr(1, LCase(Range("J" & lngRow)), LCase(v)) <> 0 Then
ws1.Cells(lngRow, "I").Value = arr2(v)
End If
Next v
Next
End Sub
EDITED CODE DOWN BELOW
Dim wb As Workbook
Dim ws1 As Worksheet
Dim FFwb As Workbook
Dim FFws As Worksheet
Set wb = ActiveWorkbook
Set ws1 = wb.Sheets(1)
Dim lngRow As Long
Dim lngRows As Long
Dim arr As Variant, var As Variant
Dim arr2 As Variant
Dim locIdx As Variant
For locIdx = LBound(arr) To UBound(arr)
var = arr(locIdx)
For lngRow = lngRows To 2 Step -1
For Each var In arr
If ws1.Cells(lngRow, "F").Value = "" And _
InStr(1, LCase(ws1.Cells(lngRow, "I").Value), LCase(var)) <> 0 Then
ws1.Cells(lngRow, "H").Value = arr2(var)
End If
Next var
Next lngRow
Next locIdx
THIS IS MY CODE THAT WORKS DOWN BELOW. It doesn't work if the one cell is #N/A, but they shouldn't be #N/A. I had to change what arr2 was pulling from in the last line to locIdx instead of var.
Dim locIdx As Variant
For lngRow = lngRows To 2 Step -1
For locIdx = LBound(arr) To UBound(arr)
var = arr(locIdx)
If ws1.Cells(lngRow, "F").Value = "" And _
InStr(1, ws1.Cells(lngRow, "I").Value, var) <> 0 Then
ws1.Cells(lngRow, "H").Value = arr2(locIdx)
'ElseIf LCase(ws1.Cells(lngRow, "A").Value) = "transfer ft<>pt" And _
'InStr(1, LCase(Range("B" & lngRow)), LCase(v)) <> 0 Then
'ws1.Cells(lngRow, "B").Value = ""
End If
'If (LCase(ws1.Cells(lngRow, "A").Value) = "transfer" And _
' InStr(1, LCase(Range("B" & lngRow)), LCase("err")) <> 0) Then
' ws1.Cells(lngRow, "B").Value = "Error"
'End If
Next locIdx
Next lngRow
Thanks for the help in advance!
ws1?Option Explicitat the top of the module. The error changes to Variable not declared (or something similar) -ws1doesn't exist; you can't make these member calls on aVariantthat doesn't contain aWorksheetobject reference. You probably don't want to make member calls against aVariantanyway.Dim ws1 As WorksheetandSetit to whatever appropriateWorksheetinstance. Also, fix any other compile errors, and always use Option Explicit in the future. Cheers!exit forif you find a match.vis not a valid index to accessarr2Exit Forin just before theEnd If.