Option Compare Text
; F* s4 ~. A1 V
9 t5 T9 k9 |& aPublic Function GSXS(Ref)
4 v0 Y0 s' [% a3 Z h. S4 ?6 F: C
, P+ j# t$ ^9 [; Q+ n1 P4 G. i GSXS = Ref.Formula# I! h% u* t: }7 e# P. @
% Y2 X4 A7 O( P+ F6 e LEnd Function
# _8 d0 F" q5 X, U! G
4 y6 i. R( q- b0 V9 y* { cPublic Function ZZL(RowHead, ColHead, Dummy)
: T9 _6 g; K4 R5 z, w! X1 p; W9 l% l, n# A$ L# B) N
Dim Values(20) As Variant8 N9 @4 q% s$ V1 U% l% ~
Dim PrevData(20) As Variant) r0 x5 z+ P8 @; f5 Q2 G6 [
Dim LE(20) As Integer- y' @2 O" G- v
u! U+ T, g1 }3 v3 k
On Error GoTo err_handler1
7 Z/ q [* C% o- c' Do the vertical selection from rows
: i! L9 h% s% vIf RowHead.Rows.Count = 1 Then
3 G: z4 d7 Y; V( r% `6 B rindex = RowHead.Row ' first argument is any cell on the row of possible values8 L+ u# l4 c9 G* }/ |3 B
Else
% |$ i6 Z8 @2 z+ i ' Store the values to be compared with each column0 V) t5 U; m2 S
For ii = 1 To RowHead.Columns.Count
. ]3 \0 C0 v6 a, ^5 K rngname = RowHead.Cells(1, ii)
- J& U' j/ U6 \+ f) [ LE(ii) = InStr(rngname, "<=")* r6 M9 V/ H/ {9 V" i" i `
If LE(ii) > 0 Then
D" h" d7 y: O# X5 U rngname = Mid(rngname, 1, LE(ii) - 1)
% a6 J6 \& ~. w4 q6 V% t9 W$ P End If# [, _$ p3 M F+ S$ H# e# P
Values(ii) = Range(rngname)8 P: H% k4 I& ^" u3 z' ^
'debug.Print "Variable:" & rngname & " is:" & Values(ii)
1 u6 D$ L6 d+ q5 `+ } PrevData(ii) = "" ' initialise' ?! f1 M0 H& y
Next ii
+ V9 k+ o8 t! O4 {0 k! r3 S, s" |
rindex = 27 J! _+ q0 q. X$ E4 s, _" }8 `
'debug.Print RowHead.Columns.Count
2 d' R: I1 e- G J Match = False
) w# o6 W! e' T% D, v, j7 V8 H8 ] For r = rindex To RowHead.Rows.Count
1 Z- }; Q2 H* C For c = 1 To RowHead.Columns.Count ' for each dimension
3 W" e1 n6 V+ c7 A( h8 l data = RowHead.Cells(r, c)0 W/ m+ M2 l9 m8 S: Y9 s. D
If data = "" Then
6 d V. c' C, G2 z6 l5 j4 N" H/ O, w& [ 'debug.Print "Empty cell found: using " & PrevData(c)
{6 C8 K' v" A% f2 W0 _9 {9 \0 C ' use the last valid cell in this column( c& y& Q2 K/ g2 A
' (this is to handle merged cells)
3 d! |+ Y" X6 \ data = PrevData(c)
$ a/ u( O: v( F: N) @ End If+ X4 H7 i* h$ `( ?: b, |
'debug.Print "data:" & data
8 X8 ?' c+ a- a* _ PrevData(c) = data ' save for use by empty cells
+ l; h" q+ ~8 g5 `2 Q7 U! q If data = Values(c) Or (data > Values(c) And LE(c) > 0) Or data = "*" Then
# y) |" O' D) y' W; R If c = RowHead.Columns.Count Then ' All columns match - It's a go7 O6 W: L7 u2 G, C, q% h/ t
Match = True$ a& W7 S0 Z* W! R
End If( T" I8 w% p& m- \( D1 ?* M
Else ' This column doesn't match - go to the next row
- \6 G3 F, Y5 A# H3 w Match = False
8 z2 j* n! J3 |6 M1 s. l Exit For
! R ]9 K0 ^/ u9 R) D End If6 b# `6 h2 k, w) T0 x6 p
Next c
4 \- R0 s K" J5 X7 `, a7 ` If Match = True Then ' Don't search any more rows- W8 i6 K, X0 V& R7 |
rindex = r+ \& d4 Z0 c; o1 ~: f: V& A
Exit For1 { x% ]" U9 ~, d& w3 j
End If; g! r% ^! L7 w& j2 |" L
Next r0 O$ |; F) y, {
) k0 U6 J; w' |* Q4 m If Match = False Then ' Didn't find a matching set of values
! z. C N4 }# L; H ZZL = "No match for rows"; r; }$ o; Q' b
Exit Function
! f3 l0 i2 Z, c5 Z9 s8 B( c End If1 v9 j+ U- g) }( O1 k
U1 i. S- ]$ w. j
rindex = rindex + RowHead.Row - 1 ' make absolute index* ^# M/ ?, z2 }, w7 Q" z
End If
7 H7 o0 H. K+ j9 W
3 c6 C- a4 @% ^' V' Do the horizontal selection from columns
: C( S1 U) A9 h# c( }( v) B AIf ColHead.Columns.Count = 1 Then/ u M& N+ b* m& s# d) J: y
cindex = ColHead.Column
' \6 c9 S1 M% e/ [$ W3 F. p) oElse
# S% j2 P4 m* z& l2 E2 D ' Store the values to be compared with each row of the header
l- H9 h' w0 t+ h For ii = 1 To ColHead.Rows.Count
( L2 P3 Z/ B+ M rngname = ColHead.Cells(ii, 1)
% V) M7 ~! ?( v# c$ ?' y4 [ LE(ii) = InStr(rngname, "<=")
" Z9 I6 R* b6 b* A7 j If LE(ii) > 0 Then. ~1 ` J5 j* o4 N7 T# ?& m
rngname = Mid(rngname, 1, LE(ii) - 1)6 W9 l( p9 ~) ~% o3 i- x+ x7 ?3 e
End If
% M* E z+ c$ I7 R Values(ii) = Range(rngname)
/ a9 M# h! d* G0 c4 I$ B6 u 'debug.Print "Variable:" & rngname & " is:" & Values(ii)7 X( v) b: v$ F; f$ S& Z
PrevData(ii) = "" ' initialise/ m+ t; Z7 G- L& S. F3 u7 z& o
Next ii
: _$ l/ [+ z! g+ w* [4 U
* E _' @* x3 w, n5 D cindex = 2
# Q% T6 f- B' g$ x* Y4 t- C$ q# y 'debug.Print ColHead.Columns.Count+ H' i3 ~& _+ Y# O* g! \, F
Match = False
5 e# p, y% t- q9 L3 J0 ?3 m For c = cindex To ColHead.Columns.Count
4 b& L3 D- V, q9 l. w- c6 `0 d) [ For r = 1 To ColHead.Rows.Count ' for each dimension
$ |4 l4 Z0 ~ e7 h& n7 ^4 ^ data = ColHead.Cells(r, c)
9 w1 H4 Y' t' t If data = "" Then
; w' S' ]# N/ r f/ k 'debug.Print "Empty cell found: using " & PrevData(r)" I/ X' }6 e& ~6 o) f
' use the last valid cell on this row
; U) j+ ^6 E2 w4 A& F2 F% z4 k ' (this is to handle merged cells)
) s! C# z e+ B% f ] data = PrevData(r)
, m8 f- Z' S/ X" I1 G5 |) h4 ?- r End If( i2 K. O! [! n8 |
'debug.Print "data:" & data
! }) \' b A+ O0 T$ G PrevData(r) = data ' save for use by empty cells
J. J, o% e S R2 O. ~2 n$ t3 z If data = Values(r) Or (data > Values(r) And LE(r) > 0) Or data = "*" Then0 h% ?" }1 P- a3 [9 m
If r = ColHead.Rows.Count Then ' All rows match - It's a go
" N u# T4 E9 i# y: {: B) i( c7 S Match = True# z0 N: d/ f- t7 K) Y+ d% K: w J2 _
End If
& s, j; w" d( r! I5 ]! Y6 u3 p1 w# ` Else ' This row doesn't match - go to the next column1 W" L$ l5 f( G z6 F, p0 Y
Match = False
( P' y, Y6 Y, @4 J f. F" F Exit For' ^4 H, P6 {- J; N/ D% I) K
End If2 A# m6 ~" o: L o+ o% i
Next r: G$ z' J" b* V f# L/ C
If Match = True Then ' Don't search any more columns
* D9 Y- Z4 h9 w8 Z cindex = c3 v) A/ D7 `$ ?* n6 ]; m" P/ v3 R4 G
Exit For0 `$ I4 V J4 c: V2 o/ y) d* U& B* \
End If0 ^$ s# @% Q, b6 X
Next c
5 A# [- g6 H* o9 y0 r+ P
2 ^! N: t, S6 V j5 k. }- G If Match = False Then ' Didn't find a matching set of values* G6 y$ s& B8 L3 K! e: P0 p$ ]
ZZL = "No match for columns"
3 |9 p1 E3 ~4 t1 x n( }, C$ @& q Exit Function! a( j$ `; @+ } p+ K# r8 j" y
End If" C7 L2 s' M, R, y1 ^7 A
y: H% U6 z1 X, _; v cindex = cindex + ColHead.Column - 1, S& \+ Q, H; l" s4 K6 x: ~0 D3 g1 [
End If
/ ]$ H0 q2 Q5 f+ d. k. Z, g- o
' O, R! D- g( G/ W- F6 z' Return the cell value from Table
. y4 O5 U8 W7 a' ]' m; o'debug.Print "Answer is in (R,C): " & rindex, cindex9 R+ C) V0 J- a5 u- I. o1 e
ZZL = ActiveSheet.Cells(rindex, cindex)
9 Y! W6 Y$ K6 d6 j1 Y4 i' ^'debug.Print "Answer is : " & ZZL6 h* Q: y7 U; i9 u% ~
Exit Function# H* U% S: Z/ o% [$ g" ?7 ?
" C! B9 l5 b5 K& B
err_handler1:
; t$ ~/ T5 ^% l8 n* s$ X% H! |ZZL = "Error on range '" & rngname & "'"" U1 p, u+ B; E, h' ~, m* p3 U
! E4 O6 F7 l0 B- L
End Function# e( g! a+ b9 ~8 L* z0 K: L5 {. r
' k, X" U: W' h) J: q9 j' l' }+ ]& f
|