找回密码
 注册会员

QQ登录

只需一步,快速开始

搜索
查看: 1616|回复: 3

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

[复制链接]
发表于 2022-6-6 18:03:11 | 显示全部楼层 |阅读模式
Option Compare Text+ w) |+ T7 u. n3 q( w$ H

* t4 w0 A  ]! T7 `, P) @5 l  M2 pPublic Function GSXS(Ref)- N4 C4 L! q) D4 V2 T  t/ W$ J
) S9 ^, m& K$ H: q
    GSXS = Ref.Formula  g8 s+ K) d! F) a! G

9 a1 i3 w+ Z( V" [" F# zEnd Function
) I' _5 j% K* Y0 R. f% V5 T9 e7 N7 I; R
Public Function ZZL(RowHead, ColHead, Dummy)
' S  b% P; o' k8 {1 c! n6 K
7 w% g7 Z# N8 GDim Values(20) As Variant
. T5 g% }9 ?, A: h. F! o7 v) {/ e2 EDim PrevData(20) As Variant; ]  J# X! T, g7 e1 E/ `2 _) c
Dim LE(20) As Integer
8 r# I- x% v! d- |8 D! _
& N6 Y& \6 q2 l' D. ~/ F( Z' l: jOn Error GoTo err_handler15 h2 l7 N' d+ y" O2 g
' Do the vertical selection from rows4 G! n7 z0 d& L2 z; e% [8 t3 ?* D$ V* P
If RowHead.Rows.Count = 1 Then7 Z5 M) Q: y$ ?2 t) U9 \3 x
    rindex = RowHead.Row    ' first argument is any cell on the row of possible values, \) H6 G. R2 k
Else$ }2 ~/ a& T! T
    ' Store the values to be compared with each column
! ^! O; K6 J3 k. k# P% B; K8 F" N    For ii = 1 To RowHead.Columns.Count7 j# w/ E* }! d( [& u3 C: n
        rngname = RowHead.Cells(1, ii)
* ]3 a3 _$ K) Q5 v. [4 I. N        LE(ii) = InStr(rngname, "<=")/ [6 q1 v% }* C4 N9 H
        If LE(ii) > 0 Then7 M+ Q. |  N1 G1 r# |# r
            rngname = Mid(rngname, 1, LE(ii) - 1)
0 K* F1 Y: u0 |1 r* I8 m5 x$ G        End If2 U: _2 x) P' t- d$ e- D( T8 F$ g" j6 S
        Values(ii) = Range(rngname)0 ?2 F( ~: M$ D$ V2 E
        'debug.Print "Variable:" & rngname & " is:" & Values(ii)) @/ W* F% ?% _8 I+ q3 }
        PrevData(ii) = ""   ' initialise1 L, q, ^/ k7 Q( f% @2 n9 t
    Next ii
$ s* L5 {2 |2 U8 D
% P: V5 j! W1 R    rindex = 20 O. U5 h8 L/ u3 V
    'debug.Print RowHead.Columns.Count! ]6 H2 j* }# I% c8 V+ S
    Match = False+ }& V8 K6 R; Q+ }5 ^
    For r = rindex To RowHead.Rows.Count6 f1 [3 d+ }7 @: w8 P  f) I7 C
        For c = 1 To RowHead.Columns.Count   ' for each dimension
$ _  f+ t3 w" t            data = RowHead.Cells(r, c)' n2 z8 _: h2 z/ x1 z$ o" D- ^
            If data = "" Then
  B" T- \% p* Y' n                'debug.Print "Empty cell found: using " & PrevData(c)1 y9 _1 r9 f# g6 c  S4 r9 }3 W
                ' use the last valid cell in this column
