If in VBA

ninjagrover

New Member
Joined
Nov 6, 2008
Messages
15
Hi All.

Been teaching myself VBA and I've gotten stuck, and no matter how I search I can't seem to find an answer...

Trying to set up a condition is vba where if a cell value minus another cell value is greater than a static value (elsewhere in the sheet), then to turn the font green.

Code so far:

Code:
    Dim Cval As Integer
    Cval = Cells(3, 6).Value
    FinalRow = Cells(65536, Cval).End(xlUp).Row
    For i = 10 To FinalRow
        If Cells(i, Cval) - Cells(i, Cval + 1) > R6C5 Then
            With Cells
            .Font.ColorIndex = 10
            .Font.Bold = True
            End With
        End If
    Next i
End Sub

I can't work out the right sintax to get a if condition to work correctly.

I have gotten this to work with conditional format, but if you want to copy/paste elsewhere, it doesn't paste the format result correctly...

Any advice would be appreciated.
 
Then I got it

You can't add/deduct from String value.

Cval + 1

change to
Code:
If Cells(i, Cval) - Cells(i, Cval).Offset(,1) > Cells(6, 5) Then

Ahhh, interesting.

However code is still returning a run time '13' type mismatch.

I even changed the colums to the actual column number, still no go õ.o

Code:
    Dim Cval As Integer
    Cval = Cells(3, 6).Value
    FinalRow = Cells(65536, Cval).End(xlUp).Row
    For i = 10 To FinalRow
        If Cells(i, 5) - Cells(i, 5).Offset(, 1) > Cells(6, 5) Then
            With Cells
            .Font.ColorIndex = 10
            .Font.Bold = True
            End With
        End If
    Next i
End Sub
 
Upvote 0

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
OK

Are you running the code for the ActiveSheet ?
Or is it suppose to run on the other sheet?
 
Upvote 0
Active sheet.

Incendentally, here is the code of the conditional format that works with a matrix of 7 columns long.

Code:
    FinalRow = Cells(65536, 6).End(xlUp).Row
    With Range("F6:F" & FinalRow)
        .FormatConditions.Delete
        .FormatConditions.Add xlExpression, Formula1:="=(RC-RC[1])>R2C7=TRUE"
        .FormatConditions(1).Font.ColorIndex = 50
        .FormatConditions(1).Font.Bold = True
        With Range("G6:G" & FinalRow)
            .FormatConditions.Delete
            .FormatConditions.Add xlExpression, Formula1:="=(RC-RC[-1])>R3C7=TRUE"
            .FormatConditions(1).Font.ColorIndex = 46
            .FormatConditions(1).Font.Bold = True
        End With
    End With
End Sub
 
Last edited:
Upvote 0
Ahhh

I think you said Cells(3, 6).Value is the result from VLookup and wuould be a,b,c,d,e...

Then Cval can't be an integer, so type mismatch
Rich (BB code):
Dim Cval As Integer
 
Upvote 0
Ahhh

I think you said Cells(3, 6).Value is the result from VLookup and wuould be a,b,c,d,e...

Then Cval can't be an integer, so type mismatch
Rich (BB code):
Dim Cval As Integer

No, the results from the Vlookup is a number. ie if a=1, b=2 so on up to z=26...still means same problem?

Even when I replace the value of F3 with a value (5), the code still runs into the same error message (with the if line highlighted in yellow btw).
 
Upvote 0
The value reruned from VLookup formula is String, unless you multiply/add/devide by 1.

Just try As Variant
 
Upvote 0
The value reruned from VLookup formula is String, unless you multiply/add/devide by 1.

Just try As Variant

Changed to As Variant. Same error msg. If line highlighted. Even when I replace Cvar with the actual column, it still refuses to work.

Dim Cval As Variant
Cval = Cells(3, 6).Value
FinalRow = Cells(65536, 5).End(xlUp).Row
For i = 10 To FinalRow
If Cells(i, 5) - Cells(i, 5).Offset(, 1) > Cells(6, 5) Then
With Cells
.Font.ColorIndex = 10
.Font.Bold = True
End With
End If
Next i
End Sub

Gah!! Sorry for all the hassle. And here I though it would be easy to turn from conditional format to actual colour change!!
 
Upvote 0
That's odd
How about
Rich (BB code):
Dim Cval As Variant, r As Range
Cval = 5
For Each r In Range(cells(10, Cval), Cells(Rows.Count, Cval).End(xlUp))
   If r.Value - r.Offset(, 1).Value > Cells(6, 5).Value Then
        With Cells
            .Font.ColorIndex = 10
            .Font.Bold = True
        End With
    End If
Next
End Sub
Gah!! Sorry for all the hassle. And here I though it would be easy to turn from conditional format to actual colour change!!
Then your code is coloring the font of entire sheet, doesn't make sense
 
Last edited by a moderator:
Upvote 0
Hi Seiya.

Thanks for all your help yesterday. It's greatly appreciated to a newbie like me.

After some more work I managed to get it working as I want it to.

Involved moving the vlookup to the macro and pastespecialling to turn it into a number then usining As Integer for Cval.

Again, thanks for all your help, wasn't aware about how strings need to be treated so it's been a great learning experience.

Code is as follows:
Code:
    [F3].Value = "=Vlookup(R3C4,Sheet2!R1C1:R26C2,2,False)"
    Range("F3").Copy
    Range("F3").PasteSpecial Paste:=xlPasteValues
    Application.CutCopyMode = False
    Dim Cval As Integer
    Cval = Range("F3").Value
    FinalRow = Cells(65536, Cval).End(xlUp).Row
    For i = 11 To FinalRow
    Cells(i, Cval).Offset(, 2).Value = "=RC[-2]-RC[-1]"
        If Cells(i, Cval).Offset(, 2) > Cells(6, 5) Then
            With Cells(i, Cval)
            .Font.ColorIndex = 10
            .Font.Bold = True
            End With
        End If
    Cells(i, Cval).Offset(, 2).ClearContents
    Next i
End Sub

Maybe in the future I'll know how to do the whole compairson in one line insead of actually putting a formula into the sheet:

Cells(i, Cval).Offset(, 2).Value = "=RC[-2]-RC[-1]"

and then doing the comparison from that. But for the moment I'm just glad I got it to work!!
 
Upvote 0

Forum statistics

Threads
1,216,372
Messages
6,130,223
Members
449,567
Latest member
ashsweety

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