找回密码
 注册会员

QQ登录

只需一步,快速开始

搜索
查看: 1686|回复: 3

excel 中BVA 麻烦大佬帮忙解释一下

[复制链接]
发表于 2022-6-6 18:03:11 | 显示全部楼层 |阅读模式
Option Compare Text; |2 t6 V1 Q6 _6 y

' v, {9 B# v6 L! I6 h# v! q  N% BPublic Function GSXS(Ref)$ e  x1 i" k* s. y$ K! t1 t; t

" }# ?: W/ H1 z) Q7 ^    GSXS = Ref.Formula2 a2 a/ x2 ~3 g9 z' i. h: s

: y3 L& Y) }% A2 BEnd Function
( ]+ M5 S. U2 {: d4 _/ n$ S! @
Public Function ZZL(RowHead, ColHead, Dummy)
. D9 H5 F7 s) }; j! }. M, V4 }' ?4 U0 T5 E* p* u* D
Dim Values(20) As Variant
7 W$ P' i" `- `/ y, c5 DDim PrevData(20) As Variant. S: X* h% Q; W5 ?
Dim LE(20) As Integer
5 [5 B3 ~  k/ r) z" A0 C  H, m9 u+ y: a# L5 h1 v- \
On Error GoTo err_handler1! \) b: `6 p6 Z" N! m& ~! P6 Z. E
' Do the vertical selection from rows" I$ ?5 F, o4 \8 |# j, V, q
If RowHead.Rows.Count = 1 Then  i0 _5 }* c. j# ?4 z7 o) y
    rindex = RowHead.Row    ' first argument is any cell on the row of possible values
# h, [, b  _+ C. W/ F2 @6 sElse7 p) p8 l) C8 ~3 `# p  H
    ' Store the values to be compared with each column
  d4 e: ~- K) M    For ii = 1 To RowHead.Columns.Count
