找回密码
 注册会员

QQ登录

只需一步,快速开始

搜索
查看: 1742|回复: 3

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

[复制链接]
发表于 2022-6-6 18:03:11 | 显示全部楼层 |阅读模式
Option Compare Text$ o3 W' y8 \' Q! S5 q; b

9 c; [5 z$ s' w9 \- P, rPublic Function GSXS(Ref)
' f* c5 w" S1 r+ ?
7 ?, ?" ?, N( l7 W    GSXS = Ref.Formula6 I; P: }3 f, c) B0 x. x, r7 g8 @

( o1 f( @8 j5 L7 m. {) n7 OEnd Function
3 O/ J# Q$ S( h' f1 X: `$ y  b! Z% e/ `" V' l0 g7 N" s4 W
Public Function ZZL(RowHead, ColHead, Dummy)  V3 l, C7 B0 i' s* d

! u6 V5 r7 B9 F' q* n7 gDim Values(20) As Variant
# N' |) i* ^  A, l2 g/ WDim PrevData(20) As Variant
( \7 P9 N8 j/ T. }1 y0 Y$ r% RDim LE(20) As Integer
  Y- v) L5 `; J: v/ J6 |3 b+ Q3 a0 X+ F# Q% ]
On Error GoTo err_handler1# O. U* T( l2 g7 q$ V$ ]6 k- j' p$ x
' Do the vertical selection from rows
3 j" G+ O+ t7 e0 r' }- |4 b' q( mIf RowHead.Rows.Count = 1 Then
1 ~' n& `2 ^# @; `' C    rindex = RowHead.Row    ' first argument is any cell on the row of possible values
( [  n6 O& H& b7 JElse* v( V3 ~- A0 [: H4 a+ C$ O+ W
    ' Store the values to be compared with each column
! f% J8 e/ h2 I+ t; e3 T8 `3 J    For ii = 1 To RowHead.Columns.Count6 l5 ?- Q& w6 ?8 `+ v. }
        rngname = RowHead.Cells(1, ii)6 l3 ^$ w1 L8 Z+ B# E
        LE(ii) = InStr(rngname, "<="): [4 I" v6 |: \" u
        If LE(ii) > 0 Then" v- N. A9 Y: s  [1 F6 J9 |
            rngname = Mid(rngname, 1, LE(ii) - 1)
0 i, b0 d; M) ^1 o        End If! G8 [$ M& o" P& t1 I9 e
        Values(ii) = Range(rngname)
9 o9 q) f9 W7 H        'debug.Print "Variable:" & rngname & " is:" & Values(ii)  D; S' F0 n5 q1 R- u
        PrevData(ii) = ""   ' initialise% y( Z2 G, \" C: A( L6 o
    Next ii
9 N% y- ?. K! h/ ?) _" K" K2 F1 l" G' I; ~: C3 `
    rindex = 2# m( i5 @/ ~; N/ F# D
    'debug.Print RowHead.Columns.Count
1 a+ }2 k* y" B) }    Match = False1 f! F/ j! f! S% V
    For r = rindex To RowHead.Rows.Count6 F; W' K$ J( \* D
        For c = 1 To RowHead.Columns.Count   ' for each dimension
& G& m/ B0 |' B. `( Y2 s0 Q            data = RowHead.Cells(r, c)1 y  L' K; @* Z8 ]- E
            If data = "" Then
. a/ J$ }& |( O# |( w                'debug.Print "Empty cell found: using " & PrevData(c)
4 _2 m! Z/ d6 J, y; P/ S                ' use the last valid cell in this column5 y. D% E( j3 _! \8 P
                ' (this is to handle merged cells)* \1 Z$ ^8 Y2 p( ~' m
                data = PrevData(c)% D, I6 U2 p" h$ [% b9 L
            End If2 c  k. G. ^. B' Y1 X+ d3 b, t7 q. i
            'debug.Print "data:" & data
; ?2 Y8 e4 G7 \$ R  r5 o) F            PrevData(c) = data ' save for use by empty cells, ]+ G( b& J. q
            If data = Values(c) Or (data > Values(c) And LE(c) > 0) Or data = "*" Then
8 N$ L7 j- E. a                If c = RowHead.Columns.Count Then   ' All columns match - It's a go
3 m/ c) ]3 }/ h* T" d                    Match = True
4 {, V+ D. k- `. @( \+ [8 I                End If
/ g* C3 l* m6 C3 n0 `6 }7 H# D            Else    ' This column doesn't match - go to the next row
" L1 C- P" S4 K0 S0 k) b3 j% j' T                Match = False( z- m8 C7 F3 m8 M
                Exit For
8 l  t, I% g6 q. s4 P; @& W            End If/ W" @0 Q7 q; F: d7 h* ?) O& a' b
        Next c
: {+ {! w$ t1 B0 P; x) y! z8 N2 ]        If Match = True Then    ' Don't search any more rows5 z$ h0 q' m1 r" r4 ?) d
            rindex = r0 {' v/ l- D0 Y: i
            Exit For9 O+ m6 B4 n" f5 \9 Y0 {; }
        End If+ V8 p7 D2 t' z/ ]  e- [( m  V# z
    Next r/ S* B4 I' D' n" n; M* J$ p  Q

# s9 U, k: Q; R1 i    If Match = False Then   ' Didn't find a matching set of values8 U4 d: K  M0 p
        ZZL = "No match for rows"9 b7 |8 E: a) X  S' P6 }$ s7 y
        Exit Function
