Option Compare Text
: V; C0 q# S0 r
+ Y4 }, H; V1 Q* }Public Function GSXS(Ref)6 j: r, p$ X0 W: \$ z9 f W& R: S/ v
7 z. f0 I* W- A. \- t6 M' H
GSXS = Ref.Formula
; |. @1 u4 S& d- _" {. x" z
2 } n. I0 b5 DEnd Function3 h+ N) t$ s* ?! x
7 v+ Z# r* b6 n% ] g
Public Function ZZL(RowHead, ColHead, Dummy)
' d4 t4 o2 ]5 G' S# G) Z7 }* N9 I, Q7 @8 q: P
Dim Values(20) As Variant) t) r0 R, i# S
Dim PrevData(20) As Variant* d1 m. i o6 h. l* `
Dim LE(20) As Integer
- P" T: u8 K% ]+ F* O+ P. k2 n! J; B& l' w) c, ~% k
On Error GoTo err_handler1
0 k# _$ S6 O4 Y! @0 L# {1 _' Do the vertical selection from rows
# h+ l) j( |, T3 o. V. p+ |5 ?) C2 }! \If RowHead.Rows.Count = 1 Then
" |" y* @* U3 t! @ \ rindex = RowHead.Row ' first argument is any cell on the row of possible values
$ M% Y8 @4 P Z8 Z6 w8 @Else# w& c6 y# c' [2 K
' Store the values to be compared with each column
( \. X) h( S: D For ii = 1 To RowHead.Columns.Count; E# P$ m- o) D9 y0 w
rngname = RowHead.Cells(1, ii)
~6 o, @4 v6 v5 a: b6 i LE(ii) = InStr(rngname, "<=")
5 U( R" {) ?1 D4 K/ U9 N If LE(ii) > 0 Then
4 C: X: L6 t# H rngname = Mid(rngname, 1, LE(ii) - 1); ~; C6 Z/ ~* N* V6 f8 q4 r/ u
End If& k3 K9 `% r8 A
Values(ii) = Range(rngname)
# T0 w6 ?: Z9 E# y* A' f 'debug.Print "Variable:" & rngname & " is:" & Values(ii)
' o1 n; ~% L$ Z0 x0 z4 T8 W PrevData(ii) = "" ' initialise6 q& E; C2 R9 u+ v( d9 X! U8 f
Next ii
/ D# G! `7 J5 l
3 L- K+ [" Y, E+ N rindex = 2
. J8 P! \3 i' D Q 'debug.Print RowHead.Columns.Count$ p, E# t) k: g' Z
Match = False# @8 G7 h2 B% ?4 b; O/ T
For r = rindex To RowHead.Rows.Count
' F( M6 |/ j: i For c = 1 To RowHead.Columns.Count ' for each dimension7 o! W2 P, T4 R5 W) I. j
data = RowHead.Cells(r, c)/ l. H6 n& Y# O7 Y
If data = "" Then
! N4 z3 N! d' G# d4 t3 u7 o7 U 'debug.Print "Empty cell found: using " & PrevData(c)$ L1 t$ q, ~' @! a( g( N: A
' use the last valid cell in this column
0 g. J, ~; T0 c. p( s ' (this is to handle merged cells)
6 r0 s3 n/ \2 s5 t5 c* L4 r% D9 ` data = PrevData(c)
" o: H7 S0 `3 ]; ~- i End If, ~+ ]' |( V4 r. s
'debug.Print "data:" & data0 H5 l9 B+ S% j4 I. y0 s! K
PrevData(c) = data ' save for use by empty cells6 Z% B$ l0 l8 e8 c
If data = Values(c) Or (data > Values(c) And LE(c) > 0) Or data = "*" Then
5 I7 q8 `2 U2 \ If c = RowHead.Columns.Count Then ' All columns match - It's a go
7 C4 J0 L1 S, X0 r. K. | Match = True
% z6 L8 }% J; f) D End If4 S- ?- x" [! ^$ }, Y6 i
Else ' This column doesn't match - go to the next row
3 L3 k7 n! B2 g. D Match = False8 m& [% p7 ]9 u6 i: v, ^
Exit For$ w" A) z1 M; ?* ?1 {
End If$ [% H* w! j, \1 z
Next c
6 M( ` l1 E2 h) p If Match = True Then ' Don't search any more rows
3 A, j" Y+ I2 G& E rindex = r
/ u* F. ]5 y! G1 L$ K9 z) V/ @ Exit For# j! M5 c' ^$ J& c( S% E8 f$ n
End If2 S# s- T7 B; {8 V, ]6 l1 `4 J' S0 u
Next r
7 h5 g' H1 _/ P& P7 G" q9 H6 O4 b& H7 Y |1 N1 m, g
If Match = False Then ' Didn't find a matching set of values
5 E; @6 J9 U0 ]% x3 G7 j5 L ZZL = "No match for rows"- l) [# b. K" C I4 h5 ^3 H
Exit Function s4 v# W( A7 z" E( h( z+ V
End If
$ ?1 y* ~6 ~( @; q6 R& F9 Y! _' t6 r9 }8 L
rindex = rindex + RowHead.Row - 1 ' make absolute index8 w" c$ N C- V% W0 e
End If9 L1 k. b" |* N5 B
- q( N' K* g' h' Do the horizontal selection from columns
1 n3 Z' I% U8 d6 M' IIf ColHead.Columns.Count = 1 Then
; t0 e: w: p+ W2 z2 W cindex = ColHead.Column
7 N# w) S6 u2 Z# \/ Z7 E" O& i6 sElse3 C& m. c; q$ ^! L$ h1 `
' Store the values to be compared with each row of the header4 E: I3 A3 M- ~
For ii = 1 To ColHead.Rows.Count* l8 ~: P: F! G
rngname = ColHead.Cells(ii, 1)
- R( s8 M" e3 v; ^ LE(ii) = InStr(rngname, "<=")! m0 y! l4 h4 K' C; S. t
If LE(ii) > 0 Then
& `$ i4 _: D \+ m6 O7 a rngname = Mid(rngname, 1, LE(ii) - 1)" q$ }0 Z* }3 B! g
End If3 t3 f) M3 \( T' e! Z% e4 c
Values(ii) = Range(rngname)) ?- i' }9 O, h- }0 L% k
'debug.Print "Variable:" & rngname & " is:" & Values(ii)6 G5 Q! W- h$ u8 h
PrevData(ii) = "" ' initialise
1 C4 l+ Y8 w7 l2 B9 p- } Next ii5 }2 F* Q: d- E" v
( y+ F) B6 W2 h+ f cindex = 2
# ~$ B) a- g/ ~: @ 'debug.Print ColHead.Columns.Count: q; w D+ R; E" D0 G
Match = False
2 }5 z0 Q1 q7 o2 U( p! a For c = cindex To ColHead.Columns.Count ]* Y t$ ?9 N W8 X0 M2 {
For r = 1 To ColHead.Rows.Count ' for each dimension1 T# U' i) _. M9 n$ \* D) ~4 h
data = ColHead.Cells(r, c)
5 h" r+ R( M @ If data = "" Then4 M" \# S5 P$ K% A* Q+ |
'debug.Print "Empty cell found: using " & PrevData(r)( Z4 U9 N' n( a7 F Y& z
' use the last valid cell on this row- ^8 ^% `3 N. [1 T; s
' (this is to handle merged cells)( s2 D9 Q, c* t& Z
data = PrevData(r)1 {. F' n4 ^- [4 Y& t! ]% o5 c2 O5 X
End If: P- P* J# t+ z5 |; t" n
'debug.Print "data:" & data$ u: v4 o2 l+ p! v9 M
PrevData(r) = data ' save for use by empty cells
, l& C0 |; x, J U3 d If data = Values(r) Or (data > Values(r) And LE(r) > 0) Or data = "*" Then
$ n7 t S$ k5 p" c2 T4 c0 H If r = ColHead.Rows.Count Then ' All rows match - It's a go6 l5 y* ?7 ?% m% r5 A$ x
Match = True, Y: X2 ]7 [, j: k$ B5 x# r* w& V
End If
K. l* |+ v2 X# i1 p" `( Q' f Else ' This row doesn't match - go to the next column1 l4 N* q+ M6 B* p, B- ~
Match = False
7 \- }7 H# L* o1 q Exit For
, \/ ~3 m Z9 W/ Q+ r0 G0 ` End If
: H2 Q1 D% F+ Y8 z& {0 c Next r2 V3 @. G* c: Z& X% \# A$ b& m
If Match = True Then ' Don't search any more columns, n4 ?( h7 _0 H$ h
cindex = c
) q4 l: O; R8 U9 O- P, F Exit For
( n/ u4 a1 K W. y0 p, M End If
$ n" Y* g' u7 H: X) A, q" Y Next c5 ?1 i* M" W* ?! N2 ^0 Z6 o
# L. ]9 R) [, u
If Match = False Then ' Didn't find a matching set of values5 Z8 }# q* K' g' {
ZZL = "No match for columns"! j' P, C8 a, q$ a6 D
Exit Function9 l8 i: g% j; Z+ b
End If2 ]" {, S. t& ?7 b9 \& r
* D4 ]6 J6 h# [2 U3 |0 c cindex = cindex + ColHead.Column - 1
) l& S# M1 U5 m7 s$ k% e8 _0 C, rEnd If% \* R) [( ]9 R# c3 }" w# O% ^
' D1 C$ Y! i# ?0 q5 S2 S: d$ ~
' Return the cell value from Table
' B6 r" c' D4 J) ^'debug.Print "Answer is in (R,C): " & rindex, cindex( g/ b# r" e: i1 B/ ^) a/ R
ZZL = ActiveSheet.Cells(rindex, cindex)# c7 l4 l; y+ e) H5 p) H- C
'debug.Print "Answer is : " & ZZL* Y2 h5 q8 k( f
Exit Function9 l3 A, I) u; Y4 v& l* l* v
( V7 x6 Z$ O# B
err_handler1: e# y2 E% N0 A- C$ t1 m
ZZL = "Error on range '" & rngname & "'"
" x- ^7 ]: a4 O. l7 H+ Z2 i+ x1 { P# O
8 I# K- Q# t8 i$ f+ ZEnd Function
$ o8 N d2 z& e0 H/ [4 D6 j7 r! n; p5 Y! I& Q! K
|