9 |1 z( m& W! T- L9 j$ e( `6 f* ]        rngname = RowHead.Cells(1, ii)
) C( x2 S  D- ^( m8 j2 h        LE(ii) = InStr(rngname, "<=")
& U( T9 l: `+ q  q3 `1 `3 H        If LE(ii) > 0 Then  Z! |4 S; g: X5 c: `2 V- G
            rngname = Mid(rngname, 1, LE(ii) - 1)( ]: j# z4 z8 t- [4 j  o
        End If# j4 M" Q( |1 u. P
        Values(ii) = Range(rngname)
3 i) c! @, Q0 C% A9 m        'debug.Print "Variable:" & rngname & " is:" & Values(ii); {6 v2 w( {3 k1 ?7 F9 ?
        PrevData(ii) = ""   ' initialise
/ m( a2 Y5 t$ I. |    Next ii3 S; y+ B0 k. G( d
5 A  b4 a7 L5 a: U* K6 O  J
    rindex = 2+ n9 A+ T" B: n, d
    'debug.Print RowHead.Columns.Count! t0 ^) g" ^8 K
    Match = False- Y3 K! a( F+ m) O. B. i
    For r = rindex To RowHead.Rows.Count9 r- h2 `) k9 {1 y6 N5 j
        For c = 1 To RowHead.Columns.Count   ' for each dimension
7 S, p+ w. M+ r& x, I2 r, |            data = RowHead.Cells(r, c)
+ J7 T( Q; F  ~3 v& S; `            If data = "" Then
$ T4 o6 n# X! E5 L                'debug.Print "Empty cell found: using " & PrevData(c)) C, I3 M- Z+ o9 y
                ' use the last valid cell in this column! ~  A( M; K- @4 ~9 E, ]
                ' (this is to handle merged cells)
* S8 v: L. h" F7 _, C/ g. N5 _                data = PrevData(c)5 c1 Q# g6 |) V; t. P9 ^
            End If* w$ |8 y  D- J) E, p5 K) s; ?
            'debug.Print "data:" & data( R# o2 ~, P3 O: Q: o" I
            PrevData(c) = data ' save for use by empty cells9 @* X- z3 o( K) p& E' g
            If data = Values(c) Or (data > Values(c) And LE(c) > 0) Or data = "*" Then  ~4 o. s- b& D9 ?( ]; d
                If c = RowHead.Columns.Count Then   ' All columns match - It's a go
! z8 D# k5 v1 i, G: q# e                    Match = True
) e6 k: N; e" H& v                End If6 y9 ]4 q, ^. ^: {% [& m" t
            Else    ' This column doesn't match - go to the next row# w  E6 Y  n2 p2 I
                Match = False
% m! P0 A, `9 x/ T5 ^                Exit For
- J9 V4 |* u% r* O  X8 y1 w            End If
- O, s2 P) M( Y! ]( P3 u1 @+ `+ t        Next c
$ s6 L2 V6 U1 \; A* |) _9 p        If Match = True Then    ' Don't search any more rows
) u1 \% A9 P7 i, |/ b            rindex = r6 F! B. I9 X4 }3 k; g
            Exit For
$ T9 M' B. ?+ W2 [0 Y  ^        End If
3 [  n  J: O' {0 @    Next r# Z3 s0 p0 J3 M! f% Z1 Y

1 M$ y' _& ?1 o7 O' l+ Y+ h    If Match = False Then   ' Didn't find a matching set of values; X8 I5 X. O- N( m
        ZZL = "No match for rows"
$ K  r6 N. ]/ E( d8 \& _( P4 w        Exit Function5 d* b! C. P" c, ]/ n( r
    End If. J- X% {3 ~! P# |* K7 U$ i
. I- ?) S: E0 C. ]% t/ a
    rindex = rindex + RowHead.Row - 1   ' make absolute index
  O, c( Q0 x9 JEnd If3 m* r0 Z0 O- @" A1 R
6 \+ S, x" D* Z2 q( t6 h) {2 \
' Do the horizontal selection from columns
$ a1 B9 H7 S& x! L$ B/ ]# k4 a5 lIf ColHead.Columns.Count = 1 Then$ W) h* t) K0 B1 ^
    cindex = ColHead.Column
$ r; k, s. E, U: z# X# WElse
+ x3 I4 S8 D5 F" e7 f: V    ' Store the values to be compared with each row of the header' l( Z+ R* _, I; J7 b. `4 `$ {
    For ii = 1 To ColHead.Rows.Count# l. \6 Q1 D" Y( Z: z+ J0 U
        rngname = ColHead.Cells(ii, 1): [5 S& J5 i8 p% P/ o- k
        LE(ii) = InStr(rngname, "<=")& U/ y: W1 @* P0 K
        If LE(ii) > 0 Then
6 ?5 ?) K0 X% I2 O* u3 R3 f            rngname = Mid(rngname, 1, LE(ii) - 1)
  p$ ^0 u+ f1 @9 n        End If
( d! D$ K* `/ _. [& Z' }8 L" ]        Values(ii) = Range(rngname)6 \. C- A' S: [- s
        'debug.Print "Variable:" & rngname & " is:" & Values(ii)
) L' _- }/ T  t        PrevData(ii) = ""   ' initialise! b+ |% c  e' p9 p( |3 A! `
    Next ii1 q1 ^: c* C" C8 O8 x9 j" f* m# `

* r; r( n4 X  X0 c* ^' F) l    cindex = 2
/ o# }8 G1 ]( G! V! I. ?$ k" o+ F( Q    'debug.Print ColHead.Columns.Count% ~! P" g' ~: w. y' [# ~
    Match = False
) a, F/ Y+ ]" C1 Z! A    For c = cindex To ColHead.Columns.Count0 p2 U3 b3 B9 a! W+ g- |
        For r = 1 To ColHead.Rows.Count   ' for each dimension/ Y% `4 @1 m, ~% P8 m
            data = ColHead.Cells(r, c)
0 q: F  y! l1 A1 M            If data = "" Then( [- C- q) g# v4 a# R: V- e% r
                'debug.Print "Empty cell found: using " & PrevData(r)5 u& }* N( s* f
                ' use the last valid cell on this row
( t# q5 F0 [; b- W. Q: W, _                ' (this is to handle merged cells)4 _0 `1 W- M0 x% |
                data = PrevData(r)
+ l/ f; c/ Y2 @& K            End If
, P1 G  n0 L: N9 `* A2 ]            'debug.Print "data:" & data! x. O' r  e; `/ t
            PrevData(r) = data ' save for use by empty cells
/ v! Z0 L1 L: @5 B1 f            If data = Values(r) Or (data > Values(r) And LE(r) > 0) Or data = "*" Then
( S- A( l9 i6 F" b- M7 D                If r = ColHead.Rows.Count Then   ' All rows match - It's a go# ]5 w' P5 r5 P; e& Z
                    Match = True
; W3 u* o! p0 I4 K" m9 ]                End If
2 z) r; b8 k/ d2 A: s            Else    ' This row doesn't match - go to the next column
8 n0 l& ]/ j" C8 Y8 U                Match = False* X: p: l/ p* y; G5 ]6 ?
                Exit For6 N( f2 P2 S- u
            End If
4 |! y0 K2 I4 y0 H. S/ f/ R        Next r
# }& l1 _/ }/ V& j        If Match = True Then    ' Don't search any more columns0 }( k1 f5 g1 A2 i( z
            cindex = c* U0 W$ t! Q! A* Y3 j
            Exit For
) @% ]! d6 ?! \& Z6 X/ @        End If0 D& w( f4 _9 o. ~+ r% o
    Next c  \. [3 s, H; k# C- ?# K$ W3 o( `

) Q- S$ l/ P. k- L) K' P. L    If Match = False Then   ' Didn't find a matching set of values1 X. h3 b  ]& s- k' n" ?& O
        ZZL = "No match for columns", v8 N% A4 c- y3 E7 K* C1 }
        Exit Function
/ e& S6 ~" @% U2 ?    End If
5 ^# W0 i5 q' _, x* N
" L- `% G% ~; X' N6 b6 o4 M    cindex = cindex + ColHead.Column - 10 M% o) G* [8 O. {/ J7 k
End If
' G6 g( L: }: o1 H/ M5 O1 _6 o, E. K" H9 ^. c: O# X
' Return the cell value from Table
: u6 n/ y1 f3 i( P'debug.Print "Answer is in (R,C):  " & rindex, cindex: `4 c* h! W3 G7 ?( X& N1 \1 N
ZZL = ActiveSheet.Cells(rindex, cindex)) S9 f7 G& P) l# S2 y# q5 g0 U
'debug.Print "Answer is : " & ZZL, t# d, e8 {- C
Exit Function
, q, k- x; x  A" r
+ S" f' a; j- F5 C' h* Derr_handler1:
- A, B2 k) V6 L) x: H* Q* BZZL = "Error on range '" & rngname & "'"
0 m  B$ j5 c- k5 C4 {' c! z9 A
& Y$ _4 J" F. I4 \" V' c8 ZEnd Function6 M- w4 W( O, C) q( A9 _: @' V

/ ?2 c8 v% t4 B6 a/ X
回复

使用道具 举报

 楼主| 发表于 2022-6-6 18:04:26 | 显示全部楼层
本人是小白,想请教大佬,如何能看懂以上信息
发表于 2022-6-6 19:17:09 | 显示全部楼层
微软官方的bbs里是有专业的VBA教程和API端口说明能检索的(全英文)
发表于 2022-6-7 08:52:01 | 显示全部楼层
上excel论坛问问看
您需要登录后才可以回帖 登录 | 注册会员

本版积分规则

Archiver|手机版|小黑屋|机械社区 ( 京ICP备10217105号-1,京ICP证050210号,浙公网安备33038202004372号 )

GMT+8, 2025-9-13 15:10 , Processed in 0.238167 second(s), 14 queries , Gzip On.

Powered by Discuz! X3.5 Licensed

© 2001-2025 Discuz! Team.

快速回复 返回顶部 返回列表