0

So, i have a problem about selecting the exact cell of a formulated value. Im really new to VBA and i searched the internet for atleast 2 days for a valuable answer. Here let me show you the code im trying to put and explain what it does afterwards;

    Dim r As Range
    MinTime = WorksheetFunction.Min(Range("C5:L14"))
Set r = ActiveSheet.Cells.Find(MinTime)
r.Select
MinRow = ActiveCell.Row
MinCol = ActiveCell.Column

So what im trying to do is to find the minimum number in a range with worksheetfunction.min and select the cell which i 've just found. Then direct the row and column information to other 2 variable. But this code is not working always. Sometimes it works just fine sometimes it doesnt and i am clueless at the moment. Any kind of help would be appreciated. Thank you already.

  • You poor searcher... https://stackoverflow.com/questions/5215616/vba-cell-address-of-max – Gadziu Nov 03 '17 at 22:40
  • Couple of things, you should fully reference your ranges e.g. ThisWorkbook.Worksheets("Sheet1").Range........... You may need to handle if the value is not found as r will then be nothing. You are also searching the entire sheet but only expecting one match – QHarr Nov 03 '17 at 22:48
  • The problem is, it sometimes finds irrelivant numbers to the number it finds at MinTime. Lets say Mintime finds the number correctly (as 11)but when i use the Set r =activesheet.cells.find(MinTime) it finds (142.1254371). And i dont get it. – Oğuzhan Alptekin Nov 03 '17 at 23:00
  • Still open to answers. – Oğuzhan Alptekin Nov 04 '17 at 00:20

3 Answers3

1

I suspect that your range is actually containing times. The code below will find the earliest time (the lowest value in the range).

Private Sub MinTime()

    Dim Rng As Range
    Dim Mini As Range
    Dim MiniValue As Double
    Dim MinRow As Long, MinClm As Long

    Set Rng = ActiveSheet.Range("C5:L14")

    MiniValue = Application.Min(Rng)
    With Rng
        Set Mini = .Find(What:=MiniValue, _
                         After:=.Cells(1), _
                         LookIn:=xlFormulas, _
                         LookAt:=xlWhole, _
                         SearchOrder:=xlByRows, _
                         SearchDirection:=xlNext, _
                         MatchCase:=False, _
                         MatchByte:=False)
    End With

    With Mini
        MinRow = .Row
        MinClm = .Column
    End With
End Sub

The trick is to LookIn:=xlFormulas instead of the default xlValues. You may enter 12:00 but the cell's formula is 0.5. Presuming that to be the minimum in the range, when you look for 0.5 in the values you won't find it because the cell's "value" in this context (confusion by Microsoft, not my doing) is "12:00". Therefore you would have to convert the minimum value found back into a string, like "12:00", or you look for the 0.5 in the formulas.

Variatus
  • 14,293
  • 2
  • 14
  • 30
  • Worked like a charm. You have my eternal thanks man. Problem was looking for xlvalues when my texts were time. Thank you again for yor help, have agood one :) – Oğuzhan Alptekin Nov 04 '17 at 15:34
0

You can even do this without using the WorksheetFunction or the Find function. Making an array and then looping through it is a fairly easy way to find what you are looking for. Try this

Sub findMin(ByVal minRange As Range)
Dim searchArr() As Range
ReDim searchArr(1 To minRange.Rows.Count * minRange.Columns.Count) As Range
Dim minVal As Double
Dim i As Integer, i2 As Integer, outputRow As Integer, outputColumn As Integer
For i2 = 0 To minRange.Columns.Count
    For i = 0 To minRange.Rows.Count
        searchArr(i + 1) = Cells(minRange.Row + i, minRange.Column + i2).Value
    Next i
Next i2

    minVal = searchArr(1).Value
    For i = 1 To UBound(searchArr)
        If searchArr(i).Value < minVal Then
            minVal = searchArr(i).Value
            outputRow = searchArr(i).Row
            outputColumn = searchArr(i).Column
        End If
    Next i
