Option Compare Text
2 v% t1 \0 v5 c: \- }) I
) a% N' s% G& z$ J: DPublic Function GSXS(Ref)9 a$ g0 C7 S) w! L; n. t$ B
1 o3 [+ Q1 G$ _ GSXS = Ref.Formula, E$ ^+ c2 O( G
/ [* i( V. L" e; B! _
End Function
2 d- N5 p* \# e; W# J- c* \- N. m) F+ \) P' u: T
Public Function ZZL(RowHead, ColHead, Dummy)
- F- u; a$ |- f B' A, q
/ S8 B7 ^4 ~3 A& C. @5 a* `. V& ADim Values(20) As Variant3 }9 h' b7 q5 w9 s2 y* O! D
Dim PrevData(20) As Variant
G3 T- B, M" u6 LDim LE(20) As Integer
. L; s: a# M. m h% P0 o; D: D2 ~* f5 p) g& l
On Error GoTo err_handler1- y5 P; A7 o5 C# N$ n
' Do the vertical selection from rows
! k5 k& Z9 i+ n) Y, n' U$ kIf RowHead.Rows.Count = 1 Then) v- F5 d$ [3 T! U, l1 Z
rindex = RowHead.Row ' first argument is any cell on the row of possible values
4 d3 x$ V7 ^' y! mElse
9 j" b* g/ H2 E1 b8 C3 L) F ' Store the values to be compared with each column
; h, E/ g4 q+ z; { d: f( w For ii = 1 To RowHead.Columns.Count1 Q9 y0 k% V6 T. Y: d$ T7 ~
rngname = RowHead.Cells(1, ii)
0 O E1 d! T* [ LE(ii) = InStr(rngname, "<=")! n! S8 P; a% ]6 k
If LE(ii) > 0 Then
3 ]9 G" d# o) E/ t7 t2 o. ^ rngname = Mid(rngname, 1, LE(ii) - 1)
; ]# `( {8 ~0 P: j/ b! Y End If2 ?# g! G& }2 L
Values(ii) = Range(rngname)# h1 v! Q0 h, y( C6 {8 y7 m
'debug.Print "Variable:" & rngname & " is:" & Values(ii)
- j: G+ }* `/ G8 I6 C4 S8 S( z PrevData(ii) = "" ' initialise
; b5 o# f* K! Y; {3 ~. X. Q" j- n Next ii
9 J5 l1 t9 Z! t3 d
/ [, u$ c5 G9 Y rindex = 29 X2 R w3 P$ [0 V- P2 d7 G
'debug.Print RowHead.Columns.Count0 Y# h" ]" P+ k) e2 D0 a7 v& h+ _ S
Match = False
8 a0 M- l, J' o! L( b! z For r = rindex To RowHead.Rows.Count
2 w& ^2 N3 B4 ?% @, x For c = 1 To RowHead.Columns.Count ' for each dimension
: C2 J1 l: K/ z5 m data = RowHead.Cells(r, c); E. j! N: ^- a8 Q: O
If data = "" Then. n4 `4 Z& O+ l$ M) A3 i
'debug.Print "Empty cell found: using " & PrevData(c)
3 U9 F, b; k2 c# i' C ' use the last valid cell in this column! f/ g7 M9 _ `" y4 P
' (this is to handle merged cells)+ z2 O. ]! @- P$ _
data = PrevData(c) E9 f6 F' v4 O& G8 b; s
End If
+ N% J' |( i( J' q, ?5 Y 'debug.Print "data:" & data6 |3 ]: G1 {3 {# r' L
PrevData(c) = data ' save for use by empty cells6 x6 Y2 p7 s' f2 E1 \- ]
If data = Values(c) Or (data > Values(c) And LE(c) > 0) Or data = "*" Then
/ h2 w& ~0 O' [- A- i; ` If c = RowHead.Columns.Count Then ' All columns match - It's a go
/ s' {: P, k& ?' I5 S Match = True
0 p2 i. B, [% ]8 c i. d2 W End If% N1 ]2 f w4 d* ?
Else ' This column doesn't match - go to the next row
' l$ m6 m' g, M' I4 G% t8 S Match = False: i/ e( v4 _! z5 |
Exit For
9 }4 Y% ~9 W' b# A' D6 c* ~, l, j End If8 s, _+ l9 u4 u1 G& ?0 }
Next c) T" B4 K6 b1 e3 f9 M% K/ B
If Match = True Then ' Don't search any more rows% `, G q' A7 |+ x, B, I
rindex = r+ ?3 O7 y4 O7 {; R
Exit For
9 E/ m3 `& {' \ End If% S) {9 }" g. y+ K B
Next r
- ], p) g! l+ B; |$ h/ I# w8 \, m
$ J9 O' N- ?6 ~5 q If Match = False Then ' Didn't find a matching set of values
! |) B* X+ c3 G: k9 G ZZL = "No match for rows"/ v4 K& M$ m9 X+ `* t
Exit Function
5 ?* F8 x3 y5 ]( W2 L" P% ? End If
) |+ \6 m- E: ~0 G7 o
/ P7 X% O% W) E/ o# a; [5 V rindex = rindex + RowHead.Row - 1 ' make absolute index
+ p! Q9 N; J' {* ?End If3 O8 h9 ]2 y- V7 k# p
9 y: z$ ?" K8 _* B% j+ `' Do the horizontal selection from columns' ]- L" |7 y3 j7 k
If ColHead.Columns.Count = 1 Then
+ N" z4 v3 w5 s& @: x cindex = ColHead.Column# [6 K: {+ M$ ?0 t _6 |' V
Else2 _3 [2 `% u u
' Store the values to be compared with each row of the header1 n; N# o+ E! B/ q5 n5 S1 v
For ii = 1 To ColHead.Rows.Count2 ?+ H: g8 O# T0 @# B) |0 n
rngname = ColHead.Cells(ii, 1)) A4 {9 }" b/ ?8 t0 I
LE(ii) = InStr(rngname, "<=")" Y8 L4 t' B0 }4 e, r# [
If LE(ii) > 0 Then0 D1 o* c6 i1 z$ E3 [' g g* X
rngname = Mid(rngname, 1, LE(ii) - 1)
8 d- L% k, S; g3 V6 @: N/ h% o End If% o: ]- V5 ?7 o( z1 ` M( K d
Values(ii) = Range(rngname)
' y$ s: _6 h& h( k4 r6 ~: t 'debug.Print "Variable:" & rngname & " is:" & Values(ii)
+ @" D1 L& R" J* p6 x+ W PrevData(ii) = "" ' initialise, U8 m& z/ [1 F) w1 ~6 I# l
Next ii
0 [) V( ^1 K- ~ P
0 k' F' W m, ^2 H1 z/ P- g1 ^. t cindex = 29 _1 p8 P% P ~$ Y' a
'debug.Print ColHead.Columns.Count
5 \' d/ Z( W6 ~' O% k) i/ K Match = False+ n, S. f G$ P- X* E
For c = cindex To ColHead.Columns.Count
3 l8 q7 h4 T+ {: {2 x3 m3 g' x- F For r = 1 To ColHead.Rows.Count ' for each dimension
4 x3 _' E) S& J+ q5 } V, S4 V data = ColHead.Cells(r, c). x2 V. s! ?7 b" V
If data = "" Then
/ }9 l; v( J6 M2 m5 W 'debug.Print "Empty cell found: using " & PrevData(r)
% M" [. u8 M- K0 W ' use the last valid cell on this row
5 c1 Y. K3 A4 S6 z ' (this is to handle merged cells)
0 k: S+ r I0 W" H# H. I* z1 { data = PrevData(r)
. H* Y% N7 Y b End If
4 j' [" b9 V5 s- C* N P1 s3 A5 E 'debug.Print "data:" & data
; `' I* G* H4 |% {. R/ `3 W' o PrevData(r) = data ' save for use by empty cells
' j, w( W3 J& K3 K. r If data = Values(r) Or (data > Values(r) And LE(r) > 0) Or data = "*" Then
8 b+ w# o; Q9 k. g: q! C4 { If r = ColHead.Rows.Count Then ' All rows match - It's a go
! R, r- n6 o0 t9 r; t; P. f Match = True, p$ j' f/ x* t/ V- F9 E0 M
End If( W1 n( l$ U: W2 ?% j1 n
Else ' This row doesn't match - go to the next column
/ _( a8 D6 z2 ~1 } Match = False
+ C' V. ~' J9 V$ C) N2 @2 h2 V8 c Exit For- g% {% Y! \& B3 s$ {
End If
4 |; _7 t" F% ]; ^ Next r
' D' R% R0 ?( a$ ?; h% a: P If Match = True Then ' Don't search any more columns
{& R& p1 @5 V4 [% W cindex = c
& Y, `& M% ]" @+ h2 |1 n Exit For
" e1 q7 A% w l% C& q End If
9 p1 r" E' E" x Next c* }" s. s5 W" I
; D- U, ^" ^/ I( G
If Match = False Then ' Didn't find a matching set of values4 M( o" ~4 @2 {7 r
ZZL = "No match for columns"4 M: k% M p+ c' D1 x% M/ {
Exit Function
; e9 F [/ o4 p6 o End If6 `& H% `; [* J7 A2 K# N+ z
" Y$ T6 t7 X2 r0 W
cindex = cindex + ColHead.Column - 1+ e+ H( S) o" ^% G8 S) H
End If
' W6 k7 T5 M, m& x5 i6 ]# i V0 N& i v N) {
' Return the cell value from Table
- m* g4 }+ M2 q" l6 \'debug.Print "Answer is in (R,C): " & rindex, cindex
0 F- I: C% ~# g( G: m- \& XZZL = ActiveSheet.Cells(rindex, cindex)
_: X. w6 X# I1 W$ Q'debug.Print "Answer is : " & ZZL* r. n' z' S# ?, ?
Exit Function
) N; g! A5 U1 e- o# \% Q
) b- Q9 d3 z5 Z( zerr_handler1: Z# w0 u8 ?! U% P& I6 y
ZZL = "Error on range '" & rngname & "'"
0 ?: p& d$ s2 w o* e1 {% m$ t
7 k$ K& M# m8 w$ g8 e' SEnd Function
0 j9 P% J$ _& z3 w0 S; m
9 s3 u' y" G) N7 _* l# I! S |