I'm not sure why the following code gives a "Not found" Message when the value in the initial active cell (-15.0) is also a value within the specified range.
Sub Test()
Dim c As Double
Dim srchRng As Range
With ActiveWorkbook.ActiveSheet
c = ActiveCell.Select
Set srchRng = .Range("V17:V37").Find(what:=c)
If srchRng Is Nothing Then
MsgBox "Not found"
End If
End With
End Sub
Note sometimes a value of 0 exists within the specified range. If so I don't need to do anything but if 0 does not exist, I first need to find the least negative value (I use separately within the worksheet =MAX(IF(V17:V37<=0,V17:V37),MIN(V17:V37))
and this becomes my initial active cell value [eg -15.0] to look for in the specified range), and this least negative value will then be set to zero (by using a goal seek function in this cell)
So this macro will ultimately incorporate an If
statement or conditional lookup
Any feedback appreciated.
I made suggested changes but revised code below still returns "Not found". Cell V38 has the value -15.0 but this number does appear in the range V17:V37. This simple macro should result in the cell in the range corresponding to the c value being selected. Any further help appreciated.
Dim c As Double
Dim srchRng As Range
With ThisWorkbook.Sheets("Reel_Pack")
c = Range("V38")
Set srchRng = .Range("V17:V37").Find(what:=c)
If srchRng Is Nothing Then
MsgBox "Not found"
End If
End With
End Sub
I found the following code to do the above.
Dim c As Double
c = ThisWorkbook.Sheets("Sheet1").Range("V58").Value
Dim cel As Range
For Each cel In ThisWorkbook.Sheets("Sheet1").Range("V17:V57")
With cel
If .Value = c Then
.Select
End If
End With
Next cel
End Sub
But can someone suggest please how to improve this by actually incorporating the equation I have in cell V58 within this macro? As mentioned that equation is =MAX(IF(V17:V37<=0,V17:V37),MIN(V17:V37))
.
So the macro would look into the range V17:V37 and if there is an exact zero then Do Nothing otherwise locate the cell with the least negative number (which is what the equation above does) and select that cell.
c = ActiveCell
rather thanc = ActiveCell.Select
– Darren Bartrup-CookSelect
theActiveCell
. I'd go a step further and say changeActiveWorkbook.ActiveSheet
toThisWorkbook.Sheets("YourSheetName")
And if your function=MAX(IF...
is in a specific range (e.g. "V16"), refer to that range directly instead of usingActiveCell
. – BigBenFIND
parameters:LookIn:=xlValues, LookAt:=xlWhole
. The settings for LookIn, LookAt, SearchOrder, and MatchByte are saved each time you use this method. If you do not specify values for these arguments the next time you call the method, the saved values are used. – Darren Bartrup-Cook