找回密码
 注册会员

QQ登录

只需一步,快速开始

搜索
查看: 1685|回复: 3

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

[复制链接]
发表于 2022-6-6 18:03:11 | 显示全部楼层 |阅读模式
Option Compare Text
" ^0 k3 {' |) L' C; W$ I1 L8 l$ D, q+ {3 V
Public Function GSXS(Ref)
% c* y& Q& m( Z6 H* }* s/ \8 {! [7 W' d9 S
    GSXS = Ref.Formula& O  ~" A- h) {5 f( S

+ G7 u7 h, ]5 D- B+ |: a9 w( AEnd Function
8 x# y" S" L% J% o& a0 ^% b% g2 j& |1 g- F$ J" F
Public Function ZZL(RowHead, ColHead, Dummy)
2 t( }( N2 A1 S9 G4 o/ g' l: `  r5 ]% `$ O1 p! P" R. v
Dim Values(20) As Variant
% ]1 E8 h5 c0 jDim PrevData(20) As Variant
/ r+ d! k$ `& }5 o8 }9 i. }7 o+ ~Dim LE(20) As Integer+ k' k7 B6 K/ F: n' y
. Q* K3 B0 n) W
On Error GoTo err_handler12 x# ?. o0 u& z( s6 [& w  Y5 [% |
' Do the vertical selection from rows
) F6 Y/ W0 H+ S3 ]- P  K/ |: J0 I4 z9 vIf RowHead.Rows.Count = 1 Then( Z1 k( Q) g2 M, w. ?; I# H/ i
    rindex = RowHead.Row    ' first argument is any cell on the row of possible values0 K& q& b6 H9 a( A
Else+ N" d, I" r7 [& Y$ p$ |5 o
    ' Store the values to be compared with each column
1 E) p. O5 v3 b4 g, e    For ii = 1 To RowHead.Columns.Count& K- I6 G" B% Q( D$ Q# g
        rngname = RowHead.Cells(1, ii)5 d& P/ W9 S+ I- j* D: g/ q5 C: i$ V
        LE(ii) = InStr(rngname, "<=")# N; i! E  w/ i  l
        If LE(ii) > 0 Then
- @5 c- f# {2 u$ b            rngname = Mid(rngname, 1, LE(ii) - 1)# O6 H- j& J# j4 o
        End If
; `2 U3 Q9 B5 f6 g0 W3 }        Values(ii) = Range(rngname)
( i/ U9 Q. ^% P        'debug.Print "Variable:" & rngname & " is:" & Values(ii)
& T& @* M) A( h% W        PrevData(ii) = ""   ' initialise
2 |" y& U+ s: [4 Q" R    Next ii$ f# g: n9 `) u* E8 X

! d2 d3 C8 I8 p    rindex = 2/ ?6 j" ?, ]5 O  d; T; C! f+ q
    'debug.Print RowHead.Columns.Count
# K7 h2 W& H/ g2 k- t9 F; c. r. z8 f9 ]    Match = False
- g( m& E7 M$ B! p9 Q4 s: p/ j7 F    For r = rindex To RowHead.Rows.Count
# D/ ?/ b* S# Q2 |7 M        For c = 1 To RowHead.Columns.Count   ' for each dimension; ~# Y! W6 z, u. t/ u8 e2 B
            data = RowHead.Cells(r, c)& T* K' z2 g6 T3 ]  E9 A0 }' ?
            If data = "" Then. U5 s3 w* U4 v4 m" m
                'debug.Print "Empty cell found: using " & PrevData(c)
6 S% @3 A$ c! F9 O6 E. b                ' use the last valid cell in this column/ D( a; E2 p* F/ Q- Z
                ' (this is to handle merged cells)/ b6 r* X: N8 K0 _6 T
                data = PrevData(c)
$ L0 |; [6 M8 E% S/ v- f            End If' ^5 s. u, }; l$ O/ v6 V
            'debug.Print "data:" & data1 g4 V( U- E( M+ w" O7 C
            PrevData(c) = data ' save for use by empty cells+ n5 m' a! Q4 k; I
            If data = Values(c) Or (data > Values(c) And LE(c) > 0) Or data = "*" Then
6 e/ J% b  Q( x3 c                If c = RowHead.Columns.Count Then   ' All columns match - It's a go
' `$ ?1 z$ `  H- q                    Match = True
5 \3 `, C% ^* Q9 V# O                End If" d6 t! G% \6 P6 {
            Else    ' This column doesn't match - go to the next row
! V$ E+ ^, h/ ]( k' w0 w; d                Match = False1 C& }+ K. x6 L7 Y4 F$ l0 v
                Exit For
) |  d$ e  q' f            End If1 u% N2 Z) A, j+ G3 R
        Next c
8 ?( R$ u( V1 T& J  q4 A) I+ s        If Match = True Then    ' Don't search any more rows4 ]' y6 N8 m( R9 a
            rindex = r
" L; h! B& F' [# k* V) ^            Exit For
, Z+ {& N" V  G8 K        End If
( u4 u9 k  o* P  Y  O5 F% f    Next r
7 ~, a* p( p$ V" W9 I  [8 S6 L5 T0 d" Y& V
    If Match = False Then   ' Didn't find a matching set of values
* v. x  Z! O! h4 Z' L' L        ZZL = "No match for rows"
7 ~7 v' F9 Q0 \        Exit Function  O* U2 p, {# X$ w3 p) r, t
    End If
7 ~1 j5 D/ [+ r1 F4 J7 E4 D
  M4 G4 Q0 w0 ?0 g$ ?2 m1 O! y9 w    rindex = rindex + RowHead.Row - 1   ' make absolute index
3 Q7 k8 ]+ c2 |& H$ D: zEnd If
4 j% S$ h0 ]- i! b
' [3 L6 g4 n" L, M7 e/ i2 J' Do the horizontal selection from columns
9 X1 Z; T& Q( r1 R* iIf ColHead.Columns.Count = 1 Then; l/ n0 i5 o0 P/ U# J# N
    cindex = ColHead.Column) F8 r$ [8 O7 m6 F9 x- w. o
Else
" i/ a! T6 k  [: l/ j4 V    ' Store the values to be compared with each row of the header
* O0 _- l7 H9 u0 ~* O+ Z( `    For ii = 1 To ColHead.Rows.Count
& h) d4 z5 B7 @        rngname = ColHead.Cells(ii, 1)
9 s& y! ~7 G3 j* x6 W3 y        LE(ii) = InStr(rngname, "<=")
$ L! M  y; v0 V1 Q) S7 h1 Q        If LE(ii) > 0 Then3 |* y, r% A/ ^
            rngname = Mid(rngname, 1, LE(ii) - 1)+ k5 A: K( D$ `2 o) b3 e* U
        End If: j2 K, r# U, B; ~
        Values(ii) = Range(rngname)3 l* @7 G7 n/ q6 c3 K2 O$ v. e1 S
        'debug.Print "Variable:" & rngname & " is:" & Values(ii). z+ }9 I- S# E& H
        PrevData(ii) = ""   ' initialise
( @1 o# b7 m' o, [) B    Next ii
0 D8 |. i* B4 O, \$ @# I5 ?2 ?5 T# N& |6 ]! a9 f% X; W
    cindex = 2
2 R. ~6 e+ Z# X- h    'debug.Print ColHead.Columns.Count
7 j8 Y) d0 B* `3 K: s  n4 i    Match = False
& W' }& t) l8 m8 _2 d    For c = cindex To ColHead.Columns.Count! P4 q! @( O/ Z. o0 T
        For r = 1 To ColHead.Rows.Count   ' for each dimension
8 P. k! r$ C& T" Y! c, I            data = ColHead.Cells(r, c)! h* H. B# O; ~/ T  I7 a
            If data = "" Then! ?8 E) @( Z9 S8 N7 q
                'debug.Print "Empty cell found: using " & PrevData(r)
+ P) f: D# s7 s# I" x# Y                ' use the last valid cell on this row
+ A5 w# H) q; S; ]1 b; K- c                ' (this is to handle merged cells)
; Q5 C& q* A1 Z8 J                data = PrevData(r)
( E+ \9 |: w  K" v  r5 S2 B            End If
) C& t1 Y% J* L0 Y- L- z- U+ ^' z            'debug.Print "data:" & data9 [2 y4 I3 V# S( q
            PrevData(r) = data ' save for use by empty cells+ V& q/ b8 S1 Y5 b
            If data = Values(r) Or (data > Values(r) And LE(r) > 0) Or data = "*" Then' ~3 Q' U5 |1 ^/ i" ?
                If r = ColHead.Rows.Count Then   ' All rows match - It's a go
; g6 j1 L- ^$ c- e5 Q* A                    Match = True
2 |/ P" ^  c# S                End If+ z) u) T  ?. e
            Else    ' This row doesn't match - go to the next column
! J1 B' g6 ]/ M3 T% l9 ^' ?. _6 h                Match = False% [" T, V$ P2 ]' E
                Exit For
# h9 w5 q! l5 a            End If
' y( D4 z! j9 F, _: \        Next r# w: u) C# S' Z6 t% ^, J/ W
        If Match = True Then    ' Don't search any more columns# r9 t% A$ F& _+ b$ y8 f+ E
            cindex = c
" N! S0 [! b; [            Exit For
1 L/ R" B* H( h, U1 [4 F% S        End If3 K# g/ P5 P! u+ b
    Next c9 V, y) o, @: [# P2 ]
3 H1 A! r  H/ g$ @1 i
    If Match = False Then   ' Didn't find a matching set of values6 a2 T0 |# g8 ^- _
        ZZL = "No match for columns"' m5 x. f7 Z* R! h' e6 o  a
        Exit Function2 d6 Y" b, f( N, I" W% Z* l/ F
    End If
6 }. J/ x1 k. m& k3 }
1 n2 x  V: \% a5 |    cindex = cindex + ColHead.Column - 1- o7 c( `% z% C' V" S
End If& k7 u) J, m* `  K. b' g% p
1 W; \/ v7 ]' _" J
' Return the cell value from Table$ f' d% d( s8 R# G5 ]  e* B# N
'debug.Print "Answer is in (R,C):  " & rindex, cindex, J& _! C4 ^  C9 ^4 D
ZZL = ActiveSheet.Cells(rindex, cindex). C( _& ?- _1 q. z
'debug.Print "Answer is : " & ZZL
7 q7 G( r) k! p) w2 ]- R1 QExit Function2 h1 |5 N6 B% u, F: i, i3 Z; H

6 e3 P1 w5 x4 b2 X. |. ~2 X# G5 Serr_handler1:
5 U5 p  t, i7 m5 RZZL = "Error on range '" & rngname & "'"
  e5 P* `6 t: B
$ {+ b+ z# u' g5 pEnd Function: |. S0 Z1 G6 O. a3 z* [5 b

  t8 T8 V; ?# k) s; 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-9-13 15:02 , Processed in 0.063219 second(s), 14 queries , Gzip On.

Powered by Discuz! X3.5 Licensed

© 2001-2025 Discuz! Team.

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