Displaying the contents of cell to a msgbox vba

Tech1_uk

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

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
32,151
Office Version
365
Platform
Windows
Maybe
Code:
MsgBox "Smallest Value in Range is " & vValue & ", in Cell " & Rngadd.Address & "." & vbLf & Cells(1, Rngadd.Column)
 

Tech1_uk

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

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
32,151
Office Version
365
Platform
Windows
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)
 

Tech1_uk

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

 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
32,151
Office Version
365
Platform
Windows
It's a linefeed character, just remove it from the code.
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
32,151
Office Version
365
Platform
Windows
You're welcome
 

Forum statistics

Threads
1,081,863
Messages
5,361,743
Members
400,654
Latest member
Pinaki Chatterjee

Some videos you may like

This Week's Hot Topics

  • populate from drop list with multiple tables
    Hi All, i have a drop list that displays data, what i want is when i select one of those from the list to populate text from different tables on...
  • Find list of words from sheet2 in sheet1 before a comma and extract text vba
    Hi Friends, Trying to find the solution on my task. But did not find suitable one to the need. Here is my query and sample file with details...
  • Dynamic Formula entry - VBA code sought
    Hello, really hope one of you experts can help with this - i've spent hours on this and getting no-where. .I have a set of data (more rows than...
  • Listbox Header
    Have a named range called "AccidentsHeader" Within my code I have: [CODE]Private Sub CommandButton1_Click() ListBox1.RowSource =...
  • Complex Heat Map using conditional formatting
    Good day excel world. I have a concern. Below link have a list of countries that carries each country unique data. [URL...
  • Conditional formatting
    Hi good morning, hope you can help me please, I have cells P4:P54 and if this cell is equal to 1 then i want row O to say "Fully Utilised" and to...
Top