comparing a number in a string

kolarbr

New Member
Joined
Jul 27, 2011
Messages
15
I want to compare a number in some text box to another number that is in a string. For example I have a "control" number that needs to be compared to all the other numbers in the column and if its greater than the control number, then change the fill color in the box to another color

Control #
0.33

Rest of Column
0.041 J
<0.5
NA
0.51
0.02 J etc...

Is there a way I can compare the control number to the each of the numbers in the rest of the column without changing or parsing that value? I know how to change the color and loop but don't know how to do the compare without changing anything or making a new column and hiding it. Thanks!!!!!!!!
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
You can use a VBA code with 2 For... Next cicle to compare Control number and other numbers in column
 
Upvote 0
I supposed your column is A and your Ceck number is in B1. In a Vba module copy this code:
Code:
Function EXTRACT_NUMBERS(s As Range) As Double
Dim T As String
Dim C As String
Dim i As Integer
Dim IsNumber As Boolean
Application.Volatile True
T = s.Value
C = ""
IsNumber = False
For i = 1 To Len(T)
If Mid(T, i, 1) = "," And IsNumber = True Then
C = C + ","
IsNumber = False
End If
On Error Resume Next
If Mid(T, i, 1) <= "9" And Mid(T, i, 1) >= "0" Then
C = C + Mid(T, i, 1)
IsNumber = True
Else
IsNumber = False
End If
Next i
EXTRACT_NUMBERS = CDbl(C)
On Error GoTo 0
End Function
 
------------------------
 
Sub test()
n = Range("A" & Rows.Count).End(xlUp).Row
For r = 1 To n
  If EXTRACT_NUMBERS(Range("A" & r)) > Range("B1") Then
     Range("A" & r).Interior.ColorIndex = 3
  End If
Next
End Sub
Run Sub test.
Enjoy!!
 
Upvote 0
In your rest of column Example, 0.041 J
Is the J in the same cell as the 0.041 or a Separate column?
 
Upvote 0
It's the same column...There are no longer any characters before the number it is simply: a number only, or a number followed by some letter(s) with a space in between

ex.
0.041
0.041 J
0.041 UJ
0.041 JU
0.041 U
 
Last edited:
Upvote 0
@Hotpepper Thanks for the reply but it did not help. I could not incorporate that into the code i already had. The value in which it starts is not "column A" it is defined in my array that I made so it varies...Cells(i, j).Value
Thanks though
 
Upvote 0
Here is what I have so far...

Sub Highlight()

Dim LastCol As Integer
Dim LastRow As Integer


'Find the last used column in a Row
With ActiveSheet
LastCol = .Cells(1, .Columns.Count).End(xlToLeft).Column
End With

'Find the last used row in a Column
With ActiveSheet
LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
End With


For i = 1 To LastCol
If Cells(1, i).Value = "1,1,1-Trichloroethane" Then
For j = 1 To LastRow
'Do some compare here
'The below line obviously doesn't work because the value contains a character
If Cells(j, i).Value > 0.33 Then
'Highlight Cell Some color
End If
Next j
End If
Next i

End Sub
 
Upvote 0
Can anyone see if they can fix the "find" function code on this...to my understanding vba code for the find function is different than if you type it into a cell in the document. Thanks!

Sub Highlight()

Dim LastCol As Integer
Dim LastRow As Integer
Dim FindSpace As Integer
Dim Value As Integer


'Find the last used column in a Row
With ActiveSheet
LastCol = .Cells(1, .Columns.Count).End(xlToLeft).Column
End With

'Find the last used row in a Column
With ActiveSheet
LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
End With


For i = 1 To LastCol
If Cells(1, i).Value = "1,1,1-Trichloroethane" Then
For k = 1 To LastRow
'Used the -1 to get the value I need for the Left function to work properly
FindSpace = Find(" ", Cells(j, i).Value, 1) - 1
Value = Left(Cells(k, i).Value, FindSpace)
If Value > 0.33 Then
Cells(k, i).Interior.Color = 15
End If
Next k
End If
Next i

End Sub
 
Upvote 0
Well thanks for trying everybody but I figured it out. For your future reference, turns out there is a built in function "val()" that returns the numbers in a string. You can simply save that to a variable and do the compare easily without changing the data in the cell. Who'd a thunk it...
 
Upvote 0

Forum statistics

Threads
1,224,567
Messages
6,179,571
Members
452,927
Latest member
whitfieldcraig

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