Displaying the contents of cell to a msgbox vba

Tech1_uk

New Member
Joined
Sep 22, 2018
Messages
17
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 ??

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
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
Maybe
Code:
MsgBox "Smallest Value in Range is " & vValue & ", in Cell " & Rngadd.Address & "." & vbLf & Cells(1, Rngadd.Column)
 
Upvote 0
Thank you very much, works a treat.

I think at times I spend to long looking to deep and just confuse myself.

Now for the next task which is to take the result of that msgbox and populate a textbox on a userform with it ……………..:confused:
 
Upvote 0
If the button is on the userform then
Code:
Textbox1.value="Smallest Value in Range is " & vValue & ", in Cell " & Rngadd.Address & "." & vbLf & Cells(1, Rngadd.Column)
 
Upvote 0
I had just got to that when you posted but thanks.

I have something strange though, in the textbox the part of the code
& vbLf & Cells(1, Rngadd.Column) it puts in front like a back to front P| in front of the text from the cell but it doesn't in the message box.
I'm guessing that is something to do with how the row 1 cell is formatted ??

 
Upvote 0
It's a linefeed character, just remove it from the code.
 
Upvote 0

Forum statistics

Threads
1,223,723
Messages
6,174,123
Members
452,546
Latest member
Rafafa

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top