VBA Code help needed

Frank3923

Board Regular
Joined
Jan 20, 2003
Messages
244
I have the following code that I use to insert formulas. I now have a need to skip a row when inserting the formula. I am not sure how to incorporate a loop into the code, so it will be inserted every two rows, instead of the next available row. Appreciate any help or hints on solving.

Sub fill_Down()

Dim my_range As Range

With Worksheets("Money")
Set my_range = .Range("B6:B" & .Range("E65536").End(xlUp).Row)
my_range.Formula = _
"=IF($Q6="YES",$E7,$E6)"
End With

End Sub
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
Hi,

I guess there's a lot of ways to accomplish this task but I would use for loops...here's a quick example you can implement in your code:

dim i as int

for i = 6 to 65536
range("B" & i).value = "=IF($Q6="YES",$E7,$E6)"
i = i + 2
next i

That way, you will always skip a row.

Hope this helps.

Thanks
 
Upvote 0
Frank3923,

If you were to use either of the above formula/code you should get this:


Excel Workbook
BEQ
676YES
77
878
99YES
10710YES
1111YES
12712YES
1313YES
14714YES
1515YES
16716YES
1717
18718
19
Money





The formula is not autofilled down (it stayes the same).



Sample data before the updated macro:


Excel Workbook
BEQ
66YES
77
88
99YES
1010YES
1111YES
1212YES
1313YES
1414YES
1515YES
1616YES
1717
1818
19
Money





After the macro:


Excel Workbook
BEQ
676YES
77
888
99YES
101110YES
1111YES
121312YES
1313YES
141514YES
1515YES
161716YES
1717
181818
19
Money





Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).


Code:
Option Explicit
Sub fill_Down()
' hiker95, 06/17/2011
' http://www.mrexcel.com/forum/showthread.php?t=558084
Dim LR As Long, a As Long
Application.ScreenUpdating = True
With Worksheets("Money")
  LR = .Cells(Rows.Count, "E").End(xlUp).Row
  For a = 6 To LR Step 2
    .Range("B" & a).FormulaR1C1 = "=IF(RC17=""YES"",R[1]C5,RC5)"
  Next a
End With
Application.ScreenUpdating = False
End Sub


Then run the fill_Down macro.
 
Upvote 0
Frank,

You are very welcome.

Glad I could help.

Thanks for the feedback.

Come back anytime.
 
Upvote 0

Forum statistics

Threads
1,224,574
Messages
6,179,634
Members
452,934
Latest member
Jdsonne31

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