Option Compare Text
- T; r4 Y& i8 S* D4 a% D' ^ \8 i- K+ M( I+ e, {. I9 @
Public Function GSXS(Ref)2 e! {' l/ f f O" ]% q
2 b1 ~" O: q) z# w" X8 F3 }% A% y GSXS = Ref.Formula8 S" l2 u$ R; @4 z, ^* b
! {: q7 z, f0 k. m% {0 L7 X; p
End Function/ ]% r" [, _5 f3 K/ _4 m" B
1 H3 J7 j9 t, U# TPublic Function ZZL(RowHead, ColHead, Dummy)5 j% T- {: ]0 ~0 @) P( o" o
0 O# }5 V4 p: G- PDim Values(20) As Variant; q1 n. @8 F1 S. Y0 u
Dim PrevData(20) As Variant
% _. _6 Y6 Z6 D, N* [! YDim LE(20) As Integer8 p* ?. Z0 {- U2 z9 ^; K
7 n, a" e X4 G, r2 m% @
On Error GoTo err_handler19 E! K2 E3 J. f1 P/ @& M
' Do the vertical selection from rows
3 ~! `4 `7 U, }& uIf RowHead.Rows.Count = 1 Then2 ~) n9 M7 T6 W# l( o5 g$ W, m7 G
rindex = RowHead.Row ' first argument is any cell on the row of possible values# r2 F! G$ e, E0 _& ^* S( u
Else6 _+ P: m! p- B- r/ a7 L- I& P
' Store the values to be compared with each column3 B3 f4 D) r: }+ ~, D
For ii = 1 To RowHead.Columns.Count. I. O. U& T7 t. Y# ]* i, x: Z
rngname = RowHead.Cells(1, ii)
( ]' J; R6 q N LE(ii) = InStr(rngname, "<=")
! \9 \3 { s& I. I, [ If LE(ii) > 0 Then2 _9 O9 Y8 S% ?* J
rngname = Mid(rngname, 1, LE(ii) - 1) S( w; _+ \/ |1 l4 o
End If0 p: n# |. ]0 s0 }6 F0 g
Values(ii) = Range(rngname)
; C4 I0 }1 C6 n3 p7 i 'debug.Print "Variable:" & rngname & " is:" & Values(ii)8 \7 V7 n- k7 M, G1 |
PrevData(ii) = "" ' initialise
/ |3 m9 h# ]. \% f9 u% w Next ii
' o7 U' y; G1 a7 H: m, n( v
& D7 \/ s6 x( |4 U7 K# O3 ? rindex = 2) J; z/ F" ]% D; G; @
'debug.Print RowHead.Columns.Count
! m- Y/ d% j9 U0 I* _5 J# h Match = False% ~' q# K4 Q- I y
For r = rindex To RowHead.Rows.Count6 d5 k% A" I; b$ N7 v
For c = 1 To RowHead.Columns.Count ' for each dimension& I. x6 E) Y* ~2 j5 F! ]" N
data = RowHead.Cells(r, c)( m2 `6 r4 V4 K( I
If data = "" Then" T4 r+ _# L9 L+ Z9 ?- l
'debug.Print "Empty cell found: using " & PrevData(c)) P7 h- o D0 W _) o6 C
' use the last valid cell in this column( W* B$ ~* Q! }0 F. j
' (this is to handle merged cells)
1 W4 \2 ~: F j8 I7 |# `( F data = PrevData(c)5 d0 v* T9 F( X
End If- y' l2 S A9 |1 f: b9 h
'debug.Print "data:" & data& x1 \+ c% n1 i0 x; z
PrevData(c) = data ' save for use by empty cells7 P3 a( A. D+ v, s) h {! J; g
If data = Values(c) Or (data > Values(c) And LE(c) > 0) Or data = "*" Then; _/ C7 t6 Y/ L$ _6 ]4 p' E
If c = RowHead.Columns.Count Then ' All columns match - It's a go
! J' u2 ^# I0 ~! h Match = True( n. F5 T% w: p$ g o" b: N O
End If
, V& d5 ^4 w0 _& L* j K Else ' This column doesn't match - go to the next row
( b( i/ ]$ }9 T! W [- X Match = False d" q) Z" ~' I% d
Exit For2 Y9 ~) P& n1 [4 `1 J# C+ c3 `4 t
End If% c% j+ d F( {, y
Next c
( b6 c: `; V% b" S6 C/ r/ W If Match = True Then ' Don't search any more rows
$ Y Q" T) f. C rindex = r, T# W6 u5 L s
Exit For0 t9 Q* H2 I0 E
End If
3 U. X6 C2 |# w5 A6 ~ Next r. C4 w( I, F) ?: w C7 M. @
, U8 i0 Y/ l* x# G If Match = False Then ' Didn't find a matching set of values
5 u5 w6 C' x, i; y& v ZZL = "No match for rows"
/ ~3 V. \0 i2 a$ ^: @ g- o Exit Function* }* c5 Y% F r" c ?9 f9 ^
End If
+ D4 U9 J' Z- Q* T& \; J i) K6 ?/ k8 A# g/ M8 U
rindex = rindex + RowHead.Row - 1 ' make absolute index
* A. `! }% C* ^# _2 R9 D6 TEnd If7 y% {( q7 D* _2 m, k' M) H1 R7 B
- k+ d3 E; B& S" N9 @/ P7 N( v
' Do the horizontal selection from columns
" ^: p! y8 z1 S1 R: e+ R: Y, hIf ColHead.Columns.Count = 1 Then9 L, }% A3 d2 \5 ~
cindex = ColHead.Column- g6 Q( a4 ]- j6 k5 z& Y
Else
( B0 g3 F' g: Q1 E! E8 c* k ' Store the values to be compared with each row of the header" o; W! Z) q) h
For ii = 1 To ColHead.Rows.Count
5 T5 N, t% {7 {$ h$ p' o* G: O* [# w rngname = ColHead.Cells(ii, 1)
' K; K/ [8 f" ~2 K* Y; Q LE(ii) = InStr(rngname, "<=")
% m- w! Y( T% ?8 a Z If LE(ii) > 0 Then0 }7 [; B- @" k( K0 L& z
rngname = Mid(rngname, 1, LE(ii) - 1)7 L0 g V0 w: Z8 |4 p& j+ w
End If
' e0 n6 v) f" I, p Values(ii) = Range(rngname)' A4 u# T$ q, ~3 j$ R
'debug.Print "Variable:" & rngname & " is:" & Values(ii)
; a9 r. a2 N& z. y/ Q$ R PrevData(ii) = "" ' initialise
Q6 [, d% i1 |8 L5 {1 N6 l Next ii
1 G0 h& j3 B7 U6 \8 v1 T- h/ {$ |. z* P0 J
cindex = 2
1 Y5 W" ^' }" o' a; h 'debug.Print ColHead.Columns.Count0 N: X8 o, l9 m9 `0 A. `
Match = False
# v M4 \* f. M2 N4 K# t; _ For c = cindex To ColHead.Columns.Count
1 H( e8 q$ O; T5 r* ]% v* b) f For r = 1 To ColHead.Rows.Count ' for each dimension# F8 o" b- D( D/ _/ G! J
data = ColHead.Cells(r, c)
6 s7 v! k8 j7 M If data = "" Then% v+ d- d+ D: j, t! X5 F
'debug.Print "Empty cell found: using " & PrevData(r)3 a! g3 x& o1 B7 j+ `) Z
' use the last valid cell on this row
' b6 Q: [8 d3 r( P2 z" I- \ ' (this is to handle merged cells)7 ]/ {5 }7 K% J+ n
data = PrevData(r)
$ u) W6 z7 s7 p$ b% B% J) L End If
: q6 D; `/ f; Z$ H1 C 'debug.Print "data:" & data
/ }2 q2 ^+ S/ F7 f8 l8 N7 K3 m PrevData(r) = data ' save for use by empty cells% W8 A4 u9 l) @$ i% x, | k2 j
If data = Values(r) Or (data > Values(r) And LE(r) > 0) Or data = "*" Then
3 }8 K* A. l" h" _2 Q7 T If r = ColHead.Rows.Count Then ' All rows match - It's a go( I% ~# O4 _2 f: x# D
Match = True
0 A" n5 l" C/ t/ h5 g End If" p, ~' T7 ?6 y% Z8 |" H! N% \% t
Else ' This row doesn't match - go to the next column
1 e2 _3 T1 a* M0 B# T# | Match = False
0 O' R! n* G) @, N6 D* ~8 T, l) y Exit For! c* n6 F4 G% e
End If4 F6 `+ G n4 m# {; `
Next r3 C/ |/ g' J, n# E$ B/ j. d
If Match = True Then ' Don't search any more columns; E: _( z6 X& S& M: K' d
cindex = c. K3 R6 W. C+ y2 w+ o: H* n3 @
Exit For
( w' m1 t2 \7 d9 a1 \. { End If
- E: _, F Y& H9 S& S) t+ I Next c; @' V; L: h$ [9 H! L3 K
( B5 A. d* q5 y If Match = False Then ' Didn't find a matching set of values
- D6 W+ ?* |7 N3 G ZZL = "No match for columns"6 O: G1 s* }- r: }) \, N5 s; e O' H( j
Exit Function3 [6 v% ^- {' e% ^
End If; i [, R+ x! h1 B
1 n6 ^# R! k2 {' Y2 E9 {% H
cindex = cindex + ColHead.Column - 12 [! x9 F0 v, w! J
End If2 i: k* P+ s0 N. H2 u
7 {: @, @* e1 L: z3 Z' Return the cell value from Table
8 V( ] ]& l/ b- @% y'debug.Print "Answer is in (R,C): " & rindex, cindex8 F% P- m0 K7 Q# o- [8 L8 b
ZZL = ActiveSheet.Cells(rindex, cindex)( U3 H% a: U/ r" T: f$ a
'debug.Print "Answer is : " & ZZL6 V: U) U! u7 u# s& [: d
Exit Function
# k2 r _* z' v! Y
( p% U$ P& j0 \4 O n$ _err_handler1:: ~) W1 J& ^: r# E3 R
ZZL = "Error on range '" & rngname & "'": J( r) h4 t, |7 Q& w+ Z0 W, c
# j& i y7 f6 D( S
End Function# C% Q" d: @' W( C/ _0 A% k
4 s) s; c, q3 ?- |' o1 \ |