机械社区

 找回密码
 注册会员

QQ登录

只需一步,快速开始

搜索
查看: 1289|回复: 3

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

[复制链接]
发表于 2022-6-6 18:03:11 | 显示全部楼层 |阅读模式
Option Compare Text2 \' y2 a, ?1 A+ X1 O" Z
* B0 K$ o* w/ H% ^4 A7 }
Public Function GSXS(Ref)
& t" d+ r" a4 Q2 m" M5 k& Z
, p/ j0 y$ e9 c; `    GSXS = Ref.Formula
5 ^* j  k3 s7 c1 x* P* @6 d2 s( Q# ]% L6 b; f* f/ u
End Function& J; K+ C- X5 `6 B) x. e
! C; E; B/ c. x/ v* n5 c
Public Function ZZL(RowHead, ColHead, Dummy)
2 Y$ o! B  t7 A9 B) {, i7 X( t5 `* U0 F" d* V
Dim Values(20) As Variant, e" W- C5 R2 L
Dim PrevData(20) As Variant1 I- d) B9 t4 V: g9 N$ T
Dim LE(20) As Integer0 q2 E2 K. H4 P% E- \' n

& |0 i! A/ P7 j8 IOn Error GoTo err_handler1
% A8 v/ a9 O- W& |. I; s/ e' Do the vertical selection from rows
  u& i; m* L& y. C1 Q8 \If RowHead.Rows.Count = 1 Then. z  Y! M$ \5 [/ ^: V
    rindex = RowHead.Row    ' first argument is any cell on the row of possible values; y' t9 {4 h! y% l6 Q
Else
7 _* @: x2 K2 N. B    ' Store the values to be compared with each column& g/ T( @6 m7 c. t
    For ii = 1 To RowHead.Columns.Count# X$ Y* h2 U9 G7 m
        rngname = RowHead.Cells(1, ii)1 W; j, p; }, o' L1 J
        LE(ii) = InStr(rngname, "<=")
4 Y$ \( ]+ }) v$ U: H. y/ U# d        If LE(ii) > 0 Then
* b1 M; e. m/ c6 O4 [0 A% I            rngname = Mid(rngname, 1, LE(ii) - 1)
  q$ x: o. }. e9 R" L. G0 P# b5 I        End If* L6 I8 q4 S0 R6 U5 Y" f. }
        Values(ii) = Range(rngname)
7 g- s8 G5 W. X        'debug.Print "Variable:" & rngname & " is:" & Values(ii)
" ~# m) _  |9 y! I* h3 ^% u        PrevData(ii) = ""   ' initialise
) j( M% n, ?  L. ]" x! O    Next ii: ~& j6 \0 M/ Z6 U1 J9 O: q

) U) Y7 C, e) B* X% I    rindex = 2
. p3 ?, @$ \! u, ~7 E0 u    'debug.Print RowHead.Columns.Count$ j; a; h; x8 B  W! l4 {
    Match = False; U& V; E; C0 A9 |
    For r = rindex To RowHead.Rows.Count) v0 y; ^& h9 A2 T" Q* f
        For c = 1 To RowHead.Columns.Count   ' for each dimension6 o0 `+ v% F% C' C8 a+ j0 H
            data = RowHead.Cells(r, c)* Y9 o7 K& M  }7 p9 }# m
            If data = "" Then: \$ Z7 N. Z1 @+ q$ }
                'debug.Print "Empty cell found: using " & PrevData(c)4 D2 B, G3 ]$ U* z- t( Z9 _
                ' use the last valid cell in this column+ F! u' D! t% z
                ' (this is to handle merged cells)
