UDF Error: Compile Error Array Expected - I do need help with this function

greegan

Well-known Member
Joined
Nov 18, 2009
Messages
643
Office Version
  1. 365
Platform
  1. Windows
The following is Part 1 of a custom function problem I'm having. Thank you for your assistance.
Code:
Function FindFedIntPg(I As Long, Period As String, Cntry As String)
Dim GetFedTableBase As Variant
Dim Tween As Boolean
Dim GetTableIncr As Double
Dim Cat1, Cat2, Cat3, Cat4, Cat5, Cat6, Cat7, Cat8 As Boolean
Dim Pg As Double
    If Pg = 1 Then
        GetTableIncr = WorksheetFunction.Lookup(Period, Array("Weekly", "Biweekly", "Semi-Monthly", "Monthly"), _
        Array(2, 4, 4, 8))
    ElseIf Pg = 2 Then
        GetTableIncr = WorksheetFunction.Lookup(Period, Array("Weekly", "Biweekly", "Semi-Monthly", "Monthly"), _
        Array(4, 8, 8, 18))
    ElseIf Pg = 3 Then
        GetTableIncr = WorksheetFunction.Lookup(Period, Array("Weekly", "Biweekly", "Semi-Monthly", "Monthly"), _
        Array(8, 16, 18, 34))
    ElseIf Pg = 4 Then
        GetTableIncr = WorksheetFunction.Lookup(Period, Array("Weekly", "Biweekly", "Semi-Monthly", "Monthly"), _
        Array(12, 24, 26, 52))
    ElseIf Pg = 5 Then
        GetTableIncr = WorksheetFunction.Lookup(Period, Array("Weekly", "Biweekly", "Semi-Monthly", "Monthly"), _
        Array(16, 32, 34, 70))
    ElseIf Pg = 6 Then
        GetTableIncr = WorksheetFunction.Lookup(Period, Array("Weekly", "Biweekly", "Semi-Monthly", "Monthly"), _
        Array(20, 40, 44, 86))
 End If
 

    If Cntry = "CA" Then
        GetFedTableBase = WorksheetFunction.Lookup(Period, Array("Weekly", "Biweekly", "Semi-Monthly", "Monthly"), _
        Array(248, 495, 537, 1072))
    ElseIf Cntry = "OC" Then
        GetFedTableBase = WorksheetFunction.Lookup(Period, Array("Weekly", "Biweekly", "Semi-Monthly", "Monthly"), _
        Array(248, 495, 537, 1072))
    End If
Cat1 = GetFedTableBase(Cntry, Period) - GetFedTableBase(Cntry, Period)
Cat2 = GetFedTableBase(Cntry, Period)
Cat3 = GetFedTableBase(Cntry, Period) + 54 * GetTableIncr(1, Period)
Cat3 = GetFedTableBase(Cntry, Period) + 54 * GetTableIncr(1, Period) + 55 * (GetTableIncr(2, Period))
Cat4 = GetFedTableBase(Cntry, Period) + 54 * GetTableIncr(1, Period) + 55 * (GetTableIncr(2, Period)) + 55 * (GetTableIncr(3, Period))
Cat5 = GetFedTableBase(Cntry, Period) + 54 * GetTableIncr(1, Period) + 55 * (GetTableIncr(2, Period)) + 55 * (GetTableIncr(3, Period)) _
    + 55 * (GetTableIncr(4, Period))
Cat6 = GetFedTableBase(Cntry, Period) + 54 * GetTableIncr(1, Period) + 55 * (GetTableIncr(2, Period)) + 55 * (GetTableIncr(3, Period)) _
    + 55 * (GetTableIncr(4, Period)) + 55 * (GetTableIncr(5, Period))
Cat7 = GetFedTableBase(Cntry, Period) + 54 * GetTableIncr(1, Period) + 55 * (GetTableIncr(2, Period)) + 55 * (GetTableIncr(3, Period)) _
    + 55 * (GetTableIncr(4, Period)) + 55 * (GetTableIncr(5, Period)) + 55 * (GetTableIncr(6, Period)) - 0.01
