Option Compare Text8 J5 g, D6 g1 `
. y$ O; F0 M1 E0 Z. X7 w+ B7 N
Public Function GSXS(Ref)
+ u5 T3 @2 P% c4 a# ~, m1 }! a z0 C
GSXS = Ref.Formula+ L2 @, h0 [! ~6 a
1 g7 M8 C* Z7 ?( U! V
End Function
/ |7 \: i" |# }$ M% j" F& `* m: g( w$ ]) ~ |: T1 K+ L
Public Function ZZL(RowHead, ColHead, Dummy)7 J- n: U* Z; s
% Q& N2 {5 M2 s9 s) v& Y- D
Dim Values(20) As Variant/ \& j) N* x# C1 `1 g [" O0 e
Dim PrevData(20) As Variant y, |: |9 Q; v' k; i
Dim LE(20) As Integer
; d% o: C, ]3 w$ C* C0 G8 C6 r9 o7 Y
On Error GoTo err_handler15 J- l+ | }4 A1 I4 G0 G
' Do the vertical selection from rows: x8 |$ j# x: F v
If RowHead.Rows.Count = 1 Then% h, i( Y: [, _1 J% H, w( a
rindex = RowHead.Row ' first argument is any cell on the row of possible values
: y$ } M# P0 f* G& Y: c% gElse
2 |$ @: K4 y7 Q' b4 S ' Store the values to be compared with each column2 J/ L3 x! a, I9 g: M f+ b
For ii = 1 To RowHead.Columns.Count
4 F0 Z- z7 p( \* R rngname = RowHead.Cells(1, ii)7 o% L0 r5 ]' G$ Z
LE(ii) = InStr(rngname, "<=")0 F$ [ a: y5 K
If LE(ii) > 0 Then
2 l5 J5 f: X$ K4 z/ | rngname = Mid(rngname, 1, LE(ii) - 1)
. ?$ n: P6 X, d* _+ I, [/ }3 Z End If& A% ^7 K0 M- k' b5 i
Values(ii) = Range(rngname)
2 u( T4 M; T! Z; R4 t$ p( f 'debug.Print "Variable:" & rngname & " is:" & Values(ii)
; n4 I% `5 Y1 Q5 S1 O5 j PrevData(ii) = "" ' initialise3 ~/ e1 Q, \! {! K p: F
Next ii
; Z9 c5 r6 b8 X. d* E! i9 l( y/ B+ w( h
rindex = 26 J( C( J7 x% F8 v2 `! n# S
'debug.Print RowHead.Columns.Count9 T8 S" n1 m, D! Z9 B
Match = False7 _+ o6 V) }) u- r8 k
For r = rindex To RowHead.Rows.Count& D% w7 t7 Z6 R6 p
For c = 1 To RowHead.Columns.Count ' for each dimension0 M% L7 f; E4 ?* O" S
data = RowHead.Cells(r, c)2 r& D2 b0 n' e# X' g8 z, t6 ]6 w
If data = "" Then
9 Y5 C' Q8 |; Y 'debug.Print "Empty cell found: using " & PrevData(c)9 s- `4 n2 F8 Q4 h8 O
' use the last valid cell in this column
1 O; K' j3 Y, M9 P ' (this is to handle merged cells)
: c. i8 d' P3 P% ]8 T. P data = PrevData(c)
4 u& [: n0 c) r2 J9 m) K2 m End If
" D3 V8 Y: z0 y* u1 A: V 'debug.Print "data:" & data) N+ R6 U+ W& d( ^. |
PrevData(c) = data ' save for use by empty cells, K& v+ V) s* H g
If data = Values(c) Or (data > Values(c) And LE(c) > 0) Or data = "*" Then
' _9 g# M# C# C! g3 J' @- Q+ n If c = RowHead.Columns.Count Then ' All columns match - It's a go
3 j' S8 M$ [- N, ~ Match = True' K5 {3 I& C' D7 o' d L z8 s5 u
End If
' x2 G5 [0 [8 e" j Else ' This column doesn't match - go to the next row% J! Q" ^( j5 y6 V; l
Match = False8 Q- b9 i$ d7 J( x$ k5 }( S8 o
Exit For
; `3 p" t' ?$ n( Z# K( J7 X3 ~ End If& O8 Y, O' d% M ]$ ` l' X
Next c
7 J# [- M5 s2 h/ a6 n5 A If Match = True Then ' Don't search any more rows* Z7 j2 L. V! x! V6 M' c
rindex = r k! M* ^: R% x. N# y4 v
Exit For
, ]. [; H3 l& c2 R: f. P0 k2 Z End If) x" @3 }( D5 x5 R {# i# D2 C8 M
Next r3 p8 \. F4 Z$ J: C9 |- l N4 ?
# D: s+ J2 @" k# M6 R If Match = False Then ' Didn't find a matching set of values
" ]2 x0 W. D# G1 M- P6 G s ZZL = "No match for rows"& o, Q+ n8 ?3 J+ h$ {
Exit Function
2 y& t( c: _, e- `" r End If4 N8 _9 J# c4 g; o( Q
) f6 w1 T# d% O: a rindex = rindex + RowHead.Row - 1 ' make absolute index/ _; k7 X: e1 e' t5 Z1 U
End If
+ X3 ]- ^. k, K& u: [9 p" b- z, H1 `$ X6 U- }, A) [& B3 e; d* n
' Do the horizontal selection from columns2 m# b# r4 N) a* { \. f* w
If ColHead.Columns.Count = 1 Then
/ D1 S p8 W" x) t- s( e; l cindex = ColHead.Column4 \7 B+ r4 W; x) _% C' O
Else
9 ^6 d0 B! t# Z ' Store the values to be compared with each row of the header9 ?) U. G, D) m4 P( f
For ii = 1 To ColHead.Rows.Count
; I( E" o; g' p! Y* M, w% Z rngname = ColHead.Cells(ii, 1)
5 X! m/ f6 K: {6 y3 r LE(ii) = InStr(rngname, "<="), ^ r; k# I& x% Z
If LE(ii) > 0 Then9 \: f$ J7 ^8 @' n+ w/ x s
rngname = Mid(rngname, 1, LE(ii) - 1)8 f7 @% l2 i% P B X3 L' ]
End If
2 F( g# I5 k* ~5 z. B' G Values(ii) = Range(rngname)' J' v5 l# P( X; n* [( P% i
'debug.Print "Variable:" & rngname & " is:" & Values(ii) x: d, D- h5 J" g
PrevData(ii) = "" ' initialise! w' r1 a; k7 J% K2 W" z
Next ii$ Q x; E" ?7 D* S# ~
( K& `2 a S$ M5 z
cindex = 2
) L' j% E$ p7 A/ f- {6 T/ Z' N( U 'debug.Print ColHead.Columns.Count
! b8 r# Z% }9 y, e Match = False3 U2 Q( \0 y, M d7 `" N
For c = cindex To ColHead.Columns.Count
" ~6 I4 c+ x6 r5 T For r = 1 To ColHead.Rows.Count ' for each dimension( y9 P' p8 U% k- |, x- U0 H, a
data = ColHead.Cells(r, c). P" Y9 T8 D! t9 ]+ Z8 |' g* ^
If data = "" Then
" x( | u2 I; A- {, _; o/ \$ w 'debug.Print "Empty cell found: using " & PrevData(r)
1 N' i) g b/ ]3 S- D# U ' use the last valid cell on this row: L Y4 [! L0 {5 H; }7 r: ~. A
' (this is to handle merged cells)* j& W a6 A, e1 @% k) z! r
data = PrevData(r)0 E9 l2 o3 Z: R0 S9 \
End If# L/ {' C* {- E8 G O5 K
'debug.Print "data:" & data- H+ P9 B# N! y" z+ e% R& u3 p
PrevData(r) = data ' save for use by empty cells3 k' `. F+ K' V; q7 @
If data = Values(r) Or (data > Values(r) And LE(r) > 0) Or data = "*" Then
. [4 G3 c! Y4 z3 a$ `5 {$ c If r = ColHead.Rows.Count Then ' All rows match - It's a go/ l4 s2 u' H' v: c: w
Match = True4 S5 f9 [6 c/ s5 r/ X( |% r
End If
* I9 | p" z1 A/ ]) R8 F% i' P! v0 E Else ' This row doesn't match - go to the next column1 w. R" |8 `9 g6 v
Match = False' {3 p! h$ ~5 w" i% p( [
Exit For
0 c- D7 P4 G4 Y+ \! ?; t) C' I* N End If, M1 D# g) _7 A6 ~- Z
Next r
6 z( f0 q8 r. F3 o: I) V6 w0 Z. q If Match = True Then ' Don't search any more columns
; s4 }8 p7 H* J# x8 i cindex = c/ w$ H1 J9 t. {6 C- A) Z
Exit For' ]$ z8 f7 N& |3 d0 }
End If3 J5 L8 M9 H/ z
Next c
/ M1 a' V" G2 E$ O& f( L, H" a( Y3 r0 Z, r- S# X3 |
If Match = False Then ' Didn't find a matching set of values( o- r, o5 A8 b) }2 H* F3 |
ZZL = "No match for columns"
% J: a2 t8 u) M6 z M Exit Function
& I# x) a$ j% s9 I7 X; l9 } End If& u- X' P% f$ r9 O3 |
9 y1 T, j6 o" v cindex = cindex + ColHead.Column - 19 Q3 c$ R1 }3 L( F% O7 E' T
End If
; C' v6 i) i* H5 E0 B# `- l8 T+ R) n/ c
' Return the cell value from Table
# p3 R3 a$ e/ k; {- _6 ^) K7 W9 v'debug.Print "Answer is in (R,C): " & rindex, cindex
; X% q. o' ?& _ZZL = ActiveSheet.Cells(rindex, cindex)0 v8 v0 F9 \7 B4 s4 `
'debug.Print "Answer is : " & ZZL
( n$ i; D" Z8 A$ [7 S" M6 zExit Function+ R3 I% B2 f. s% v$ a3 N
# [4 o8 R; a' K- K' p) ?* x; T+ Zerr_handler1:
/ v( p! z2 _* ~+ j' l9 F( X& R8 oZZL = "Error on range '" & rngname & "'"0 ?+ \0 t3 b! S
0 s/ C6 l; m" P& G, }
End Function
+ p3 @" v& ^5 [0 G3 w5 w2 R$ d1 [7 ^
6 [- p0 T1 i. G3 K0 J4 @ |