找回密码
 注册会员

QQ登录

只需一步,快速开始

搜索
查看: 1604|回复: 3

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

[复制链接]
发表于 2022-6-6 18:03:11 | 显示全部楼层 |阅读模式
Option Compare Text
# n, F7 V8 g7 I% b& Q' b* ^. `. B( ^7 ^5 |. Y* l  I. ]
Public Function GSXS(Ref)* y4 q6 c* H' [9 A4 L
$ V2 G- ?9 o; N) b' ?: ?
    GSXS = Ref.Formula
6 \7 A2 |8 b5 \9 b& @
( M" Q' U, E" ?+ iEnd Function
" U4 r, {, a, i) |
8 r/ b- X+ L1 L) j' Y3 u4 a! iPublic Function ZZL(RowHead, ColHead, Dummy)
, R% V  O: v; q+ ~7 S2 G0 M  D2 E
4 \  e7 O# ?1 C/ V) S6 }1 @) cDim Values(20) As Variant- G' ~: @( C4 Z2 @8 U/ K& _) y
Dim PrevData(20) As Variant
* B# k( A# Q) tDim LE(20) As Integer9 b/ ~4 L# @6 r* u! r' ~- }" l

$ P$ u  e, O0 B" |  k. ?On Error GoTo err_handler1
8 d* e  W9 O7 [0 ~. N0 ~9 ?* v7 _' Do the vertical selection from rows
( @: x6 X, V/ JIf RowHead.Rows.Count = 1 Then' R1 q- ]2 W' k/ J' w6 l9 E
    rindex = RowHead.Row    ' first argument is any cell on the row of possible values
/ A2 t; ]0 p$ t- XElse
, ]4 F; F; t" u& R- W5 X8 E    ' Store the values to be compared with each column; S5 U; A: ~0 S
    For ii = 1 To RowHead.Columns.Count
