Option Compare Text
* j' k% r8 v* \" e q
+ v2 P; S0 S9 C b, l; Y9 ]Public Function GSXS(Ref)5 [- x1 x w: H1 i) [: h2 u1 I
( _/ a/ M: R( Z& o' j& l GSXS = Ref.Formula
/ Q1 b, g. H" q3 J' F& u' y8 j4 B8 ~# R) |
End Function4 ]3 o4 G6 Z* p( y h) i& v# P! L
) D$ C, b" w9 H, D
Public Function ZZL(RowHead, ColHead, Dummy)8 _: F1 z7 L* }4 ^8 _1 Y4 J4 y
3 I' o9 P: ?5 `1 w( d) u
Dim Values(20) As Variant
2 i1 s7 c8 `) GDim PrevData(20) As Variant! G6 k8 G: x z) V8 u
Dim LE(20) As Integer \9 H3 p4 s& k# f. D q* Q
/ |5 h) I! _0 G5 H- T$ R
On Error GoTo err_handler1% ~( s+ j, H" J6 o' t. n( y& G
' Do the vertical selection from rows2 o/ E7 o# E7 e: J9 c
If RowHead.Rows.Count = 1 Then
! k5 }% I- ~# r$ G8 E rindex = RowHead.Row ' first argument is any cell on the row of possible values/ A* F: W5 [! l5 q9 Z+ l1 [
Else* q8 n2 f) R4 e' I6 Q* ^
' Store the values to be compared with each column- y/ [0 u6 Z( Z% e4 ~7 V+ j
For ii = 1 To RowHead.Columns.Count% y* X |6 B% H/ z: R
rngname = RowHead.Cells(1, ii)
1 K$ I9 a9 o( y" t* \2 R LE(ii) = InStr(rngname, "<=")# b X2 s" o7 \. H5 V; L5 f
If LE(ii) > 0 Then
* r% V9 h( C4 g: ^1 O$ I8 k- k3 w rngname = Mid(rngname, 1, LE(ii) - 1)
+ e1 f4 j0 p: s4 P7 e End If3 X/ W' }6 K' r2 F* i& y; s
Values(ii) = Range(rngname)
# J1 U$ @0 u+ f2 m7 b6 s" H 'debug.Print "Variable:" & rngname & " is:" & Values(ii)! {8 { x5 \* @7 Q ]5 M7 T
PrevData(ii) = "" ' initialise9 k8 {' A2 {' X: `, Z t" ]9 J
Next ii
5 v- V- W2 Y' u Z" M$ y
. q$ R3 R6 k! V/ d, v J rindex = 2
4 \7 H) i% L6 Z0 z" |; N* p 'debug.Print RowHead.Columns.Count
- p5 B6 `4 q$ }4 R Match = False/ h7 b# a0 _3 r0 \2 O' G
For r = rindex To RowHead.Rows.Count
* U& U p; U6 z3 V6 S3 Y For c = 1 To RowHead.Columns.Count ' for each dimension6 c/ y: p& j1 m- g
data = RowHead.Cells(r, c)
) h5 n1 Y, h6 S; C" r2 e If data = "" Then
" r1 I5 y `2 l8 i8 ] 'debug.Print "Empty cell found: using " & PrevData(c)
4 e, A% T ^! |% P ' use the last valid cell in this column
% r! O+ t/ \) ^" g/ l5 \$ ? ' (this is to handle merged cells)
" [( r! {( j, f6 D data = PrevData(c)
& K. j2 V$ M6 r" `' d End If
$ ~. g1 g0 f! e6 h, Y. z 'debug.Print "data:" & data
3 N- l: [$ {6 v. j PrevData(c) = data ' save for use by empty cells
( p' S* g1 Z7 A' D- w, U$ ^ If data = Values(c) Or (data > Values(c) And LE(c) > 0) Or data = "*" Then. b- L$ E+ Q' t' ]8 p
If c = RowHead.Columns.Count Then ' All columns match - It's a go
. b* M$ `, ~+ a- H/ h4 ? Match = True: g+ h# l# V8 \4 f
End If+ z) n6 G! k- [7 Z" H) _" U
Else ' This column doesn't match - go to the next row
% Y7 O0 U! r9 [: D# U Match = False+ `( E6 v2 X K5 z. c
Exit For
- Y1 {* I1 {' x$ ` End If
3 N+ R3 s4 o5 I Next c' U; [8 c. @" H j4 y
If Match = True Then ' Don't search any more rows
7 ^' A; r( A( I rindex = r
8 Y# t+ E* B _5 n! F Exit For
1 z: X+ \) E/ ^" L/ |, Y, o End If
: \. |9 U" _3 K7 o3 G, l: k Next r8 x. ^# n& p. q& u! k* e& t/ g: W
3 x- n3 Y, D: p( X |+ G2 l If Match = False Then ' Didn't find a matching set of values5 K7 Z, M* U7 V" ^* f
ZZL = "No match for rows" [: g! j4 O; {" n, c
Exit Function
3 A& x: i7 a3 @# D6 w" h" d End If
1 A6 d: }( }% P3 y/ O( b* H. A3 P& H/ p; _0 `3 |1 ~. m6 e
rindex = rindex + RowHead.Row - 1 ' make absolute index7 F& ^9 R; M. Y) x. J
End If
) d" Y. _6 V! k1 b+ ^3 b) r
/ K: X3 a0 z' Y3 i, `' Do the horizontal selection from columns' B! d) N* F( Q% N% f( k3 K
If ColHead.Columns.Count = 1 Then
9 ^: b) `* V5 P' F. A" ?/ F cindex = ColHead.Column
% } j; D9 z6 J) tElse- v$ |; ?2 Z( a% M
' Store the values to be compared with each row of the header
1 B6 E* [+ ^* k7 ^) O& | For ii = 1 To ColHead.Rows.Count* Y$ R0 i; c( M. j9 \
rngname = ColHead.Cells(ii, 1)
: F& R& A+ b; B# O7 l9 { LE(ii) = InStr(rngname, "<=")
' i$ T0 T& I* ]) o0 C @ If LE(ii) > 0 Then
2 m0 {7 R% }( Z: T/ f# K6 V8 \6 O0 s rngname = Mid(rngname, 1, LE(ii) - 1)
3 A1 I+ s3 B7 b6 C End If+ d" {. n: Z5 u
Values(ii) = Range(rngname)
+ r1 {" n3 t4 k4 [* X 'debug.Print "Variable:" & rngname & " is:" & Values(ii)6 a- v; s1 |0 }1 N) Z) R% f0 E2 K
PrevData(ii) = "" ' initialise
# R. j" N4 P$ y6 @ Next ii
( H! ~4 O' D' Q+ ^: O$ _9 L' g6 }1 l4 y) F& m1 M0 b6 W. O9 ]+ o% p
cindex = 2' s/ ]0 J; H' M0 C0 z; A* L
'debug.Print ColHead.Columns.Count( E* P, b- N# [
Match = False
9 y0 u8 E3 U" z, p5 T9 l! |0 r* \ For c = cindex To ColHead.Columns.Count
# t4 c0 F2 V! p. h7 [* f For r = 1 To ColHead.Rows.Count ' for each dimension0 A% Y; H2 F) t1 f( i
data = ColHead.Cells(r, c)
* d( I0 h* b' y8 K- P: P If data = "" Then( P3 k( Z5 I9 W) H- l
'debug.Print "Empty cell found: using " & PrevData(r)
; B M% a! L' j2 F" e& y' X/ X ' use the last valid cell on this row! n D8 a9 D7 f" o; R+ i$ i
' (this is to handle merged cells)
( ^: z2 Z4 n* _0 d data = PrevData(r)
8 N- ]: q& }$ {4 l3 W$ Y8 Q End If
: F3 z% B, @/ ]" X3 I& r 'debug.Print "data:" & data
% Y! s# |- A2 K% D/ f PrevData(r) = data ' save for use by empty cells
" r+ `; k8 m" l- Z% a2 z& Z$ O. m; h If data = Values(r) Or (data > Values(r) And LE(r) > 0) Or data = "*" Then) K6 Z7 s* {( z* D; z8 B
If r = ColHead.Rows.Count Then ' All rows match - It's a go8 \: d u: K9 z7 c
Match = True r/ g; G8 P* I, n
End If
! a# c5 ^+ d* R. A L Else ' This row doesn't match - go to the next column
. B3 P" A' B, l: {. {1 S' O Match = False3 A1 }" \& ?1 u
Exit For! l# ?* F0 [+ j7 m7 s" `
End If
' _$ E E. H$ |5 h" @ Next r0 A1 r1 A7 I; T; b
If Match = True Then ' Don't search any more columns
, |) F$ |# X, {9 Z cindex = c
+ h f( i3 v+ \7 F6 V Exit For3 [1 W9 R. _9 }+ d7 O
End If: q; l: }" J$ \$ o# L& [1 E
Next c
) Y$ N8 ^3 Q8 J% k0 T8 E* L9 X' |, @+ @5 E% U
If Match = False Then ' Didn't find a matching set of values
' w0 D; Q/ e9 r" h ZZL = "No match for columns"
! e* a3 q0 H" ~4 ^4 N$ I4 y2 X Exit Function
' C D+ s3 [0 m. F End If
: t4 Q& G, X$ T* p% k; q6 l* j l9 z$ Y& x. d
cindex = cindex + ColHead.Column - 1
1 d) y# @/ {- U7 DEnd If
- k, U7 p% h' e3 M+ t- E$ c$ f9 e& z; u# O5 J0 W
' Return the cell value from Table
2 W4 h: T6 e0 p'debug.Print "Answer is in (R,C): " & rindex, cindex3 ?# x; L: J$ u' o$ H
ZZL = ActiveSheet.Cells(rindex, cindex)# Y2 ~) `! E: j7 d! E
'debug.Print "Answer is : " & ZZL. E" S! F9 S" V1 F8 y
Exit Function8 k; d X' O; H$ Q0 `9 Z! p7 L' ]
1 I6 L& E4 Y7 Oerr_handler1:! x; h( w$ X) |
ZZL = "Error on range '" & rngname & "'"5 }; |$ c+ n2 V" s2 |
0 v. f" l, k) J$ u/ lEnd Function
' h+ `) w8 V/ d1 J0 q! J- y5 G+ G9 b L4 V) i6 F
|