excel visual basic question

Status
Not open for further replies.

campitel

Ars Praetorian
402
Subscriptor
Howdy Folks,

I have a macro that shades vlaues in a table that are greater than a given number. It is used for comparing environmental chemistry data to governmental standards. for example:

code:<HR>
Code:
Chemcial     Standard     value1     value2     value3
benzene      5            <5         13         6 J

Each field represents a cell in an Excel worksheet. My macro will shade 13 since it is larger than 5. However, it will not shade "6 J" because it interprets that cell as containing text. By the way, the "J" means the value is estimated. Okay, here's my question:

Is there a way to find cells that have "J*" values in them, separate the numerical portion of the cell, and then use that numerical portion for comparisson? If so, I would be might psyched. I included my code below. Thank you in advance for any help.

code:<HR>
Code:
Sub shadeExceed()
    Dim data
    Dim standardColumn
    
'Dialog box that asks the user for the data range and the column in which the standards reside.

    Set data = Application.InputBox(prompt:="Select data range", Type:=8)
    standardColumn = Application.InputBox(prompt:="Enter letter of column that contains the standards", default:="B")
        

    For Each datum In data
        rownumber = datum.Row
        colnumber = datum.Column

'Shade cells that are numbers, not null, and greater than the standard.

            If datum > Cells(rownumber, standardColumn) And IsNumeric(datum) = True And Cells(rownumber, standardColumn) <> "" And IsNumeric(Cells(rownumber, standardColumn)) Then
                With datum.Interior
                    .ColorIndex = 15
                    .Pattern = xlSolid
                    .PatternColorIndex = xlAutomatic
                End With
                                
            End If

'-------------------------------
'This is where I need some help
'-------------------------------
'Rows were commented out cause they don't do what I need them to do.
           
'Conditional statement that shades any cells that have a J value, but do not have
'a less than sign in them

            'If datum Like ("*J") And Not (datum Like "<*") Then
                    'With datum.Interior
                        '.ColorIndex = 15
                        '.Pattern = xlSolid
                        '.PatternColorIndex = xlAutomatic
                    'End With
            'End If
    Next datum

End Sub
 
You should just be able to use the Right or Left functions that are native to the macro environment. It depends on if the "J" is the first character or the last character. Check them out in the Excel Help and you should be all set.

Example:

If "J" is always the last character ---
if Right(datum, 1) = "J" then
datum_number = CInt(Left(datum, Len(datum) - 1))


If "J" is always the first character ---
if Left(datum, 1) = "J" then
datum_number = CInt(Right(datum, Len(datum) - 1))
 

campitel

Ars Praetorian
402
Subscriptor
Cool, thank you. The "J" is almost always the last character, but there are rare exceptions. Your post got me thinking, however. There is usually a space between the J and the numerical value. Perhaps I could use a "search" command to provide the position of the space. That is, if "search" exists outside of the worksheet environment. Then I could employ "len" and friends. Many thanks. I will work on this puppy for a little while. Take care.
 
Status
Not open for further replies.