机械社区

 找回密码
 注册会员

QQ登录

只需一步,快速开始

搜索
查看: 1477|回复: 3

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

[复制链接]
发表于 2022-6-6 18:03:11 | 显示全部楼层 |阅读模式
Option Compare Text
/ i0 B3 Z2 Y( X" e; _2 M  \; @, ^& e1 H) W  K. e0 W$ k
Public Function GSXS(Ref)
0 Q. ]' ?5 E0 {# y
) X9 B, D8 T! A8 G$ J    GSXS = Ref.Formula
) g5 c4 ?" t4 {8 k; B7 g' O  c! D0 R/ }3 `1 Q+ c8 j& i( y0 X
End Function1 _( a1 n& S% l7 K1 B3 P" u
7 m% b. G" T& b! y9 m
Public Function ZZL(RowHead, ColHead, Dummy)% r! R) y% Z% C
$ _5 O) S& v; Q$ E( c
Dim Values(20) As Variant- R% z2 T8 ]. i5 ^" H
Dim PrevData(20) As Variant: L) u$ S2 H+ x4 d, p  t
Dim LE(20) As Integer2 `  W8 Q+ w$ x$ [1 P2 \
4 U1 p' A' @3 L& U; H- q( ^( T
On Error GoTo err_handler1+ h2 Z, q; `8 v& F' {7 \9 r
' Do the vertical selection from rows
1 L- K# b# |% Y! s6 eIf RowHead.Rows.Count = 1 Then
7 o$ c/ a: s  x5 T- l5 g2 I/ r( I7 K    rindex = RowHead.Row    ' first argument is any cell on the row of possible values
9 U$ I0 V% }* v; \' \4 X" `Else8 |! W" ?: s& x0 b4 q, u% [  _
    ' Store the values to be compared with each column/ }- o- _* \2 l) y
    For ii = 1 To RowHead.Columns.Count
% Z. i* @7 O5 @# n* ]1 s7 i* o) J  C        rngname = RowHead.Cells(1, ii)
+ n* y0 F# o! O& x& h        LE(ii) = InStr(rngname, "<=")
' N, T/ K" [5 m, n# U. q        If LE(ii) > 0 Then3 r" R' r# v! C' K! }% q
            rngname = Mid(rngname, 1, LE(ii) - 1); O* h: D" M: E0 N, Y/ Z
        End If$ k7 B! k# Q3 S
        Values(ii) = Range(rngname)