End Sub
Jizzle21013
  • 21
  • 1
  • 1
  • 4
  • i appreciate the idea and your effort. Yeah it may work like that but its an assignment so i have to do it in a spesific way. And i dont really see my ones problem and it drives me crazy i tried every answer given here but yours since its too complex for me and teacher would understand it instantly. But thank you for it again. – Oğuzhan Alptekin Nov 04 '17 at 00:19
0

In your question's code, you're using the Find method on all of the worksheet's cells. As @Gary's Student's answer shows, you should invoke the Find method on the same range as the one within which you've found the Min.

Also, there are several parameters to the Find method that will make a difference, such as looking for whole values or not, looking within formulas or not, looking for a particular format or not. You can easily find information about the Find method on the 'net.

Here's my take on your code:

Sub MyAssignment()
    Dim searchRange As Range
    Dim minCell As Range
    Dim minTime As Double
    Dim minRow As Long
    Dim minCol As Long

    Set searchRange = ActiveSheet.Range("C5:L14")
    minTime = WorksheetFunction.Min(searchRange)

    Set minCell = searchRange.Find(minTime, searchRange.Cells(1), XlFindLookIn.xlValues, XlLookAt.xlWhole, MatchCase:=False, SearchFormat:=False)
    minCell.Select

    minRow = minCell.Row
    minCol = minCell.Column
End Sub

The parameters you use when invoking the Find method from VBA will be mirrored on the Find dialog as seen from Excel (Ctrl+F). Conversely, changing options on Excel's Find dialog will impact your next VBA's Find call because the parameters you don't supply to the Find method will take the values last used on the dialog.

Let us know how it turns out.

Excelosaurus
  • 2,789
  • 1
  • 14
  • 20
  • Now it says object variable or with block variable not set. I got your point now i'll check find method on net thank you btw. Let me know if you find whats wrong with the error :) I can show you the full code if you are intrested just send friendship from facebook. – Oğuzhan Alptekin Nov 04 '17 at 01:37
  • Did you copy & paste my code verbatim? I run it here and it works fine. Also please always say _where_ any code fails. – Excelosaurus Nov 04 '17 at 01:42
  • Yeah i copied and pasted it and changed variable names to fit my own. It gives error at minCell.Select. Because minCell is equal to 0 when the upper code is finished. – Oğuzhan Alptekin Nov 04 '17 at 01:45
  • Then your `minCell` variable is `Nothing` when you try to invoke its `Select` method. You're probably setting the result of the `Find` method to another variable. Look at my code, look at yours, put breakpoints, execute both step by step using the F8 key, hover your mouse over the variables to see their values, etc. Do your homework ;-) – Excelosaurus Nov 04 '17 at 01:48
  • 127,6035668 155,5996374 261,2719655 142,3371184 202,8136633 181,5528881 184,9361084 40,44437605 220,0611108 182,3976608 160,0785182 142,4342193 265,0912963 146,0878083 204,3578686 176,3872533 194,4086635 33,97961735 202,7361451 186,9472482 161,3697674 140,0743092 271,7175501 145,0436261 212,9574969 179,1607264 168,3274554 26,055447 191,8257958 170,6011316 131,3491774 145,3563413 283,7265125 139,3825584 218,7051327 194,3037201 186,9069942 27,15935139 221,3384705 181,6220334 163,0985244 154,0639554 269,7671612 144,8527593 211,9973883 192,5977296 184,9435968 37,76695262 210,9238662 – Oğuzhan Alptekin Nov 04 '17 at 01:52
  • These are the number is my range. I did gave the info that Mincell is equal to nothing (ok maybe i said 0 but i ment nothing :) ) Plus im doing it the way you are saying from the beginning and checking each variable value if its correct. It works fine till the Set mincell code. Mintime value is right and im not appointing anything to my mintime value before this. There is only For i loop beginning before these codes. and Dim as lines. – Oğuzhan Alptekin Nov 04 '17 at 01:55
  • I could only show you some of those numbers btw. The code i wrote works fine with these kind of numbers. But when i put say like "15" to one of these cells then Mintime finds 15 and my "r" finds something else that is irrelivant. Im really at lost here. – Oğuzhan Alptekin Nov 04 '17 at 01:58