If Target >= Low And Target <= High Then
Tween = True
Else
Tween = False
End If

If Tween(I, Cat1, Cat2) = True Then FindFedIntPg = 1
ElseIf Tween(I, Cat2, Cat3) = True Then FindFedIntPg = 2
ElseIf Tween(I, Cat3, Cat4) = True Then FindFedIntPg = 3
ElseIf Tween(I, Cat4, Cat5) = True Then FindFedIntPg = 4
ElseIf Tween(I, Cat5, Cat6) = True Then FindFedIntPg = 5
ElseIf Tween(I, Cat6, Cat7) = True Then FindFedIntPg = 6
End If

End Function


I'm getting a compile error: Expected array and GetTableIncr is highlighted in the Cat1= formula.
In my cell I have it structured as I=3000,Period=Biweekly,Cntry=CA

Basically, if I falls between two values, it should produce a number ranging from 1 to 6.

Cheers,

-- G
 
1 is the page number, we are trying to determine the increments the page represents based on the table (period)

But GetTableIncr is a number ? (the result of a lookup)

Also, why do you have here this:

Code:
Cat2 = GetFedTableBase(Cntry, Period)Cat3 = GetFedTableBase(Cntry, Period) + 54 * GetTableIncr(1, Period)
Cat3 = GetFedTableBase(Cntry, Period) + 54 * GetTableIncr(1, Period) + 55 * (GetTableIncr(2, Period))
Cat4 = GetFedTableBase(Cntry, Period) + 54 * GetTableIncr(1, Period) + 55 * (GetTableIncr(2, Period)) + 55 * (GetTableIncr(3, Period))

Two times Cat3 ???

Besides that, why don't you substitute Cat3 in the expression for Cat4?

Code:
Cat4 = Cat3 + 55 * (GetTableIncr(3, Period))

Much easier in my opinion.

I would strongly suggest to rethink your code and go over it in full detail, test it with F8 and Debug.Print,
before trying to get this correct here. It will save you and also the helpers quite some time.
 
Upvote 0

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
But GetTableIncr is a number ? (the result of a lookup)

Also, why do you have here this:

Code:
Cat2 = GetFedTableBase(Cntry, Period)Cat3 = GetFedTableBase(Cntry, Period) + 54 * GetTableIncr(1, Period)
Cat3 = GetFedTableBase(Cntry, Period) + 54 * GetTableIncr(1, Period) + 55 * (GetTableIncr(2, Period))
Cat4 = GetFedTableBase(Cntry, Period) + 54 * GetTableIncr(1, Period) + 55 * (GetTableIncr(2, Period)) + 55 * (GetTableIncr(3, Period))

Two times Cat3 ???

Besides that, why don't you substitute Cat3 in the expression for Cat4?

Code:
Cat4 = Cat3 + 55 * (GetTableIncr(3, Period))

Much easier in my opinion.

I would strongly suggest to rethink your code and go over it in full detail, test it with F8 and Debug.Print,
before trying to get this correct here. It will save you and also the helpers quite some time.

I'll do that and get back to this thread soon. You've pointed out many good points. Thank you

-- G
 
Upvote 0
Hello, I have checked my code for this function and now I have it working. My problem now is that there is too much (in my opinion) coding for something that should be simpler. So here is what I have. This works great, but you'll soon see why I need help...