2 k- Q1 g, k% T# t$ H                data = PrevData(c)
( F' r# h+ |$ \4 i0 e            End If( G4 F$ z' p: \# _3 E/ C
            'debug.Print "data:" & data
+ f* c! ~* U* n8 u: R            PrevData(c) = data ' save for use by empty cells4 S$ d* b0 A! n, I7 o, V/ ^
            If data = Values(c) Or (data > Values(c) And LE(c) > 0) Or data = "*" Then. k# ?7 p/ d# g+ k9 A; ~* R
                If c = RowHead.Columns.Count Then   ' All columns match - It's a go# F) q: X. w0 y$ a' c4 h  f
                    Match = True: c% r4 [- \# X7 u- y0 r
                End If* [0 C& Y% _; e( H7 s( P  ~
            Else    ' This column doesn't match - go to the next row, g& g+ ?+ z( P
                Match = False
' @0 C( \/ N% c3 Q9 Z                Exit For, c6 U8 |7 G' X4 x- B. f- g! Y2 e
            End If
! G- _% N5 _0 i& r& U        Next c. _2 H& B) l+ l- V2 f) [
        If Match = True Then    ' Don't search any more rows3 e) C. X  a. M# e/ D
            rindex = r1 e9 ~7 X6 K4 j& o
            Exit For
. U: v) {/ M3 ?- A$ ?, ?        End If- |6 n+ K, M" D
    Next r8 A1 s" T, f( ^4 n

9 g' d8 g& j1 {& j1 `! c    If Match = False Then   ' Didn't find a matching set of values
: [: F) V. \8 c6 B        ZZL = "No match for rows"
$ |1 Y; [- r# @  {# U        Exit Function/ S' g- I9 e- `
    End If
0 b' S1 G, l" C0 \6 m! r$ U- ]0 ?2 \8 z6 u/ G
    rindex = rindex + RowHead.Row - 1   ' make absolute index% m9 F+ S+ f1 Q: k3 u3 q2 o/ r
End If
2 b( n0 x2 u$ B0 q: a+ f7 h' F# l! w$ v
' Do the horizontal selection from columns
5 T! ]! z1 f+ Z" j; w4 ?, UIf ColHead.Columns.Count = 1 Then
  w- p7 h$ m, L( W# C    cindex = ColHead.Column. `0 V% X/ X/ Q( |
Else
7 l6 z  Y9 j+ W/ f  ^    ' Store the values to be compared with each row of the header& E5 L5 G1 P! }" n: g
    For ii = 1 To ColHead.Rows.Count
+ B0 {5 r! G# m4 Y        rngname = ColHead.Cells(ii, 1)3 P6 `" i6 S' ~4 `* @/ E
        LE(ii) = InStr(rngname, "<=")
2 s4 y9 }% a) _8 G        If LE(ii) > 0 Then
* y8 k" F2 C( l" X( u* Z9 V            rngname = Mid(rngname, 1, LE(ii) - 1)% D" `& N( o  u8 T) ?3 D
        End If
: H8 D6 V( e/ l8 G9 Z& Y" K% i        Values(ii) = Range(rngname). f# s9 T% W+ J- Z8 t
        'debug.Print "Variable:" & rngname & " is:" & Values(ii)
% ~8 F9 ]# k0 U& x. b  E- }        PrevData(ii) = ""   ' initialise2 S# u# x  B0 Z1 \
    Next ii
$ E( F( D5 k, d6 }* E( v' c( x. Q4 ^1 g( k2 S
    cindex = 2+ H- F$ S: r0 ?5 N% F, I
    'debug.Print ColHead.Columns.Count9 O( t8 O% x" ^* e# m7 ~
    Match = False9 B4 |$ U+ X9 l& Z2 U: U+ W0 Q
    For c = cindex To ColHead.Columns.Count! v) S! M" ]/ h  x- N
        For r = 1 To ColHead.Rows.Count   ' for each dimension
# B( Q& f& W, F4 _4 x; i; Y            data = ColHead.Cells(r, c); V( |0 i% p5 V' E: h
            If data = "" Then
# Q* m* ^8 x) V7 [                'debug.Print "Empty cell found: using " & PrevData(r)
! g0 |* ^9 l+ L6 R5 E; t# s                ' use the last valid cell on this row
* k! J, J5 j" i. r  E* _                ' (this is to handle merged cells)
/ {( c0 k  P  h9 D8 ^2 }0 I                data = PrevData(r)/ {" @2 z0 r5 f4 }( F
            End If
2 p, {, z2 K5 K. l            'debug.Print "data:" & data* |8 b3 W' n2 ^
            PrevData(r) = data ' save for use by empty cells
: P/ t: f8 R0 l            If data = Values(r) Or (data > Values(r) And LE(r) > 0) Or data = "*" Then
  |7 H) C- G1 c                If r = ColHead.Rows.Count Then   ' All rows match - It's a go
" @3 E3 H( r2 q8 v( |/ v                    Match = True) I( r7 f# `1 A1 G& p/ f9 n
                End If) y, G- X$ U+ K0 r% [2 o
            Else    ' This row doesn't match - go to the next column0 e4 D& t, k0 [
                Match = False- x; B$ K! N7 H7 H
                Exit For7 M' Y5 [& N* R2 B6 \
            End If
, `# t( X9 w4 Z8 T& y9 g        Next r. i  e" [! l6 F. q
        If Match = True Then    ' Don't search any more columns
( a# a0 |- m9 }8 F            cindex = c
3 i# j! r  Z: e$ T2 H/ R            Exit For& Q  x2 u+ g$ S5 D( z
        End If
3 G5 m- q7 o, v5 |" ^  F4 J# c- y    Next c% K$ A3 V4 f% k1 Y' ~4 m4 c7 x( _
/ H$ t, ^6 j# \6 _
    If Match = False Then   ' Didn't find a matching set of values& t7 @) C; p5 Z+ z' ]6 h0 R
        ZZL = "No match for columns"4 \2 ]0 k# R- M1 t  j  {7 ]/ r
        Exit Function
* h0 B+ r1 i7 v* \( C    End If
+ F% j3 U* L" ~6 D2 [+ ~
) k! X8 X! v6 U" f' y# N2 p+ `5 ]    cindex = cindex + ColHead.Column - 18 M+ {3 d" P; d; u/ F  G
End If
( ^$ D& X! P& C, U3 l; r. C$ K0 H' L' M) X( \+ D" v. [% {
' Return the cell value from Table( T7 U1 @6 L6 u/ l& A5 W/ g4 s
'debug.Print "Answer is in (R,C):  " & rindex, cindex
1 \( a0 a4 I0 f& p3 O7 ~& N2 sZZL = ActiveSheet.Cells(rindex, cindex)
0 C, d. w4 \6 G'debug.Print "Answer is : " & ZZL) U5 H2 g$ p& h) D
Exit Function( B% T2 |; B7 T- e! w9 G) E6 b
, d5 m6 I3 |* e/ V
err_handler1:
( ?" v- _3 b+ W8 h- _' R7 T- O  QZZL = "Error on range '" & rngname & "'"
+ X* ~1 H' c6 O& r
: X" O: I1 I; j3 GEnd Function
  W# j7 `; j8 _
& g3 J9 z! p$ n- c. R+ ?
回复

使用道具 举报

 楼主| 发表于 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, 2024-11-23 04:34 , Processed in 0.054186 second(s), 15 queries , Gzip On.

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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