( T; l# o1 G7 H3 E5 }* |4 B* S1 j    End If
- g. S8 {. n# U! [4 t6 @! E1 T( J! `+ \- `4 u
    rindex = rindex + RowHead.Row - 1   ' make absolute index/ T( q/ }$ ?4 b; G( D) ]
End If
, x2 ]! P! x8 ^/ }; x! O# M" F8 b0 n+ c( M" B: Q# p% j
' Do the horizontal selection from columns
4 P1 Y- n5 a" [" c8 w1 IIf ColHead.Columns.Count = 1 Then1 b1 K4 i! Z% ?* I" M- }- u: A
    cindex = ColHead.Column$ p; U0 P# f1 J, z# [6 V3 {
Else
. m6 ?+ z; m. z9 F( a  F    ' Store the values to be compared with each row of the header$ B7 A8 K  r2 ~- n
    For ii = 1 To ColHead.Rows.Count
. g2 p) g* Y. a0 I/ ?5 q7 ^        rngname = ColHead.Cells(ii, 1)
! i5 K$ @, D* N/ B! ~3 R5 U6 W        LE(ii) = InStr(rngname, "<=")' J3 k( [5 h( W) Y
        If LE(ii) > 0 Then
# T4 g% I& U8 d, }; y            rngname = Mid(rngname, 1, LE(ii) - 1): {# B( M* y  ?
        End If+ ~# k. Q# A) p, K
        Values(ii) = Range(rngname)2 ]. p# X6 Z, Y: x5 t
        'debug.Print "Variable:" & rngname & " is:" & Values(ii)  s! m7 N: L2 A* y& Z
        PrevData(ii) = ""   ' initialise/ X5 D# V" q7 Q. S+ ?+ F) N
    Next ii
7 }( s9 V/ X1 D4 [* B3 y& y1 i$ O& d8 i
    cindex = 2: ]; ?, b( @7 b3 O
    'debug.Print ColHead.Columns.Count
. b$ }0 X# `! m" z    Match = False
8 P+ g0 Y# l+ E" P    For c = cindex To ColHead.Columns.Count
! p. f$ r* _# M/ J$ x        For r = 1 To ColHead.Rows.Count   ' for each dimension8 S  Q3 r$ {  p4 W* \0 U# K+ J& H& \9 \, A
            data = ColHead.Cells(r, c)5 M! Z& _+ B; |: D, `! Z$ o6 Y/ A
            If data = "" Then
! e3 I+ }1 q/ t! l  _3 R+ m                'debug.Print "Empty cell found: using " & PrevData(r)
# w6 W" ]! C$ G; O6 \7 x( E) F7 ]                ' use the last valid cell on this row7 C3 K- a& {1 o
                ' (this is to handle merged cells)
  q# C' g4 R0 V# S( j2 g, L                data = PrevData(r)% ~6 d$ o# X8 N; c
            End If5 p1 E/ v3 M4 z3 G
            'debug.Print "data:" & data
7 ]6 }1 X  c* j            PrevData(r) = data ' save for use by empty cells$ J1 X, G* ], Z' m; j' i
            If data = Values(r) Or (data > Values(r) And LE(r) > 0) Or data = "*" Then. Y+ `, L3 {0 O6 v6 S% Z" d5 F
                If r = ColHead.Rows.Count Then   ' All rows match - It's a go
% M; _+ l0 h. p0 c4 M% T                    Match = True
& `& s4 \# E* `  B, q! t                End If
) `  h& \4 ?5 c: d            Else    ' This row doesn't match - go to the next column: \0 K$ [: e  c& F
                Match = False. F& r) P) S: W7 C6 x& D5 m
                Exit For
3 ^' }1 v  |2 S9 m            End If/ ^6 L4 U$ u' q; C
        Next r
+ N0 F4 q* O3 z: f2 V6 {        If Match = True Then    ' Don't search any more columns8 M7 M& U1 h  _) `5 W' ]% ?
            cindex = c8 e5 D' z/ P6 }+ ]
            Exit For; K( h/ q! h5 B: V
        End If
1 r2 X# e; j% }& H, H& n1 C    Next c
! o& @7 t* W; Y0 e$ T: L3 f$ Y5 d! b. V8 l: z4 }* z3 b
    If Match = False Then   ' Didn't find a matching set of values! c* x/ [% P( ?. l- i. h  J
        ZZL = "No match for columns"0 X& n' X* T1 \4 P+ f' |1 ]" W
        Exit Function' g3 a- K" ?5 o# r2 S( v/ i6 K4 p" j
    End If
7 P, B$ P/ t) U. N
8 |( o6 O, w- S  y/ Z    cindex = cindex + ColHead.Column - 1
( F0 ?* ?! F7 B( F0 o1 `End If' b, c. G! D( l
6 P6 L' o3 a* `# `
' Return the cell value from Table; R  o# W6 D; X) T7 K2 g
'debug.Print "Answer is in (R,C):  " & rindex, cindex1 l7 K0 u8 K- M3 X
ZZL = ActiveSheet.Cells(rindex, cindex)
- K, i* L" j- }5 X'debug.Print "Answer is : " & ZZL, |( e$ [  g0 d9 k
Exit Function# o0 D% r# A. ^

' K9 W. f; }7 v& E# Terr_handler1:
+ C7 @$ d( ]8 v( G8 H+ hZZL = "Error on range '" & rngname & "'"
0 k( C% t7 I* \8 k7 M* a
3 v0 X- p' W& ^" z3 k' Q" g1 R& ]End Function
. s4 e4 U3 a5 R( H2 b4 l/ c+ M: o7 u
回复

使用道具 举报

 楼主| 发表于 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-10-23 10:39 , Processed in 0.054891 second(s), 14 queries , Gzip On.

Powered by Discuz! X3.5 Licensed

© 2001-2025 Discuz! Team.

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