RatExcel
Board Regular
- Joined
- Aug 24, 2014
- Messages
- 222
Hello Guys,
What is the fastest / most effective way to check if given value is in the given range?
Let's say I have a sheet with 10 000 rows of data in it. Let's supose I want to do something to cell in column B if corresponding cell in column A is equal to given value.
The method that I use most often is the below:
But if value "ABC" is not in column A the whole action is pointless and it takes time and resource if the worksheet has a lot of rows.
The other solution I can think of is the below:
But the code gets longer and less readible and I personally think this is not a good solution.
So is there any simpler, faster, more elegant way to determine if the given value is in the range?
I'm certain that there is and it is very obvious but I can't think of it. Would you help?
What is the fastest / most effective way to check if given value is in the given range?
Let's say I have a sheet with 10 000 rows of data in it. Let's supose I want to do something to cell in column B if corresponding cell in column A is equal to given value.
The method that I use most often is the below:
Code:
Sub Test()
For i = 1 To Cells(Rows.Count, 1).End(xlUp).Row
If Cells(i, 1) = "ABC" Then
Cells(i, 2) = "DEF"
End If
Next i
End Sub
But if value "ABC" is not in column A the whole action is pointless and it takes time and resource if the worksheet has a lot of rows.
The other solution I can think of is the below:
Code:
Sub Test()
On Error Resume Next
If WorksheetFunction.Match("ABC", Range(Cells(1, 1), Cells(Cells(Rows.Count, 1).End(xlUp).Row, 1)), 0) Then
If Err.Number > 0 Then
On Error GoTo 0
Err.Clear
GoTo NextStep
End If
On Error GoTo 0
For i = 1 To Cells(Rows.Count, 1).End(xlUp).Row
If Cells(i, 1) = "ABC" Then
Cells(i, 2) = "DEF"
End If
Next i
End If
NextStep:
End Sub
But the code gets longer and less readible and I personally think this is not a good solution.
So is there any simpler, faster, more elegant way to determine if the given value is in the range?
I'm certain that there is and it is very obvious but I can't think of it. Would you help?