: _  i8 h0 ]( `) F                ' (this is to handle merged cells)% q/ h6 y! |; ]) E4 [
                data = PrevData(c)
2 h2 z1 ^9 N& X) a            End If
, c; w5 F6 \# O( s6 L1 G! h2 v            'debug.Print "data:" & data  T) b! @' }. S5 g$ i7 G) I
            PrevData(c) = data ' save for use by empty cells2 d7 \. Y3 g5 A. _* [3 ]9 z
            If data = Values(c) Or (data > Values(c) And LE(c) > 0) Or data = "*" Then
3 s* f9 C. K8 g" k' }' s- B                If c = RowHead.Columns.Count Then   ' All columns match - It's a go" u- R( A" `. A0 a& T0 W% c
                    Match = True, N6 l5 s" n. V; J/ B' C! x) K2 s) P
                End If7 S: c0 j( ]9 T. c$ v
            Else    ' This column doesn't match - go to the next row
/ ?! W. h8 [" H' Q. T                Match = False% k* h: j6 B7 k# v2 w
                Exit For
% c) r& L: S! ^% I* [) p: K            End If6 ?$ }) ]' }, c2 e. f
        Next c; \; B- t. ~, M
        If Match = True Then    ' Don't search any more rows9 g+ n) L) s/ {7 W; P7 N, m: M
            rindex = r
' e0 c: N6 c8 G& U; T- ?' D; E            Exit For8 g+ K2 v+ Z* D& @& [! B$ X; w
        End If* G$ H2 r: j! V  `9 n* \3 H, j- {, {4 R8 c
    Next r" A8 |2 C# w7 N) C$ Z) \

/ m+ e  h% c4 @: P3 i# s    If Match = False Then   ' Didn't find a matching set of values( j3 {2 M+ [' s1 |6 S
        ZZL = "No match for rows"1 }6 ?1 I+ W2 U6 C+ D1 }0 c; {+ l% M* x  J
        Exit Function
$ U' G0 ~. ~" F, l- j0 i    End If
$ q" Z/ P# \3 {
' S) V# a9 i9 w4 R, W; [    rindex = rindex + RowHead.Row - 1   ' make absolute index2 B* a% g5 M1 H+ R3 t3 j
End If. i9 G! a/ j# i
4 z+ H; T9 i4 r. k0 f+ X
' Do the horizontal selection from columns
- B( E4 l0 u2 t, M1 z4 DIf ColHead.Columns.Count = 1 Then4 l. _& S+ w3 }' U
    cindex = ColHead.Column
+ D9 I. _: o" z6 [3 l$ SElse
+ y7 j$ P* y/ D7 g, B    ' Store the values to be compared with each row of the header
& u% i9 C, v, P- M) V9 l# P    For ii = 1 To ColHead.Rows.Count
5 o" {% c# H: r$ o/ t        rngname = ColHead.Cells(ii, 1)
3 A% @0 R2 f' H, I3 s        LE(ii) = InStr(rngname, "<=")
! `0 Q4 g/ S  d& v) P        If LE(ii) > 0 Then( P* s9 U/ d, q% F' b' u
            rngname = Mid(rngname, 1, LE(ii) - 1)
! k7 |# Q1 {" x7 Y) A        End If
3 \+ `3 }4 Y& t: O        Values(ii) = Range(rngname)
  }! D/ w5 o8 m9 R3 W        'debug.Print "Variable:" & rngname & " is:" & Values(ii)
! s4 Q3 o1 M# X, P, V7 X# M" f        PrevData(ii) = ""   ' initialise+ Q. ~6 w0 M, n1 c  t4 X2 m
    Next ii1 Z0 u4 e  t% G4 r: f: C
0 ^( ]( t2 L4 \& Y. r: S3 e
    cindex = 24 Q' m- N% i9 m1 q% Q1 p
    'debug.Print ColHead.Columns.Count0 `5 K2 [( M- T7 \
    Match = False
& \. Z  w" u* i" U& C! G    For c = cindex To ColHead.Columns.Count
! }3 W* V0 ~2 q* H; Q4 |% `+ a        For r = 1 To ColHead.Rows.Count   ' for each dimension5 S3 ]- F! Y. u8 L$ Z" b6 k
            data = ColHead.Cells(r, c)- _5 \  s9 }3 _1 }# d# K8 b
            If data = "" Then% m. O' b1 z/ _9 y
                'debug.Print "Empty cell found: using " & PrevData(r)
" z, O1 w7 X, z# u                ' use the last valid cell on this row
& A  c' v4 r6 x8 V% _                ' (this is to handle merged cells)/ T: F" R' r& H2 a- L
                data = PrevData(r)
; D. v9 {! K2 X2 D' e' `, k            End If0 S. ]2 \6 f1 i. J
            'debug.Print "data:" & data, |: s1 W# [- U% p4 n3 `4 D# O
            PrevData(r) = data ' save for use by empty cells% M: O+ I2 x0 [5 T  k) l. }3 X
            If data = Values(r) Or (data > Values(r) And LE(r) > 0) Or data = "*" Then$ b2 _( H8 s- @, E9 c- a
                If r = ColHead.Rows.Count Then   ' All rows match - It's a go, t! F/ v) Q. I  l  V% y  e+ i
                    Match = True
0 f0 H. r* ]$ C- l& d  g& W                End If4 }0 `$ a5 T8 a1 Y5 t, A9 L% I% X
            Else    ' This row doesn't match - go to the next column( T0 M: i5 Y% i! B1 Q+ s' W
                Match = False
' t, O) W4 H+ @; \, j- k                Exit For
6 q7 H/ ~) ^9 M& _9 g- Z            End If
6 ?/ h1 B4 S, ^5 l6 p9 A        Next r
5 ^& z) ~+ i: l+ y        If Match = True Then    ' Don't search any more columns
) [8 H; r) {- N2 ?! C7 b# ~, B            cindex = c' v* c: D: W+ I$ T
            Exit For' p- h0 ]+ R+ J5 R
        End If1 p1 a' p3 X" D  S
    Next c
3 ?* R; E4 L- d8 R3 J1 S9 u$ d4 r8 N" p
    If Match = False Then   ' Didn't find a matching set of values
9 [! ^( `" G* |, Y/ f6 I  h        ZZL = "No match for columns"( V0 F9 {$ A1 Y4 ^5 D( u+ j* Y
        Exit Function
0 }6 M% E0 S  D7 `3 Q1 a    End If
2 F- C* \& h+ m8 @8 Z" \7 O$ S5 `! m0 }& q: ]
    cindex = cindex + ColHead.Column - 1) H1 N) X' \5 q1 ]: A+ t8 r: o
End If: R4 h& G# t3 g0 l1 r) ^

8 V7 j% J( G& `1 c' Return the cell value from Table! N! [) D: D+ L; h' ~) o
'debug.Print "Answer is in (R,C):  " & rindex, cindex
% U9 t: Z$ @4 {3 _ZZL = ActiveSheet.Cells(rindex, cindex)+ w/ @8 |( r7 T. j+ a5 \- A4 ^
'debug.Print "Answer is : " & ZZL+ W( t) {# L3 v
Exit Function$ y5 N$ t- W& b7 I9 |" L4 H

8 A! w2 f3 g) O% e6 o: Herr_handler1:
( U! W$ ?4 X  g7 RZZL = "Error on range '" & rngname & "'", a" C4 O0 Y, w+ J- N1 I
1 R6 Z, Z7 m* W8 C5 `6 M# s
End Function3 [; I/ \6 P0 x8 q

5 W/ U3 r. g0 Z+ W) e$ S' i# k
回复

使用道具 举报

 楼主| 发表于 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-7-15 18:06 , Processed in 0.060341 second(s), 14 queries , Gzip On.

Powered by Discuz! X3.5 Licensed

© 2001-2025 Discuz! Team.

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