2 |9 u1 D+ W/ W* C/ P$ U        'debug.Print "Variable:" & rngname & " is:" & Values(ii)
* `' c: l  S2 M: v4 O0 J4 h) W        PrevData(ii) = ""   ' initialise
. Y6 y3 k' O- d6 y+ L1 c- f3 w    Next ii. n$ q9 ^: I/ T/ N! X  e
+ p& I( G( y, F
    rindex = 25 g# C0 C1 H( J! {; [2 D
    'debug.Print RowHead.Columns.Count
1 {0 |' A9 U# M+ o& q) |' d, }    Match = False% G' \; W8 h5 r, S
    For r = rindex To RowHead.Rows.Count+ v. Z/ g% k; w
        For c = 1 To RowHead.Columns.Count   ' for each dimension
- }7 R  O, g4 B1 [  f            data = RowHead.Cells(r, c)
  I( d) u6 v  C2 r/ K( p2 f9 n            If data = "" Then& O' m! L. A# K& I- j8 n
                'debug.Print "Empty cell found: using " & PrevData(c); }' M3 v4 Q" F0 M; f0 j% C
                ' use the last valid cell in this column/ c2 E# x' q3 Z. _3 O( l
                ' (this is to handle merged cells)
0 ?9 f& {1 ?' ^) V7 [) |                data = PrevData(c)
2 [  S$ a8 \, N+ Q            End If
4 E% v: B+ F$ W: u2 |; h3 X            'debug.Print "data:" & data
3 F9 N& L" D4 [            PrevData(c) = data ' save for use by empty cells
- g- _0 t4 y) a+ x* n            If data = Values(c) Or (data > Values(c) And LE(c) > 0) Or data = "*" Then; W. ^- z* a! Q
                If c = RowHead.Columns.Count Then   ' All columns match - It's a go
: c8 d: H; F2 m3 m1 H9 L                    Match = True9 i/ |: |9 X/ y0 m* {; [, n1 h! E7 b
                End If
1 k6 }$ W9 ]# B% J! _            Else    ' This column doesn't match - go to the next row
8 ?! g/ f- e) C+ A0 b                Match = False
! T8 l. A% u8 @* B                Exit For
9 h1 X* ]3 C  s' g( x            End If
% y4 B* z' i6 v' J: |1 p6 y        Next c, ~) `% _& z7 {
        If Match = True Then    ' Don't search any more rows
8 J; V- S8 F/ y* M2 C            rindex = r
9 v/ ^& `( E. h" M% J; m; ]            Exit For
( d* y+ q- b9 ^; _- G5 |        End If
5 O9 z- ]$ P$ q$ u+ W9 s+ }( c    Next r  H. `* D6 t; q

1 j4 |2 m* `7 X    If Match = False Then   ' Didn't find a matching set of values8 I; F& U# c1 R! ]' E4 ^
        ZZL = "No match for rows"8 e4 n8 h$ k8 Z' b) ?$ M
        Exit Function
/ E& \3 W  h; B: D7 v8 M    End If) B# y1 M0 @, ~8 T0 D' c4 d

3 f3 y6 \  G  [4 O4 a$ u    rindex = rindex + RowHead.Row - 1   ' make absolute index
8 M5 G* X5 l2 _8 x( VEnd If
, h7 z; q1 T& y# B6 c: `$ ?' N& D$ Y9 s# c( t
' Do the horizontal selection from columns- N+ J4 }2 O  E+ j
If ColHead.Columns.Count = 1 Then6 y2 _3 E9 H  M# y8 ?1 q- Q
    cindex = ColHead.Column0 [3 S- p) `; u8 s* d- b
Else
4 H- X( h, ?( f2 q) ?9 l9 k    ' Store the values to be compared with each row of the header6 Z: o3 p3 q2 @' s+ P+ l0 a
    For ii = 1 To ColHead.Rows.Count) i2 L' x- C# g
        rngname = ColHead.Cells(ii, 1)
0 A# Q( M/ D8 h; U) q        LE(ii) = InStr(rngname, "<=")- e4 `9 d! w  ~
        If LE(ii) > 0 Then% g- Y* @, ?+ i- y; C' \  u, Z
            rngname = Mid(rngname, 1, LE(ii) - 1)
9 K# R; h" z& J8 [# z        End If
% t  T" ?4 s; I9 x& _  g        Values(ii) = Range(rngname)
, U- j$ ?/ C# f        'debug.Print "Variable:" & rngname & " is:" & Values(ii)6 k, K9 Z' T: k. u
        PrevData(ii) = ""   ' initialise
; V+ k( w  i- l. ^3 O7 [9 V9 W    Next ii
3 y! ?' q- V; ?: U! f' F- p6 B8 a# ^/ C' x" n
    cindex = 2
* t1 f- [0 F. m( |    'debug.Print ColHead.Columns.Count# d" Z4 x+ D( \7 h+ N
    Match = False/ }: F) j6 c9 D3 {  |) j
    For c = cindex To ColHead.Columns.Count
8 X5 C9 |; a  }9 ?2 I        For r = 1 To ColHead.Rows.Count   ' for each dimension) P6 h$ h% g5 J$ m' m, ]
            data = ColHead.Cells(r, c)
* S  s# P  J2 K9 _; F% l) M            If data = "" Then
$ G# i7 T. p' F2 j8 z8 e5 U                'debug.Print "Empty cell found: using " & PrevData(r)
4 ~2 D' y5 N9 o5 `1 B! N3 b                ' use the last valid cell on this row" ]6 m) o. r/ a- F
                ' (this is to handle merged cells)
* f/ O& {. b+ k' ~+ |+ W7 A                data = PrevData(r)
% C8 s! ?" O. e/ f  r$ }, F6 U            End If: M' ?" D6 ^2 Q  I% j, l" ]
            'debug.Print "data:" & data
+ c  r; s* H# u# \3 A  O2 W            PrevData(r) = data ' save for use by empty cells
5 n/ G% n' [" ~0 v            If data = Values(r) Or (data > Values(r) And LE(r) > 0) Or data = "*" Then
* A% ^9 D7 a  S0 h' \9 H                If r = ColHead.Rows.Count Then   ' All rows match - It's a go( @  ^+ [2 l/ z+ `! c
                    Match = True
# `$ j& A, J( ], ?) I! B" j% S6 u                End If
8 p. I3 O! D8 k" A  T            Else    ' This row doesn't match - go to the next column
9 X5 @% o' a" a1 x  C                Match = False: ^- @3 j! i2 D
                Exit For( _+ [1 m0 X1 c0 G$ R" D6 ]9 Z% r
            End If
. A+ ]& ~0 W: Z        Next r
3 V! j0 v/ \. \' _" X1 m        If Match = True Then    ' Don't search any more columns" ^, o% U" f1 ]  r8 {' \
            cindex = c0 j! p4 c8 {2 ]0 _
            Exit For
! d& @- Y! U! y% W7 l# P* w        End If- h7 z8 D3 u; s3 F9 v
    Next c5 B' U: ]# t0 D1 m+ r" ]# _
& }0 A) X5 i6 h5 e5 k, r
    If Match = False Then   ' Didn't find a matching set of values
1 ]) m1 X& n8 x/ D: H        ZZL = "No match for columns"
8 z7 \- `6 {. ^: D        Exit Function3 c+ k5 J4 X" N& l. _: @
    End If& U4 [# F+ u* h0 V6 ?$ u
1 ^+ j0 }7 \# U' g. S! ~
    cindex = cindex + ColHead.Column - 1# ~4 ?7 _$ Y4 t6 k8 S
End If
1 h- C2 b& x& _/ R3 S0 f; H- S5 r% w. q+ l: h8 x- l
' Return the cell value from Table+ m# e9 E- p( G7 y- s. ?6 K- y
'debug.Print "Answer is in (R,C):  " & rindex, cindex" L  W+ \  t! M/ y5 q* w" ?; h
ZZL = ActiveSheet.Cells(rindex, cindex)
6 N( ~2 j9 \$ s'debug.Print "Answer is : " & ZZL7 i7 G% h/ c1 A! o3 ]
Exit Function: e. l' V0 {" R- a
! j# _( h5 w- v) B7 Y: m7 |: s
err_handler1:
4 l! ^3 Q. j% x$ DZZL = "Error on range '" & rngname & "'"7 x$ M2 m( g9 W1 ?* s: J6 W
$ J5 @. w: H0 P
End Function( n$ C6 f+ M/ l' e! Y
2 U' M6 K- n4 M3 ?5 t
回复

使用道具 举报

 楼主| 发表于 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-4-12 06:24 , Processed in 0.056906 second(s), 14 queries , Gzip On.

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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