Code:
Function FindFedIntPg(I As Long, Period As String, Cntry As String) As Variant
Dim GetIncrPg As Variant
Dim Cat0, Cat1, Cat2, Cat3, Cat4, Cat5, Cat6, Cat7 As Variant
Dim a, b, c, d, e, f, g As Variant
a = GetFedTableBase(Cntry, Period)
b = GetTableIncr(1, Period) * 54
c = GetTableIncr(2, Period) * 55
d = GetTableIncr(3, Period) * 55
e = GetTableIncr(4, Period) * 55
f = GetTableIncr(5, Period) * 55
g = GetTableIncr(6, Period) * 55
Cat0 = a - a
Cat1 = a
Cat2 = Cat1 + b
Cat3 = Cat2 + c
Cat4 = Cat3 + d
Cat5 = Cat4 + e
Cat6 = Cat5 + f
Cat7 = Cat6 + g
Dim Pg As Double
Dim Pgx As Variant
Dim j As Variant
Pgx = WorksheetFunction.Lookup(I, Array(Cat0, Cat1, Cat2, Cat3, Cat4, Cat5, Cat6, Cat7))
Pg = WorksheetFunction.Lookup(I, Array(Cat0, Cat1, Cat2, Cat3, Cat4, Cat5, Cat6, Cat7), Array(1, 2, 3, 4, 5, 6, 7))
j = GetTableIncr(Pg - 1, Period)
Dim y0, y1, y2, y3, y4, y5, y6, y7, y8, y9, y10, y11, y12, y13, y14, y15, y16, y17, y18, y19, y20 As Variant
Dim y21, y22, y23, y24, y25, y26, y27, y28, y29, y30, y31, y32, y33, y34, y35, y36, y37, y38, y39 As Variant
Dim y40, y41, y42, y43, y44, y45, y46, y47, y48, y49, y50, y51, y52, y53, y54, y55 As Variant
y0 = Pgx
y1 = y0 + j
y2 = y1 + j
y3 = y2 + j
y4 = y3 + j
y5 = y4 + j
y6 = y5 + j
y7 = y6 + j
y8 = y7 + j
y9 = y8 + j
y10 = y9 + j
y11 = y10 + j
y12 = y11 + j
y13 = y12 + j
y14 = y13 + j
y15 = y14 + j
y16 = y15 + j
y17 = y16 + j
y18 = y17 + j
y19 = y18 + j
y20 = y19 + j
y21 = y20 + j
y22 = y21 + j
y23 = y22 + j
y24 = y23 + j
y25 = y24 + j
y26 = y25 + j
y27 = y26 + j
y28 = y27 + j
y29 = y28 + j
y30 = y29 + j
y31 = y30 + j
y32 = y31 + j
y33 = y32 + j
y34 = y33 + j
y35 = y34 + j
y36 = y35 + j
y37 = y36 + j
y38 = y37 + j
y39 = y38 + j
y40 = y39 + j
y41 = y40 + j
y42 = y41 + j
y43 = y42 + j
y44 = y43 + j
y45 = y44 + j
y46 = y45 + j
y47 = y46 + j
y48 = y47 + j
y49 = y48 + j
y50 = y49 + j
y51 = y50 + j
y52 = y51 + j
y53 = y52 + j
y54 = y53 + j
y55 = y54 + j
 

Dim l As Variant
l = WorksheetFunction.Lookup(I, Array(y0, y1, y2, y3, y4, y5, y6, y7, y8, y9, y10, y11, y12, y13, y14, y15, y16, y17, y18, y19, y20, _
                                    y21, y22, y23, y24, y25, y26, y27, y28, y29, y30, y31, y32, y33, y34, y35, y36, y37, y38, y39, y40, _
                                    y41, y42, y43, y44, y45, y46, y47, y48, y49, y50, y51, y52, y53, y54, y55), Array(y0, y1, y2, y3, y4, y5, y6, y7, y8, y9, y10, y11, y12, y13, y14, y15, y16, y17, y18, y19, y20, _
                                    y21, y22, y23, y24, y25, y26, y27, y28, y29, y30, y31, y32, y33, y34, y35, y36, y37, y38, y39, y40, _
                                    y41, y42, y43, y44, y45, y46, y47, y48, y49, y50, y51, y52, y53, y54, y55))
 
Dim h As Variant
h = l + j

FindFedIntPg = WorksheetFunction.Median(l, h)
 
End Function

Cheers,


-- g
 
Upvote 0

Forum statistics

Threads
1,215,548
Messages
6,125,464
Members
449,229
Latest member
doherty22

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