找回密码
 注册会员

QQ登录

只需一步,快速开始

搜索
查看: 1739|回复: 3

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

[复制链接]
发表于 2022-6-6 18:03:11 | 显示全部楼层 |阅读模式
Option Compare Text/ ]! k  G# `0 v: ~8 p3 r. a1 H7 @/ k
9 T7 `/ ]) ]7 C, l% {9 E4 v2 K
Public Function GSXS(Ref)( |3 n& k3 S$ v. Z

: d7 n* A) R  @. {  G& }! p    GSXS = Ref.Formula
4 y# u( D4 w0 K
/ h2 y& a5 F% E# n) GEnd Function
! ?1 t4 V( F' n& n  @. _& i4 D- C. E$ _  @( J/ e
Public Function ZZL(RowHead, ColHead, Dummy)
" J$ D3 ?) ^8 ^/ }& J' @0 r/ ^- |" T* m# ?0 |" R# s2 L
Dim Values(20) As Variant
2 r2 L. _3 r; lDim PrevData(20) As Variant
! T" U4 j4 D3 P$ s' kDim LE(20) As Integer
" m# S2 W: z6 ~5 g' R) K( v7 G$ Z& Y7 i( G7 g+ v
On Error GoTo err_handler1
. z% ]* E* x! _' Do the vertical selection from rows1 T( N2 n* ]9 ?
If RowHead.Rows.Count = 1 Then5 t; F/ e/ c  Q6 J3 I; b  l
    rindex = RowHead.Row    ' first argument is any cell on the row of possible values. n. \. y1 ], i& M' P# P' v; L
Else
0 j% t+ }- P2 d* Y1 w    ' Store the values to be compared with each column. R$ b4 @( X* @6 |
    For ii = 1 To RowHead.Columns.Count) S9 e: W- Q( G, @5 F) b& C' A; I
        rngname = RowHead.Cells(1, ii)6 G. ~2 l* g' W& I3 {( W$ [
        LE(ii) = InStr(rngname, "<=")
; v- f, m$ A$ Q. Y- y: S# v8 e7 d9 J& E. ~        If LE(ii) > 0 Then2 F, {* b8 n( z# {  s* B, H3 W
            rngname = Mid(rngname, 1, LE(ii) - 1)
- Z6 X. l: V% M  w6 O/ N        End If8 v4 n2 N- g2 [- L; F: l
        Values(ii) = Range(rngname). h6 C# U4 t7 T7 l" Z& a! }/ o) q9 h
        'debug.Print "Variable:" & rngname & " is:" & Values(ii)! i8 f+ L2 o3 t# E
        PrevData(ii) = ""   ' initialise) a. U3 ^" u: j/ G: o
    Next ii
' O3 X# z; W3 M: F& k) D3 C$ ]/ y6 x8 U1 f. Z  J/ k6 S- K/ b# t7 _# Y
    rindex = 2- {/ O) I% f4 M  w# H) \
    'debug.Print RowHead.Columns.Count) g( d5 r% z. Z
    Match = False% n, u; M' t2 C% c/ \1 K
    For r = rindex To RowHead.Rows.Count1 i( l0 ~& |- g
        For c = 1 To RowHead.Columns.Count   ' for each dimension3 V  h* D8 n9 S6 B
            data = RowHead.Cells(r, c)
* ~$ Y: P% O0 W* S/ m( t            If data = "" Then8 m* r1 z# ?5 a3 `* \* e+ w- a# S
                'debug.Print "Empty cell found: using " & PrevData(c)
% L1 P$ c! }9 V3 m% [+ c- b                ' use the last valid cell in this column
+ M- X, \5 ~) b6 \1 o/ \                ' (this is to handle merged cells)
+ V% a4 I7 q+ c" h1 p                data = PrevData(c)
8 w/ Z8 [$ T. B/ d) S8 h% h            End If4 H# E6 y% I' s8 }# B, O
            'debug.Print "data:" & data
& |+ n  b. r  @            PrevData(c) = data ' save for use by empty cells0 |( _' t* M0 P/ O
            If data = Values(c) Or (data > Values(c) And LE(c) > 0) Or data = "*" Then
3 Q  r- Z' t4 l                If c = RowHead.Columns.Count Then   ' All columns match - It's a go" p5 c; d* `) k, B- y' z
                    Match = True