/ o- Q9 n) Z' y3 V        rngname = RowHead.Cells(1, ii)6 O; G7 s8 w/ O9 T) ~9 b
        LE(ii) = InStr(rngname, "<="); S5 X# F6 o/ b7 l
        If LE(ii) > 0 Then( l+ F6 b6 o  R" _9 |
            rngname = Mid(rngname, 1, LE(ii) - 1)* H& Q$ u2 `  `0 w" {* n
        End If
7 L. Q& H4 {- C- r8 S$ J        Values(ii) = Range(rngname)
" Y- |+ ]3 d- @+ R3 J5 z        'debug.Print "Variable:" & rngname & " is:" & Values(ii)$ _9 V; C9 C& W" q  U
        PrevData(ii) = ""   ' initialise
6 Q; B+ L+ \' D* I* J8 w9 c    Next ii6 l5 p+ E' ^3 k# O' O8 x! u" P
8 L6 a# X" R* t
    rindex = 2
6 P! X8 C, @* k" s    'debug.Print RowHead.Columns.Count
' M+ m) V4 H6 c/ f9 f6 m# a( c    Match = False: F% D0 \! t$ k( T6 s9 l
    For r = rindex To RowHead.Rows.Count
$ O8 B2 r7 R- z( J/ H        For c = 1 To RowHead.Columns.Count   ' for each dimension
- A8 H2 w* D9 h6 ?' p. d2 x            data = RowHead.Cells(r, c)
: O7 o3 [3 M6 s. e6 V            If data = "" Then
* v" D. ^* G& X, l                'debug.Print "Empty cell found: using " & PrevData(c)" \$ T. l! R6 Y1 ~. Z7 E5 |
                ' use the last valid cell in this column
4 e' m5 ]  C6 z% Q                ' (this is to handle merged cells)/ q+ P3 x/ S0 h% L
                data = PrevData(c)
9 i; R0 M8 b$ F% Z) j            End If, M! h$ W% `1 E& j2 v
            'debug.Print "data:" & data. L/ y+ n' _8 O' T3 z
            PrevData(c) = data ' save for use by empty cells
9 V) |6 d& r# Q            If data = Values(c) Or (data > Values(c) And LE(c) > 0) Or data = "*" Then
3 [  s% v( t. G: x/ G                If c = RowHead.Columns.Count Then   ' All columns match - It's a go; Y# E+ I* {3 q. E" R
                    Match = True: p9 y$ M9 Z) x% Y' y
                End If4 ]5 @. g+ i; N( E
            Else    ' This column doesn't match - go to the next row  U8 |* k7 z* x" H
                Match = False
2 _7 A* M! U4 |, i1 ?- P2 v2 ^' a2 @                Exit For; R. N: w  W+ Y  l& U9 s4 p! x
            End If9 k( q: a( L3 k: O6 Y
        Next c
7 S% ?" W+ u, F" ~9 g# g        If Match = True Then    ' Don't search any more rows
; h5 @$ L  M3 t! x. \  M- l            rindex = r
8 t% `' G9 p( E% [8 h; H7 S7 g            Exit For" q7 t+ q/ s: m1 `; j7 V/ s- B% U
        End If, P2 I1 d8 F$ R% k& q/ y
    Next r- @5 k% `/ c9 b# ?7 g$ O% x6 a

1 |# E. R. c+ d5 W2 j, @    If Match = False Then   ' Didn't find a matching set of values8 C2 s2 I5 a/ l, V& a* \
        ZZL = "No match for rows"
9 @9 {% X2 t6 k3 c* i2 a. e0 o        Exit Function# h8 x  a$ I0 a$ D5 ]
    End If+ E/ e, Q! ?/ U1 F: G

" \& g9 c& F4 O6 G3 O8 H    rindex = rindex + RowHead.Row - 1   ' make absolute index3 X6 B7 H, E- I2 `1 k( H
End If
$ ~: K2 `5 R9 l" W% O( l
/ }  {2 N( c+ q7 e! M/ |+ [+ i' Do the horizontal selection from columns2 R8 j5 g" ~8 p- a: T
If ColHead.Columns.Count = 1 Then
' ?4 |% X' y& }8 m, A  [% z    cindex = ColHead.Column6 [2 A/ D* o0 {6 |
Else7 R$ Z* `' I: `+ G1 o
    ' Store the values to be compared with each row of the header
' f0 F7 Z: X1 X! y3 L& \    For ii = 1 To ColHead.Rows.Count* t+ j% {4 n( t; F" Q
        rngname = ColHead.Cells(ii, 1)3 q4 \* }4 u  z. {
        LE(ii) = InStr(rngname, "<=")
1 P" E7 e8 z# L- }' U! |        If LE(ii) > 0 Then$ k7 o1 J6 u, W6 p8 Z  x
            rngname = Mid(rngname, 1, LE(ii) - 1)' ^9 m* L9 ]( ~9 c8 G
        End If" F! e9 A& l( G$ ^
        Values(ii) = Range(rngname)
$ X: S# ^# y% P+ T& Z6 g! f        'debug.Print "Variable:" & rngname & " is:" & Values(ii)0 v1 r! p$ x0 t, j
        PrevData(ii) = ""   ' initialise* ?7 ^  R0 T+ g  w1 ^  O
    Next ii
9 F8 i1 a  L# w$ Z+ b$ l+ z3 H5 {8 J) k" {1 h1 T4 {' B) }! Q
    cindex = 2
, h- {9 W' [. V) P    'debug.Print ColHead.Columns.Count
! g  N1 B7 [  q6 e    Match = False* Q9 Y: y; k- `- G8 o
    For c = cindex To ColHead.Columns.Count- d7 u: x# y2 t3 F* p
        For r = 1 To ColHead.Rows.Count   ' for each dimension
$ E+ I% d# D% S# c* e& |' r            data = ColHead.Cells(r, c)
% W  D+ l" c& C+ _9 \  U            If data = "" Then) h, n& I  D: _: x' R( E) y' y
                'debug.Print "Empty cell found: using " & PrevData(r)
  w) n, N% t# Z$ o7 i, q                ' use the last valid cell on this row, Z/ ^0 S5 g6 f8 K8 M* t* d
                ' (this is to handle merged cells)
- X; n8 y$ {: e0 p( M! w/ x                data = PrevData(r)4 Y' g# L1 ^# Q3 S. F
            End If& Z0 w7 b; k% }# y, W8 Y
            'debug.Print "data:" & data
: }( _- P( [! ^- d9 S& J( }            PrevData(r) = data ' save for use by empty cells
. X+ M' P+ U8 B1 A            If data = Values(r) Or (data > Values(r) And LE(r) > 0) Or data = "*" Then
& P9 O. I/ G3 H+ ^  {                If r = ColHead.Rows.Count Then   ' All rows match - It's a go2 x; i' i  ~' U& B
                    Match = True
' w( y- F8 n/ m! j) l, g                End If
! r+ w: J0 Y4 m4 g# S" @            Else    ' This row doesn't match - go to the next column
' n" I$ L3 U7 V9 \/ g! H                Match = False- Q' K# v0 k0 ~% s0 u+ X; c
                Exit For* q2 Q% n- k$ z% L3 T7 l4 y
            End If
* @$ ^0 s+ F  D1 t$ p; ^" A2 B        Next r, Y% I: L. T  @2 g5 [
        If Match = True Then    ' Don't search any more columns
! ]4 S  ?" t, l" Q; W; u2 k+ a            cindex = c
9 r% @0 l. s8 ^8 G$ ^            Exit For
7 F( P7 J& e$ M0 b- b        End If( u) s# b4 K2 c' Q8 n$ G
    Next c
  z. j- |" p: @% |8 l# @5 e% ^0 M4 }. P) N& u' K2 W6 v
    If Match = False Then   ' Didn't find a matching set of values
) \5 J; }  \( N8 i1 @( Y7 }        ZZL = "No match for columns"' v/ \% f- |4 S3 o5 d
        Exit Function9 a0 V. S3 @, Y$ [: M0 Y
    End If
8 Z$ `4 L5 e  R/ [: {! e3 G& R
1 A4 }2 i; \  i4 {. u    cindex = cindex + ColHead.Column - 1
2 Q1 O# U# P" B8 a2 ]2 wEnd If- B* T) A0 t& R# V; W- N- n! I  P
) n8 k4 B# l& B3 _2 b
' Return the cell value from Table9 B+ u+ u; z& D  H: M. ]
'debug.Print "Answer is in (R,C):  " & rindex, cindex3 ~# L- j' k8 _- o
ZZL = ActiveSheet.Cells(rindex, cindex)
; M% q% t6 a  R4 ~) i, ^'debug.Print "Answer is : " & ZZL
5 H6 B" Y! Z- Z4 bExit Function
1 x+ M1 L( B; q& ?6 Y& q0 z) S; e6 M  H9 W9 H2 }1 m
err_handler1:' R) K: W- }0 y) [" R( y' \* h# X! d; p
ZZL = "Error on range '" & rngname & "'"
" ?- J3 e' t8 Y( e. ~0 s
; G1 Q& v+ }, ^  qEnd Function
4 G# Q1 b5 H1 o. G
! G) f" |$ m! |- u6 ^6 L) o9 u* 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-6 14:44 , Processed in 0.057927 second(s), 14 queries , Gzip On.

Powered by Discuz! X3.5 Licensed

© 2001-2025 Discuz! Team.

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