vba Cells(x, 2).Formula = "= IF(" & Cells(y, 2) & ">=" & Cells(z, 2) & ",true, false)"

montecarlo2012

Well-known Member
Joined
Jan 26, 2011
Messages
985
Office Version
  1. 2010
Platform
  1. Windows
Hello.
I am working out this idea

VBA Code:
sub test0()
Cells(15, 2).Formula = "=IF(" & Cells(2, 2) & ">=" & Cells(7, 2) & ",TRUE, FALSE)"
Cells(31, 2).Formula = "=IF(" & Cells(18, 2) & ">=" & Cells(23, 2) & ",TRUE, FALSE)"
Cells(47, 2).Formula = "=IF(" & Cells(34, 2) & ">" & Cells(39, 2) & ",TRUE, FALSE)"
Cells(63, 2).Formula = "=IF(" & Cells(50, 2) & ">=" & Cells(55, 2) & ",TRUE, FALSE)"
Cells(79, 2).Formula = "=IF(" & Cells(66, 2) & ">=" & Cells(71, 2) & ",TRUE, FALSE)"
Cells(95, 2).Formula = "=IF(" & Cells(82, 2) & ">=" & Cells(87, 2) & ",TRUE, FALSE)"
end sub

From here I tried to loop, in order to make it shorter. like this:
VBA Code:
Sub test1()
Dim y%
Dim z%
Dim x%

y = 2
z = 7

      For x = 15 To 95 Step 16      
                        Cells(x, 2).Formula = "= IF(" & Cells(y, 2) & ">=" & Cells(z, 2) & ",true, false)"            
            y = y + 16
            z = z + 16            
      Next x

End Sub

but give me an error
1624398571820.png


debug show me this line

Cells(x, 2).Formula = "= IF(" & Cells(y, 2) & ">=" & Cells(z, 2) & ",true, false)"

What I did wrong.
Please let me know.
Thanks for reading.
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
Hi, try this :​
VBA Code:
Sub Demo1()
    For R& = 0 To 80 Step 16
        Cells(15 + R, 2).Formula = "=" & Cells(2 + R, 2) & ">=" & Cells(7 + R, 2)
    Next
End Sub
 
Upvote 0
But the obvious Excel way :​
VBA Code:
Sub Demo2()
    For R& = 15 To 95 Step 16:  Cells(R, 2).FormulaR1C1 = "=R[-13]C>=R[-8]C":  Next
End Sub
 
Upvote 0
Just declare the variable with Dim statement like in your Test1 VBA procedure or remove Option Explicit …​
 
Upvote 0
well now I am here
1624406336988.png

I delete option explicit also.
and still.
Thanks for your input
 
Upvote 0
you need double quotations in the formula...see in red
Cells(x, 2).Formula = "=IF(" & Cells(y, 2) & "" >= "" & Cells(Z, 2) & ",true, false)"
 
Upvote 0
Try this:

VBA Code:
Sub test1()
  Dim x As Long
  For x = 15 To 95 Step 16
    Cells(x, 2).Formula = "=IF(" & Cells(x - 13, 2).address & ">=" & Cells(x - 8, 2).address & ",TRUE, FALSE)"
  Next
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,223,711
Messages
6,174,025
Members
452,542
Latest member
Bricklin

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