6 w4 }: _( v& b4 q4 W$ p                End If
) V( E  Q) V* P/ l9 T  ^7 p; b            Else    ' This column doesn't match - go to the next row
+ N/ @) H& @) V" Z7 [                Match = False
# c# i" p% ~+ `; e' X                Exit For
% w4 {: [9 B" r! b            End If* F: T! q) }2 Q7 t, o. I4 p
        Next c2 F- i1 l0 S& r" j/ r; D4 A( g
        If Match = True Then    ' Don't search any more rows2 D: H$ @6 S/ i) A) K
            rindex = r
) J# i) i" W1 U% D; ?5 h            Exit For
+ H  A) X' |% ~1 ?0 T        End If
6 b4 P7 t/ S" m    Next r
1 Y  [! O' Q3 s* b( g
+ _. M* s( I3 Q8 v8 b5 [    If Match = False Then   ' Didn't find a matching set of values
4 e; S2 V' L4 ~6 i' j        ZZL = "No match for rows"
, n( [2 k) N6 g& f& z1 f1 L5 I: ?/ D        Exit Function8 I" E* W) L+ B' y0 M
    End If
4 t" Q. {: L! M3 K+ Z  E, \: ?- A. X
  E/ j. h3 M, o- g8 f    rindex = rindex + RowHead.Row - 1   ' make absolute index' s3 |  T  X. t
End If0 G- F: s" ]) g: j1 e7 V- M

' T6 e0 N; c% g; O' Do the horizontal selection from columns6 n+ k$ O; X  s9 H3 A# [$ J
If ColHead.Columns.Count = 1 Then1 N. s$ j) @$ h4 Z7 Y" g
    cindex = ColHead.Column" S6 |8 ]. k* a$ X. ~+ d
Else8 Y/ n8 @1 q( u; X" I, n6 Q
    ' Store the values to be compared with each row of the header- S3 Z" I9 H6 M4 G6 \3 c
    For ii = 1 To ColHead.Rows.Count& x& C* i7 k) _: c' r
        rngname = ColHead.Cells(ii, 1)3 w3 z$ O0 s  t) H' Q
        LE(ii) = InStr(rngname, "<=")
0 y' P0 v. Q' i& t9 E1 d, W        If LE(ii) > 0 Then) |2 s( l* h5 O9 y+ @' e; b
            rngname = Mid(rngname, 1, LE(ii) - 1); p. J: N, q) m( \0 l1 N* x1 L! T9 c2 \
        End If! y( I; s1 d. `* b  n4 _* {$ L, C
        Values(ii) = Range(rngname)
9 q- r1 Y: M0 E        'debug.Print "Variable:" & rngname & " is:" & Values(ii)- |! Z/ V3 T# o5 s
        PrevData(ii) = ""   ' initialise
# j/ \& `9 Z" Q# c# |. X% e6 c/ H    Next ii
3 d4 T6 v) T0 n1 W, f; D6 d. x9 n% r' F. D$ D
    cindex = 29 [/ Q& T( c$ \. ^
    'debug.Print ColHead.Columns.Count
5 u) g; H  D& W    Match = False. R( j! v! o* \( T  E6 r( N
    For c = cindex To ColHead.Columns.Count
9 D; }& X, C; _, }/ U        For r = 1 To ColHead.Rows.Count   ' for each dimension4 }# F& I; W1 M* {2 P
            data = ColHead.Cells(r, c): s! L! b' L7 I7 X7 l( `
            If data = "" Then* X4 \6 m  b! ~0 U% r
                'debug.Print "Empty cell found: using " & PrevData(r)
; ?2 h& R1 e: Y' b$ h5 {% a                ' use the last valid cell on this row
+ B  g- C; S% [                ' (this is to handle merged cells)1 g, m+ ~2 Y; @, W: i3 S% W; m
                data = PrevData(r)' X) p) M1 m' H$ a2 e* Q& [
            End If# B) }6 X7 N7 F6 q9 D
            'debug.Print "data:" & data
/ s! f3 v$ b; O/ [2 A            PrevData(r) = data ' save for use by empty cells7 {7 a5 q; l! Z( w7 |
            If data = Values(r) Or (data > Values(r) And LE(r) > 0) Or data = "*" Then
) e& |+ N  q% T7 I6 z' c% C) f                If r = ColHead.Rows.Count Then   ' All rows match - It's a go
$ E( Q$ J3 r7 V8 u' C                    Match = True
# E0 f& D5 `* h8 y" N1 L% M/ a                End If4 b' z* d$ p) v* Q
            Else    ' This row doesn't match - go to the next column
4 z6 ^7 g4 j$ Y) F1 N  J) s, [                Match = False3 \* p! `# _2 @+ r
                Exit For
4 J% l5 }8 K  X            End If) l- T1 a' e4 O) E. F- {% D% S  p
        Next r
( U, r+ x" O" a7 d  Y/ K7 ?        If Match = True Then    ' Don't search any more columns
! @2 R$ B( d& U  m            cindex = c
" Q: y/ ^, f1 [            Exit For2 I. R8 t4 V8 A1 e
        End If
- p& Y, X9 M& d    Next c
/ j2 b$ h1 w8 [* p; j. m
; t& Q- K- {8 A8 X    If Match = False Then   ' Didn't find a matching set of values9 h7 b/ \/ x4 g4 S2 y; }* A
        ZZL = "No match for columns"6 [/ P3 q$ s: V7 f  U9 B) L% m: E" X2 V
        Exit Function
3 t# Y) K6 y% T! Y    End If
& `+ I! g5 l1 ?/ ~: A2 r& B1 n/ V! F( E2 c% S
    cindex = cindex + ColHead.Column - 1
9 M$ j: ]( z1 J4 dEnd If! [2 I: ~5 C) b3 m9 Z0 Q' T

$ G" @9 x5 l7 A0 @7 z5 @0 ?; Q) E' Return the cell value from Table1 W: U0 c4 Z, p3 [: Q4 D
'debug.Print "Answer is in (R,C):  " & rindex, cindex# _) D4 K, C" Y, k
ZZL = ActiveSheet.Cells(rindex, cindex)
: i- L. @- `# c: `'debug.Print "Answer is : " & ZZL% c5 I5 {4 S+ G# q
Exit Function
. P0 R1 ^' a9 W( E
# P  m  p8 F4 q- [0 ?  ierr_handler1:
9 F- D5 c$ Y' N+ _- o" FZZL = "Error on range '" & rngname & "'"; T! s4 F, Z% X+ Z, ?% w& I; ?0 J5 @: j6 n

0 G1 a2 C8 r4 G+ E& k3 L. PEnd Function" |! |. N: w* X8 L0 ~$ C- u
/ G& J6 z3 E1 W+ [! D
回复

使用道具 举报

 楼主| 发表于 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-10-21 13:45 , Processed in 0.065089 second(s), 14 queries , Gzip On.

Powered by Discuz! X3.5 Licensed

© 2001-2025 Discuz! Team.

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