Macro fills formula in the alternate row.

motilulla

Well-known Member
Joined
Feb 13, 2008
Messages
2,422
Office Version
  1. 2010
Using Excel 2010
Hello,

The code below fill formula in BJ7 to BJ100 Where Lastrow = 100. I need the formula fill in the alternate row for example BJ7, BJ9, BJ11 and so on. Please help.
VBA Code:
Sub FillFormula()
Dim Lastrow As Long
Lastrow = Range("C" & Rows.Count).End(xlUp).Row
Range("BJ7:BJ" & Lastrow).Formula = "=COUNTA(BM7:CF7)"
End Sub

Regards,
Moti
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
Here is one way:
VBA Code:
    Dim r As Long
    Application.ScreenUpdating = False
    For r = 7 To 100 Step 2
        Cells(r, "BJ").FormulaR1C1 = "=COUNTA(RC[3]:RC[22])"
    Next r
    Application.ScreenUpdating = True
 
Upvote 1
VBA Code:
    Dim Lastrow As Long
    Dim i As Long
    
    Lastrow = Range("C" & Rows.Count).End(xlUp).Row
    
    For i = 7 To Lastrow Step 2
        Range("BJ" & i).Formula = "=COUNTA(BM" & i & ":CF" & i & ")"
    Next i
 
Upvote 1
Solution
Another option with array that might be faster, NOT TESTED.

VBA Code:
Sub FillFormulaWithArrays()
    Dim Lastrow As Long, i As Long
    Dim dataRange As Variant, resultFormula As Variant, numRows As Long
    
    Lastrow = Cells(Rows.Count, "C").End(xlUp).Row
    If (Lastrow - 6) Mod 2 = 1 Then
        numRows = Int((Lastrow - 6) / 2) + 1
    Else
        numRows = (Lastrow - 6) / 2
    End If
    
    dataRange = Range("BM7:CF" & Lastrow).Value
    ReDim resultFormula(1 To numRows, 1 To 1)
    
    For i = 1 To UBound(dataRange, 1) Step 2
        resultFormula((i + 1) / 2, 1) = WorksheetFunction.CountA(Application.Index(dataRange, i, 0))
    Next i
    
    Range("BJ7").Resize(numRows).Formula = resultFormula
End Sub
 
Upvote 0
Here is one way:
VBA Code:
    Dim r As Long
    Application.ScreenUpdating = False
    For r = 7 To 100 Step 2
        Cells(r, "BJ").FormulaR1C1 = "=COUNTA(RC[3]:RC[22])"
    Next r
    Application.ScreenUpdating = True
Joe4, Thank you for the quick response it worked. Good Luck!

My Best Regards,
Moti :)
 
Upvote 0
VBA Code:
    Dim Lastrow As Long
    Dim i As Long
   
    Lastrow = Range("C" & Rows.Count).End(xlUp).Row
   
    For i = 7 To Lastrow Step 2
        Range("BJ" & i).Formula = "=COUNTA(BM" & i & ":CF" & i & ")"
    Next i
Cubist, I like your code it is easy to translate and understand it. (y)

Thank you for your quick response. Good Luck!

My Best Regards,
Moti :)
 
Upvote 0

Forum statistics

Threads
1,223,897
Messages
6,175,271
Members
452,628
Latest member
dd2

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