Hi,
I have the code below which looks for a min valve in a range which is working ok. The range of columns and rows does vary which I have sorted in the code.
At the moment the lowest value is highlighted on the sheet (data) and a Msgbox will report its location, i.e. $AA:$2000 or $BZ:$1000
What I need is too also report what text is in row 1 of found column, so msgbox will display the location and the cell text i.e. $BZ:$1000 & $BZ:$1 or $AA:$2000 & $AA:$1
The code I below I found through google, so I don't fully understand it but it does what I need so far.
Any ideas ??
I have the code below which looks for a min valve in a range which is working ok. The range of columns and rows does vary which I have sorted in the code.
At the moment the lowest value is highlighted on the sheet (data) and a Msgbox will report its location, i.e. $AA:$2000 or $BZ:$1000
What I need is too also report what text is in row 1 of found column, so msgbox will display the location and the cell text i.e. $BZ:$1000 & $BZ:$1 or $AA:$2000 & $AA:$1
The code I below I found through google, so I don't fully understand it but it does what I need so far.
Any ideas ??
Code:
Private Sub CommandButton7_Click()
'Determines smallest value in range, highlights it and returns its address
'Cells with dates also return a value, and get covered for determining smallest value. Percentages will convert and return numerics.
'Determines values from the active worksheet
Dim strData As String
Dim rng As Range
Dim vValue As Variant
Dim rngCol As Range
Dim lngRow As Long
Dim rngAdd As Range
Dim wsSheet As Worksheet
On Error Resume Next
Set wsSheet = Sheets("data")
On Error GoTo 0
If wsSheet Is Nothing Then MsgBox "Cell Data Has Not Been Loaded Yet!", vbExclamation: Exit Sub
Sheets("Data").Select
Set rng = Sheets("Data").Range("A3:CR" & Sheets("Data").Columns("A:CR").Find("*", , xlValues, , xlByRows, xlPrevious).Row)
'Determines smallest value in range
vValue = Application.WorksheetFunction.Min(rng)
For Each rngCol In rng.Columns
'Determines in case the smallest value exists in a particular column
If Application.WorksheetFunction.CountIf(rngCol, vValue) > 0 Then
'Returns row number of the smallest value, in the column which has the same
lngRow = Application.WorksheetFunction.Match(vValue, rngCol, 0)
'Returns cell address of the smallest value
Set rngAdd = rngCol.Cells(lngRow, 1)
'Selects smallest value to highlight with color
rngAdd.Select
With Selection
.Interior.Color = RGB(255, 255, 0)
End With
'Message displays the searched range, smallest value, and its address
MsgBox "Smallest Value in Range is " & vValue & ", in Cell " & rngAdd.Address & "."
MsgBox "Smallest Value in Range is " & vValue & ", in Column " & rngAdd.Column & "."
Exit Sub
End If
Next
End Sub