Hello and thank you for taking the time to read this and thank you for any advice!
I'm using VBA to search for the first value within an array which is greater than a value specified elsewhere in a cell, and outputting the column and row number to new cells.
The code I am using is:
Sub A()
Dim rng As Range
Dim v As Variant
Dim colnum As Long
Dim row As Long
Set rng = Range("B2:BT72")
v = Sheets("WIP").Range("AC76").Value
For Each cll In rng
If cll.Value > v Then
colnum = cll.Column
row = cll.row
Exit For
End If
Next
Sheets("WIP").Range("AF76").Value = colnum + 25
Sheets("WIP").Range("AG76").Value = row + 25
End Sub
And that works fine. However I now need to only search a certain column and just output the row value. The column I need to search is also specified in a specific cell. I've modified the code to:
Sub B()
Dim rng As Range
Dim v As Variant
Dim x As Variant
Dim row As Long
Set rng = Range("A2:BT72")
v = Sheets("WIP").Range("AC77").Value
x = Sheets("WIP").Range("AF77").Value
For Each cell In rng.Columns(x - 25)
If cell.Value > v Then
row = cell.row
Exit For
End If
Next
Sheets("WIP").Range("AG77").Value = row + 25
End Sub
This time I'm getting an error message type mismatch on the line "If cell.value > V Then" Can anybody spot why?
Thanks
The '.Cells' property needs to be specified for the range object in the second example:
Sub B()
Dim rng As Range
Dim v As Variant
Dim x As Variant
Dim row As Long
Set rng = Range("A2:BT72")
v = Sheets("WIP").Range("AC77").Value
x = Sheets("WIP").Range("AF77").Value
For Each cell In rng.Columns(x - 25).Cells
If cell.Value > v Then
row = cell.row
Exit For
End If
Next
Sheets("WIP").Range("AG77").Value = row + 25
End Sub
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.