How do I use VB to create a formula?

slam

Well-known Member
Joined
Sep 16, 2002
Messages
921
Office Version
  1. 365
  2. 2019
I have a worksheet that needs some formulas applied each time upon worksheet activate because I have some other stuff going on that wipes them out.

In A2 through A36, I need this formula:

=IF(B2="","",INDEX(Team,MATCH(B2,Driver,0)))

And in V2 through V36, I need this array formula:

{=IF(COUNTBLANK($C2:$U2)=COLUMNS($C2:$U2),"",SUM(IF(A$2:A$36=A2,C$2:U$36)))}

Could anyone help with this?

Thanks
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
This sheet activate procedure should do what you want...

Code:
Private Sub Worksheet_Activate()
  Range("A2:A36").Formula = "=IF(B2="""","""",INDEX(Team,MATCH(B2,Driver,0)))"
  Range("V2:V36").Formula = "=IF(COUNTBLANK(C2:U2)=19,"""",SUMIF(A$2:A$36,A2,C$2:U$36))"
End Sub
Note that I changed your second formula a little bit... I replace the COLUMNS function call with the number 19 which it will always be equal to (saves Excel the trouble of calculating it in every formula) and I changed your SUM function to a SUMIF function (doing that means the formula can be a normal-entered one).
 
Upvote 0
That works great, but for some reason I'm getting a different (inaccurate) result in column V now.
 
Upvote 0
I tried this with the original formula, but it doesn't like it....

Range("V2:V36").FormulaArray = "=IF(COUNTBLANK($C2:$U2)=COLUMNS($C2:$U2),"",SUM(IF(A$2:A$36=A2,C$2:U$36)))"
 
Last edited:
Upvote 0
I tried this with the original formula, but it doesn't like it....

Range("V2:V36").FormulaArray = "=IF(COUNTBLANK($C2:$U2)=COLUMNS($C2:$U2),"",SUM(IF(A$2:A$36=A2,C$2:U$36)))"
You have to double up the internal quote marks or else VB tries to use them to end and start new text string. Try it this way...

Range("V2:V36").FormulaArray = "=IF(COUNTBLANK($C2:$U2)=COLUMNS($C2:$U2),"""",SUM(IF(A$2:A$36=A2,C$2:U$36)))"
 
Upvote 0
That is now putting the same formula in every cell without incrementing the row number?

Here is my entire code if that may help:

Private Sub Worksheet_Activate()
Dim headerRow, lastRow, c

'unmerge
For Each c In Array(1, 22)
With Intersect(Columns(c), ActiveSheet.UsedRange)
.UnMerge
On Error Resume Next
.SpecialCells(xlCellTypeBlanks).FormulaR1C1 = "=R[-1]C"
On Error GoTo 0
.Value = .Value
End With
Next

'apply formulas
Range("A2:A36").Formula = "=IF(B2="""","""",INDEX(Team,MATCH(B2,Driver,0)))"
Range("V2:V36").FormulaArray = "=IF(COUNTBLANK($C2:$U2)=COLUMNS($C2:$U2),"""",SUM(IF(A$2:A$36=A2,C$2:U$36)))"

'sort
Range("A2:X36").Sort Key1:=Range("W2"), Order1:=xlAscending, _
Key2:=Range("X2"), Order1:=xlAscending, _
Header:=xlNo, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom

're-merge
On Error GoTo safeExit
headerRow = 1
lastRow = Cells(Rows.Count, "A").End(xlUp).Row
f = headerRow + 1
Application.DisplayAlerts = False
Do Until f > lastRow
l = f + 1
Do Until (Cells(l, 1) <> Cells(l - 1, 1)) Or (Cells(l, 22) <> Cells(l - 1, 22))
l = l + 1
Loop
If f <> l Then
For c = 1 To 22 Step 21
With Range(Cells(f, c), Cells(l - 1, c))
.merge
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
End With
Next c
End If
f = l
Loop
safeExit:
Application.DisplayAlerts = True

End Sub
 
Upvote 0
That is now putting the same formula in every cell without incrementing the row number?
Sorry, I posted without testing. Replace the code I gave you with this and see if it works for you...

Code:
Range("V2").FormulaArray = "=IF(COUNTBLANK($C2:$U2)=COLUMNS($C2:$U2),"""",SUM(IF(A$2:A$36=A2,C$2:U$36)))"
Range("V2:V36").FillDown
Range("A2").Formula = "=IF(B2="""","""",INDEX(Team,MATCH(B2,Driver,0)))"
Range("A2:A36").FillDown
 
Upvote 0

Forum statistics

Threads
1,224,592
Messages
6,179,774
Members
452,942
Latest member
VijayNewtoExcel

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