Variable increment

G2K

Active Member
Joined
May 29, 2009
Messages
355
Hi All,

I am having a very small problem but i am unable to solve it since morning, i have used a variable I and assigned a value 2, unfortunately after each iteration the value of I increased with 2 instead of 1

Code:
Private Sub AddColumns()
Set WS = Sheets("DATA")
I = 0
With WS
    .Range("Y1").Value = "Deal Completed This Month"
    .Range("Z1").Value = "Opportunities Persued This Month"
    .Range("AA1").Value = "Opportunities Persued This Year"
    .Range("AB1").Value = "Inactive"
    .Range("AC1").Value = "Close Date Category"
    LR = .Range("B65536").End(xlUp).Row
    For I = 1 To LR
 
        Range("Y" & I).FormulaR1C1 = "=IF(IF(R[" & I & "]C[-15]="""",R[" & I & "]C[-6],R[" & I & "]C[-15])<MASTER!RC[-23],0,1)"< p> 
       MsgBox "=IF(IF(R[" & I & "]C[-15]="""",R[" & I & "]C[-6],R[" & I & "]C[-15])<MASTER!RC[-23],0,1)"< p>  
    Next
End With
End Sub

when i check the formula in msgbox it shows the correct syntex, but when vba copy the formula in worksheet, the value of I becomes 2,4,6,8.........

Any idea what went wrong??
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
That's because you are using incrimenting the offset from relative addressing. Try
Code:
Range("Y" & I).FormulaR1C1 = "=IF(IF(RC[-15]="""",RC[-6],RC[-15])"
If that works, looping is not needed.
Code:
With WS
    .Range("Y1").Value = "Deal Completed This Month"
    .Range("Z1").Value = "Opportunities Persued This Month"
    .Range("AA1").Value = "Opportunities Persued This Year"
    .Range("AB1").Value = "Inactive"
    .Range("AC1").Value = "Close Date Category"

    .Range("Y1").Resize(.Range("B65536").End(xlUp).Row, 1).FormulaR1C1 = "=IF(RC[-15]="""",RC[-6],RC[-15])" 
       
End With
 
Upvote 0

Forum statistics

Threads
1,224,603
Messages
6,179,849
Members
452,948
Latest member
UsmanAli786

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