Option Compare Text6 g6 j, z# Q. I1 Z7 m5 @
) d! n4 X1 N' R; m- { k8 r: B
Public Function GSXS(Ref)
" T+ p/ F0 m' J* |( ] M& c5 q7 {* O$ l9 ?# p) @
GSXS = Ref.Formula
( L$ Y- i8 ?; j/ Y0 q- D; S/ [
7 |/ d, _6 O; p# t8 i/ tEnd Function( m$ o+ y/ X( a, h4 p w
' U/ q4 J3 V* J9 _0 O- F- CPublic Function ZZL(RowHead, ColHead, Dummy)
, b# `% m+ ?* F; [! E8 n7 S' @1 }; N8 u# w
Dim Values(20) As Variant
& {+ ^0 S ]8 {+ r% ]7 h4 h3 ADim PrevData(20) As Variant
$ h) K) q9 q; y* c' y, wDim LE(20) As Integer
" n8 z9 @; f5 ]; G$ Z, W; O5 g, C: O% z1 P
On Error GoTo err_handler1
% y) w& R7 h" q* D5 C, _' Do the vertical selection from rows
v* n8 l! o: v( s2 j8 h. mIf RowHead.Rows.Count = 1 Then
9 Z1 Z+ [9 j% O0 V rindex = RowHead.Row ' first argument is any cell on the row of possible values0 E$ e/ s& C$ i* S& n# l
Else* S9 k7 l5 l+ H
' Store the values to be compared with each column# R3 l8 }4 H+ g+ ^5 d, s
For ii = 1 To RowHead.Columns.Count7 s) M/ c( p! `5 g3 `
rngname = RowHead.Cells(1, ii)3 {" |4 a z! F( W. t
LE(ii) = InStr(rngname, "<=")
' i2 q3 F1 r* n! Q/ Y/ u3 `, ]) W If LE(ii) > 0 Then
4 K" K# @1 }2 ~% [: ] rngname = Mid(rngname, 1, LE(ii) - 1)8 W: J4 P. A5 W+ P; h
End If
1 E3 c$ d1 |% z+ z/ K6 U6 U5 a Values(ii) = Range(rngname)
, a' `) T3 m2 h3 l" A0 G$ A 'debug.Print "Variable:" & rngname & " is:" & Values(ii)
% s: v" Y" p6 e. S: @" Y t PrevData(ii) = "" ' initialise' Q' b$ N( J& L5 ]/ n7 a% h8 I8 C; W. d
Next ii5 }5 B; e5 _" r# O; D5 P; z8 M
* v( \0 [- P7 x4 A. q
rindex = 2
; k5 F% A6 g* f3 e7 W 'debug.Print RowHead.Columns.Count0 S# J1 ]. o/ x" h" l5 D/ i5 k. F
Match = False
9 R! d# C5 S1 `5 E# ^ For r = rindex To RowHead.Rows.Count
, X1 \$ C5 U P For c = 1 To RowHead.Columns.Count ' for each dimension
8 S/ S' [' n% K1 F, }7 Q data = RowHead.Cells(r, c)
{+ f5 U% x, l" g1 l If data = "" Then
% q1 w, T6 K3 s, U! j 'debug.Print "Empty cell found: using " & PrevData(c)
8 Y, N- V, r1 _3 q( N6 V |0 k ' use the last valid cell in this column
. R! k2 x7 ?, f2 b+ G ' (this is to handle merged cells)
8 S6 a( c) \" T0 Y& b+ Z3 p data = PrevData(c)7 K6 x7 f) M& ^! {
End If3 ]0 ?# M( [1 P0 P* b0 ?) U
'debug.Print "data:" & data7 w& \6 r$ b$ P. P8 O
PrevData(c) = data ' save for use by empty cells! N8 }6 N- _: @! C9 Q [
If data = Values(c) Or (data > Values(c) And LE(c) > 0) Or data = "*" Then' \9 z W6 O# `" S+ Z" J
If c = RowHead.Columns.Count Then ' All columns match - It's a go5 x3 a/ h. P% E9 M6 [4 A5 {8 A
Match = True- M' }9 ?' A) G1 m7 x4 U2 y+ O
End If" u U' y& i' G* Z X% ^. z
Else ' This column doesn't match - go to the next row
& Q% ~- \8 b* S Match = False
% w5 F$ `3 K: ~% v Exit For( [5 S7 ^/ h$ [1 p0 D) ~. o3 l
End If) e7 ?0 S& Y5 l. D! N
Next c% T% ^+ \. q- u8 S+ h1 w
If Match = True Then ' Don't search any more rows( d9 W$ ^9 r; p- I$ i$ f2 y( R
rindex = r, _3 K+ l- G* _* {: ]
Exit For
8 [5 r2 t# p" k1 Z7 B- y" N" q End If+ S1 i' x! S& G4 m0 L( e4 ~" [
Next r
+ B( `9 U1 h8 C v/ Q. @- Y$ ~+ d
3 [6 }; d3 K4 R6 F9 \5 ]: C& K) O If Match = False Then ' Didn't find a matching set of values) D$ A, ?' m0 I1 C( o
ZZL = "No match for rows"5 ?9 H6 v1 s6 f) C
Exit Function
& Q0 _, g) S- O4 \6 v End If" C1 G N' F+ l& f7 r" W* y5 X
2 S1 M1 w( V, O
rindex = rindex + RowHead.Row - 1 ' make absolute index
3 s* e4 J; B. ?, q. UEnd If
$ b% ]' p: [" y, f
1 E4 F% ^: Q. r! _% v' Do the horizontal selection from columns
$ T! x+ W3 [$ m7 \+ b. ?8 jIf ColHead.Columns.Count = 1 Then
: d2 J- [6 i _ cindex = ColHead.Column
& p6 S2 b+ |3 A8 rElse
, \# x% `$ Z0 x# x: Q ' Store the values to be compared with each row of the header$ t. b" f: O* k+ H2 ^+ I
For ii = 1 To ColHead.Rows.Count" O* C% v2 X9 t! H7 `
rngname = ColHead.Cells(ii, 1)
/ l; G1 g) u3 Q" A' t LE(ii) = InStr(rngname, "<=")/ h8 x9 y9 m$ ^# Z* s: z
If LE(ii) > 0 Then
4 q' N Q" b) _: s. ]$ _8 D+ o! E rngname = Mid(rngname, 1, LE(ii) - 1) O& ?$ C; _# _3 e8 S
End If
! k' r. }1 ?9 O8 B, { Values(ii) = Range(rngname)
9 I0 t( f4 t, } 'debug.Print "Variable:" & rngname & " is:" & Values(ii)
3 h J# [' j/ i4 g$ g J PrevData(ii) = "" ' initialise
! N4 i [( l" ~3 ? Next ii
& c1 ], @8 H7 R- F( ~/ m @& M+ W( j
" I' B; ?0 }: h cindex = 2+ ~: q1 ]& g: L. k# R3 ]7 |* e
'debug.Print ColHead.Columns.Count0 N: j+ z& M: B
Match = False% q/ o- Z8 a- T- \6 S% @
For c = cindex To ColHead.Columns.Count
! F6 L) Z ]; G5 x: L" Z For r = 1 To ColHead.Rows.Count ' for each dimension
' j0 |! h1 B! e8 X data = ColHead.Cells(r, c)
! ^) f; S9 F8 d8 l If data = "" Then
) f `' Q# x6 { i+ m; T 'debug.Print "Empty cell found: using " & PrevData(r)
' \3 _% ~; u; a) C" _/ d! E9 Q ' use the last valid cell on this row5 g1 Y+ s' K! @# A0 ^
' (this is to handle merged cells)1 p6 y4 j( J& U$ E# g, b
data = PrevData(r)
. r! O, T6 \+ e' _) H End If
?1 g' z. l0 ~" c: @- H: E 'debug.Print "data:" & data
# G1 m4 V# d) V* X/ A' F PrevData(r) = data ' save for use by empty cells0 n+ M i) h( f& x2 o
If data = Values(r) Or (data > Values(r) And LE(r) > 0) Or data = "*" Then. r" V, I' h+ [$ K# b. w, @
If r = ColHead.Rows.Count Then ' All rows match - It's a go7 s8 W2 E: I7 s1 Y+ u. b
Match = True
4 D6 f4 f n% g& G: i- x End If
2 Z- W4 ?! E& s3 }- N ] Else ' This row doesn't match - go to the next column% z9 m9 a( j: @: m. X0 W% Z
Match = False
5 [6 H8 s9 T; a7 k# _ Exit For5 h: I9 A8 G: R, q, S3 @
End If p/ v T- a1 G: z/ l6 [
Next r
F8 ?! C$ P2 G' Q; T3 m If Match = True Then ' Don't search any more columns
" c t0 H( _$ b1 [# U7 G cindex = c. u; P: N3 J2 d, `: K
Exit For
/ A% a1 s( y" S End If: p2 R% k4 t3 B! r: q8 c/ f
Next c
- X8 Z2 F; i+ g* w; H7 K7 H3 `
0 @' u( o3 {* a. ^! g If Match = False Then ' Didn't find a matching set of values1 I# S9 i* ]% b0 x& g
ZZL = "No match for columns"
( E8 m) z% o9 T; o Exit Function
5 L# ^* M3 ~$ h0 q End If
6 t( D* ]. F& S, Q N, r. `6 @- b; l) k$ b) r
cindex = cindex + ColHead.Column - 14 U( l; s5 R$ N. n
End If/ ~! y) M: L9 `9 g
5 a& R; F o6 P, n' Return the cell value from Table. P' j% p# m/ ]- m, G9 s$ e
'debug.Print "Answer is in (R,C): " & rindex, cindex2 z6 ~+ Y0 f& Q3 f' I
ZZL = ActiveSheet.Cells(rindex, cindex)
: z3 t3 f0 A+ K: n4 X'debug.Print "Answer is : " & ZZL7 G9 a6 E! C& y; y. h% t
Exit Function7 T, j$ Q' v. K0 B5 i I
' M8 J. C R: \. a
err_handler1:
" b0 f8 z e( a4 b0 l wZZL = "Error on range '" & rngname & "'"
2 o: A3 C3 ]* V: h5 m. Z w& g
( k" j# ?5 g7 M" V$ A' [, s8 U' b* mEnd Function
; l% c1 f4 Z5 [, F
0 a, G8 B$ c7 ?3 j3 }; Y2 }' } |