NettyBetty
New Member
- Joined
- May 14, 2015
- Messages
- 1
Hello,
I need the below code to count the number of columns in a worksheet and extend an array formula to look at all of those columns. I know there is a 255 character limit with arrays so I've tried to break it in to smaller chunks. I'm getting a 'Type mismatch' error and it's breaking at the .Replace V_V() part.
Any help on why I'm seeing this would be appreciated.
Sub Schedule_Holidays()
' Sets the workbook to R1C1
' Sets the workbook to Manual Calculation mode
' Counts the number of columns in the Holiday Lookup tab and extends the array formulas to match
' Sets the workbook back to Automatic Calculation mode
Dim z As Integer
Dim sWorkSheet As Excel.Worksheet
Dim ArrayPart1 As String
Dim ArrayPart2 As String
Dim ArrayPart3 As String
Dim Stub As String
Set sWorkSheet = Worksheets("Holiday Lookup")
'Sets the workbook to R1C1
Application.ReferenceStyle = xlR1C1
'The array formula must be broken in 3 as it exceeds 255 chrs
Stub = "V_V()" & _
"X_X()" & _
"Y_Y()"
ArrayPart1 = "=IFERROR(IF(R[4]C[11]>=R[4]C[72],(IF(R[4]C[10]<=R[4]C[71],((NETWORKDAYS(R[4]C[71],R[4]C[72],IF(ISNUMBER('Holiday Lookup'!R[2]C[2]:R[2]C[" & z & "]),'Holiday Lookup'!R[2]C[2]:R[2]C[" & z & "]),1))*R[4]C[9])/5*R[4]C[7]),(IF(R[4]C[10]>R[4]C[71],IF(R[4]C[10]>R4C[72],0,((NETWORKDAYS(R[4]C[10],R[4]C[72],IF(ISNUMBER('Holiday Lookup'!R[2]C[2]:R[2]C[" & z & "])),'Holiday Lookup'!R[2]C[2]:R[2]C[" & z & "]),1))*R[4]C[9])/5*R[4]C[7]))))))"
ArrayPart2 = ",(IF(K4>=BU4,(IF(J4<BU4,((NETWORKDAYS(BU4,K4,IF(ISNUMBER('Holiday Lookup'!R[2]C[2]:R[2]C[" & z & "])),'Holiday Lookup'!R[2]C[2]:R[2]C[" & z & "]),1))*I4)/5*G4),"
ArrayPart3 = "(IF(J4>=BU4,((NETWORKDAYS(J4,K4,IF(ISNUMBER('Holiday Lookup'!R[2]C[2]:R[2]C[" & z & "])),'Holiday Lookup'!R[2]C[2]:R[2]C[" & z & "],1)))*I4)/5*G4)))))))),"")"
'Manual calculation
Application.Calculation = xlCalculationManual
' Count number of rows
z = sWorkSheet.Cells(1, Columns.Count).End(xlToRight).Column
' Write to Schedule
With Worksheets("Schedule").Cells(4, 12)
.FormulaArray = Stub
.Replace "V_V()", ArrayPart1
.Replace "X_X()", ArrayPart2
.Replace "Y_Y()", ArrayPart3
End With
Worksheets("Schedule").Range(Cells(4, 12), Cells(65, 12)).FillDown
'Sets the workbook back to A1
Application.ReferenceStyle = xlA1
'Automatic calculation
Application.Calculation = xlCalculationAutomatic
End Sub
I need the below code to count the number of columns in a worksheet and extend an array formula to look at all of those columns. I know there is a 255 character limit with arrays so I've tried to break it in to smaller chunks. I'm getting a 'Type mismatch' error and it's breaking at the .Replace V_V() part.
Any help on why I'm seeing this would be appreciated.
Sub Schedule_Holidays()
' Sets the workbook to R1C1
' Sets the workbook to Manual Calculation mode
' Counts the number of columns in the Holiday Lookup tab and extends the array formulas to match
' Sets the workbook back to Automatic Calculation mode
Dim z As Integer
Dim sWorkSheet As Excel.Worksheet
Dim ArrayPart1 As String
Dim ArrayPart2 As String
Dim ArrayPart3 As String
Dim Stub As String
Set sWorkSheet = Worksheets("Holiday Lookup")
'Sets the workbook to R1C1
Application.ReferenceStyle = xlR1C1
'The array formula must be broken in 3 as it exceeds 255 chrs
Stub = "V_V()" & _
"X_X()" & _
"Y_Y()"
ArrayPart1 = "=IFERROR(IF(R[4]C[11]>=R[4]C[72],(IF(R[4]C[10]<=R[4]C[71],((NETWORKDAYS(R[4]C[71],R[4]C[72],IF(ISNUMBER('Holiday Lookup'!R[2]C[2]:R[2]C[" & z & "]),'Holiday Lookup'!R[2]C[2]:R[2]C[" & z & "]),1))*R[4]C[9])/5*R[4]C[7]),(IF(R[4]C[10]>R[4]C[71],IF(R[4]C[10]>R4C[72],0,((NETWORKDAYS(R[4]C[10],R[4]C[72],IF(ISNUMBER('Holiday Lookup'!R[2]C[2]:R[2]C[" & z & "])),'Holiday Lookup'!R[2]C[2]:R[2]C[" & z & "]),1))*R[4]C[9])/5*R[4]C[7]))))))"
ArrayPart2 = ",(IF(K4>=BU4,(IF(J4<BU4,((NETWORKDAYS(BU4,K4,IF(ISNUMBER('Holiday Lookup'!R[2]C[2]:R[2]C[" & z & "])),'Holiday Lookup'!R[2]C[2]:R[2]C[" & z & "]),1))*I4)/5*G4),"
ArrayPart3 = "(IF(J4>=BU4,((NETWORKDAYS(J4,K4,IF(ISNUMBER('Holiday Lookup'!R[2]C[2]:R[2]C[" & z & "])),'Holiday Lookup'!R[2]C[2]:R[2]C[" & z & "],1)))*I4)/5*G4)))))))),"")"
'Manual calculation
Application.Calculation = xlCalculationManual
' Count number of rows
z = sWorkSheet.Cells(1, Columns.Count).End(xlToRight).Column
' Write to Schedule
With Worksheets("Schedule").Cells(4, 12)
.FormulaArray = Stub
.Replace "V_V()", ArrayPart1
.Replace "X_X()", ArrayPart2
.Replace "Y_Y()", ArrayPart3
End With
Worksheets("Schedule").Range(Cells(4, 12), Cells(65, 12)).FillDown
'Sets the workbook back to A1
Application.ReferenceStyle = xlA1
'Automatic calculation
Application.Calculation = xlCalculationAutomatic
End Sub