P
PBcorn
I am using a UDF (code at bottom) to look up particular combinations of 2
column headers and return a range from below them. so where Avars = say, A,B,
and Product = say, Q,W,E,R,T,Y,.... (this UDF is then inserted in place of
a particular range argument in existing sumproduct formulae.)
Avars is a string argument in the function
Product is a range (a cell - column header in the column where the formula
is used)
source table:
A A A A A A B B B B B B B
Q W E R T Y Q W E R T Y
[
Data ranges in here
]
The problem is that although the function correctly looks up the relevant
values in the first part of the table (Avars= A), for the second part (Avars
= B), it seems unable to find some combinations, e.g. BQ, BR and BT and
therefore cannot return the ranges below them. The strange thing is that it
finds BW and BE without any problem.
I have tested the lookup code in a sub, it works fine. I have tested all the
arguments in the function.
Please help or suggest an alternative way of doing this.
Thanks
Function REFR(Product As Range, Colhead As Range, Avars As String)
Application.Volatile
Dim c As Range
Dim cs As Integer
Dim p As Integer
Dim sumthese()
p = Colhead.Columns.Count
For Each c In Colhead.Cells
If Trim(c.Value) = Trim(Product) And _
Trim(Cells((c.Row - 1), (c.Column)).Value) = Trim(Avars) Then
sumthese() = Range(Cells(c.Row + 1, c.Column), Cells(46, c.Column))
Else: cs = cs + 1
End If
Next c
If cs = p Then
REFR = 1
Else
REFR = sumthese
End If
End Function
column headers and return a range from below them. so where Avars = say, A,B,
and Product = say, Q,W,E,R,T,Y,.... (this UDF is then inserted in place of
a particular range argument in existing sumproduct formulae.)
Avars is a string argument in the function
Product is a range (a cell - column header in the column where the formula
is used)
source table:
A A A A A A B B B B B B B
Q W E R T Y Q W E R T Y
[
Data ranges in here
]
The problem is that although the function correctly looks up the relevant
values in the first part of the table (Avars= A), for the second part (Avars
= B), it seems unable to find some combinations, e.g. BQ, BR and BT and
therefore cannot return the ranges below them. The strange thing is that it
finds BW and BE without any problem.
I have tested the lookup code in a sub, it works fine. I have tested all the
arguments in the function.
Please help or suggest an alternative way of doing this.
Thanks
Function REFR(Product As Range, Colhead As Range, Avars As String)
Application.Volatile
Dim c As Range
Dim cs As Integer
Dim p As Integer
Dim sumthese()
p = Colhead.Columns.Count
For Each c In Colhead.Cells
If Trim(c.Value) = Trim(Product) And _
Trim(Cells((c.Row - 1), (c.Column)).Value) = Trim(Avars) Then
sumthese() = Range(Cells(c.Row + 1, c.Column), Cells(46, c.Column))
Else: cs = cs + 1
End If
Next c
If cs = p Then
REFR = 1
Else
REFR = sumthese
End If
End Function