Option Compare Text
8 C/ B& d1 M2 D3 F3 E( q+ C8 B$ H7 p7 |% _
Public Function GSXS(Ref)" \7 E" E8 C3 T' o7 x6 ]
" A; y7 d# Y" p: I; F
GSXS = Ref.Formula
& W, e; N- n [! z: y7 u/ u7 X7 K, i, E _8 \
End Function
. o5 `: V1 `' Q4 o
, C2 s/ T* x' w6 F! H* u- P! J# DPublic Function ZZL(RowHead, ColHead, Dummy)! X+ F3 M! L0 v2 C
/ t, _: V7 V7 D3 v
Dim Values(20) As Variant
' _- {7 Q% b" `7 gDim PrevData(20) As Variant
/ V1 K2 M1 q. d' q2 KDim LE(20) As Integer$ g& h) {# F. c8 B
$ K% R3 C5 ^: R7 b4 T( V
On Error GoTo err_handler11 {7 c& \9 B5 x# s% D
' Do the vertical selection from rows0 [. t# G4 w) F# ~" I
If RowHead.Rows.Count = 1 Then
" [; T' Z( E7 I9 \7 _ rindex = RowHead.Row ' first argument is any cell on the row of possible values1 f- X1 L8 F( \6 X
Else' X3 u& r7 t; I+ A6 E- a
' Store the values to be compared with each column; N1 u9 p/ |% k- v" N, ~
For ii = 1 To RowHead.Columns.Count* O: m& W; X, U& f; ~8 r
rngname = RowHead.Cells(1, ii)
, h c2 j! C- T* ~$ P: a LE(ii) = InStr(rngname, "<=")( X, |+ ?% |+ b; g& a! o
If LE(ii) > 0 Then: l' u7 K7 I6 Q/ m5 x
rngname = Mid(rngname, 1, LE(ii) - 1)
; L+ f9 F* A* b8 p+ S) d End If8 g, D& i/ Z3 |% ^/ n% R
Values(ii) = Range(rngname)- S0 j! b# m. H0 U
'debug.Print "Variable:" & rngname & " is:" & Values(ii)9 f/ ^5 G6 z6 |2 o5 Q6 h
PrevData(ii) = "" ' initialise: i: N& z4 R; T/ f
Next ii
$ Q$ S: v' |0 M. X+ d6 ]% E3 n. \3 G
rindex = 22 e" j# O% }3 c5 P( g6 \+ R
'debug.Print RowHead.Columns.Count
n# C: Z$ u& c9 x: w# i0 |' Z( v Match = False
1 l0 E6 S, Y9 e W5 P9 w6 e0 Z l For r = rindex To RowHead.Rows.Count
! p8 s5 o2 Y D6 F6 [ For c = 1 To RowHead.Columns.Count ' for each dimension
' w) P' S6 T1 P data = RowHead.Cells(r, c)
4 c: u0 J: U# j" @' y If data = "" Then
$ R4 n2 E: ^: k: k; S7 I 'debug.Print "Empty cell found: using " & PrevData(c)8 J1 n9 p( z) V" B* n
' use the last valid cell in this column
# O! S- r8 K: @9 G' h ' (this is to handle merged cells)
6 x4 V" c( Q! p. ?0 w! p" V data = PrevData(c)
& C5 r& |( S% Q( C* I0 p! X3 i End If! D9 y9 ~6 I! \, s/ b$ [4 L
'debug.Print "data:" & data
# g/ M" `3 M! ^6 n PrevData(c) = data ' save for use by empty cells) ]: z. v/ p: |) R$ ^0 U! Y
If data = Values(c) Or (data > Values(c) And LE(c) > 0) Or data = "*" Then
: ~$ d/ Y4 P1 A" J If c = RowHead.Columns.Count Then ' All columns match - It's a go
+ ~4 i. s% ]7 T# x- e Match = True% O$ c3 q. g; |/ D. h- o. c
End If( L8 U: i m8 \# C# e! G
Else ' This column doesn't match - go to the next row
) p7 }: Q: q/ n* q9 X Match = False# r( ?+ z" D% e
Exit For0 L' I- b# h1 {. ^1 }2 o0 v# s" m
End If; `5 \/ s* i z/ Q
Next c
- {8 W9 o! ]! {1 V If Match = True Then ' Don't search any more rows* d t2 a4 q% l, K( t, S
rindex = r
$ v9 k- {1 E6 \0 x, X e1 y* @/ P Exit For
& ?& y: h, }! M" v End If
k0 N# V& F$ |6 z9 M Next r* J3 s6 }% ?$ M2 O7 H( d2 `8 e
3 X5 k! i8 w9 S2 Y2 \" a If Match = False Then ' Didn't find a matching set of values$ u% |/ |% r+ V1 Y7 [
ZZL = "No match for rows"( z: E# T, i L
Exit Function
X7 w+ G* T) R" p/ X# ~ X. H$ m# L End If
$ ^# X( v6 W" Z$ P: ]% o" _% \5 b+ L. b. g0 ^ a
rindex = rindex + RowHead.Row - 1 ' make absolute index
! d4 U! d5 {- w7 `End If! v$ \* D3 W( x! k
" ~ z; k2 ]- \4 b' Do the horizontal selection from columns6 V+ C: P' Q b4 d
If ColHead.Columns.Count = 1 Then
/ u. y& x& U# S; Y" {5 P$ ] cindex = ColHead.Column' q+ {8 j- b8 G. q6 {$ C
Else
/ k( M; `% x, x4 M+ E3 d ' Store the values to be compared with each row of the header
" B0 z3 A0 I" V4 j8 \ For ii = 1 To ColHead.Rows.Count
) l: L- V& Y" m3 @$ y5 @ rngname = ColHead.Cells(ii, 1)
! Y& M9 s$ B8 W6 h# j) X; I1 I LE(ii) = InStr(rngname, "<=")2 P$ E3 s0 S s W# d
If LE(ii) > 0 Then/ N& U/ K: g7 N- K4 y# L* p
rngname = Mid(rngname, 1, LE(ii) - 1)# h/ p1 u/ b B
End If9 b7 h% L; L( E' N, j1 V! |! A( R
Values(ii) = Range(rngname)$ |# _# f4 c$ }. N6 Y) ?# v
'debug.Print "Variable:" & rngname & " is:" & Values(ii)
/ \6 }# q+ Z5 m+ X' j/ j0 ] PrevData(ii) = "" ' initialise9 _% e- l3 E3 {$ E. | r' t
Next ii( x- g; ]6 \ \' \
; Y1 J# B7 [" i$ [( L cindex = 2& @! c. U( W6 I* x* u8 K
'debug.Print ColHead.Columns.Count
/ X3 t b9 a9 j$ d1 i Match = False
) x9 T' T C8 t+ O5 Q6 L0 e: O For c = cindex To ColHead.Columns.Count
) L5 E$ H% T: R `! q+ L: x For r = 1 To ColHead.Rows.Count ' for each dimension
: y5 j1 N' j6 ]9 a0 k# B data = ColHead.Cells(r, c)
7 z; e; }# X" n" `7 `9 t If data = "" Then% k( @/ m7 m# c" R5 x0 n# W
'debug.Print "Empty cell found: using " & PrevData(r)
& K" v- T" ~: }2 J0 U" K ' use the last valid cell on this row! L& L8 B P8 K" V+ o
' (this is to handle merged cells)
1 }* r2 B6 K; ?" s! \1 R$ s data = PrevData(r)) J0 X% }+ ~! I. q2 E2 l" A
End If
+ s3 }+ D3 z- |7 _7 k) ` 'debug.Print "data:" & data
; M$ L& G- G! g8 F PrevData(r) = data ' save for use by empty cells8 C* H9 S* e/ L$ Z% o# D
If data = Values(r) Or (data > Values(r) And LE(r) > 0) Or data = "*" Then
$ H( |1 N- h1 y5 ^' l If r = ColHead.Rows.Count Then ' All rows match - It's a go
9 E5 j1 _! g( m9 J2 e5 r/ O7 H! T Match = True. e& n* _! P3 ?- K9 e) \
End If: V* x! s' T' M) m
Else ' This row doesn't match - go to the next column- R/ b% m. A( Y- M
Match = False
' x( b3 q2 t, `$ q& W# |7 e( t Exit For
' R$ I# k8 z9 _ End If
) o' ~5 k+ A7 ^, k Next r
3 `& T! W' n3 n2 C) J If Match = True Then ' Don't search any more columns' s U7 _. M" B3 c4 ^/ Q+ t
cindex = c3 k) [7 O( C X
Exit For2 V9 |8 q% u, w7 d( [( O3 e' o
End If# R" c( I7 Y! q) v( z" I
Next c; M+ v1 U( ?. g: ]. T
4 m1 H( K' L# G/ T9 _) N
If Match = False Then ' Didn't find a matching set of values8 [3 y' \4 l, N3 m; d
ZZL = "No match for columns"6 Q6 L! `6 k- [% {
Exit Function! m0 |' ]! `' r. }8 C$ Z
End If W1 ^" M% N! I2 w+ l' i* G& ~- p
% i/ f3 _* Z' }* u7 G
cindex = cindex + ColHead.Column - 1
V3 V$ [+ I2 TEnd If! ^% y4 |) e# b" i
3 a* v' C" Q3 z3 {7 z- c' Return the cell value from Table
+ I' K% ^4 {( K" _8 F5 U: ?. N'debug.Print "Answer is in (R,C): " & rindex, cindex
- A/ ]- C, R5 a* v7 P6 VZZL = ActiveSheet.Cells(rindex, cindex)0 W0 ], R- ]* E; u
'debug.Print "Answer is : " & ZZL
6 g0 ~; _+ W+ W7 x3 x5 CExit Function
& [ B7 G. R! B+ |
7 H! O; f, u) {9 Xerr_handler1:- C8 U6 P" y; i
ZZL = "Error on range '" & rngname & "'" z' Z4 g. R4 X# F- P
" o5 q, E v2 [. i; Y/ y- u* v8 G# f
End Function3 M0 `& b7 h2 _, K' M
- b+ u) O/ L6 }
|