Insert formula depending on cell values

thedobbs

Board Regular
Joined
Apr 20, 2010
Messages
59
Hi guys. Bit of a problem here. I have a "Resource Plan" in excel and rather than have 1000s of cells with formulas in I am trying to run a macro that inserts the formula only where it is needed. I am getting there but one line appears to be beating me. Can anybody help, or does anyone know a simple way of doing what I am trying to do.

Here is my formula below:

Sub TestSpread()
Dim RowCount As String
Dim ColCount As String
RowCount = ActiveCell.Row
ColCount = ActiveCell.Column
Dim c1 As Range
For Each c1 In Range("TestRange")
If Cells(RowCount, 16).Value > Cells(12, ColCount).Value Then
c1.FormulaR1C1 = "=IF(AND(RC16>=R12C,RC16<R12C+6),(NETWORKDAYS(RC16,R12C+5,Holidays)/5),IF(AND(RC16<=R12C,RC18>R12C+5),(NETWORKDAYS(R12C,R12C[1]-1,Holidays)/5),IF(AND(RC18>=R12C,R12C>RC16),(NETWORKDAYS(R12C,RC18,Holidays)/5),"""")))"
End If
Next c1
End Sub

This line appears to be the problem:
If Cells(RowCount, 16).Value > Cells(12, ColCount).Value Then
 

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.
Try like this

Code:
Sub TestSpread()
Dim RowCount As Long
Dim ColCount As Long
Dim c1 As Range
For Each c1 In Range("TestRange")
    RowCount = c1.Row
    ColCount = c1.Column
    If Cells(RowCount, 16).Value > Cells(12, ColCount).Value Then
        c1.FormulaR1C1 = "=IF(AND(RC16>=R12C,RC16<=R12C,RC18>R12C+5),(NETWORKDAYS(R12C,R12C[1]-1,Holidays)/5),IF(AND(RC18>=R12C,R12C>RC16),(NETWORKDAYS(R12C,RC18,Holidays)/5),"""")))"
    End If
Next c1
End Sub
 
Upvote 0
Thanks for that, that is brilliant.

How would I go about adding another if statement.
For example

If Cells(12, ColCount).Value >= Cells(RowCount, 16).Value Then
AND ALSO
If Cells(12, ColCount+1).Value >= Cells(RowCount, 18).Value Then

Also:
Can I do ColCount+1??
 
Upvote 0

Forum statistics

Threads
1,224,516
Messages
6,179,231
Members
452,898
Latest member
Capolavoro009

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