VBA实战技巧33:动态用户窗体图像显示

2021-09-22 10:15:32 浏览数 (1)

学习Excel技术,关注微信公众号:

excelperfect

本文所展示的技巧主要是根据工作表中的数据,在用户窗体的组合框中选择项目后,显示该项目的说明和相应的图像。用户窗体的大小会根据图像的大小进行调节,如下图1所示。

图1

所使用的工作表数据如下图2所示。

图2

用户窗体界面如下图3所示,一个组合框、一个文本框和一个图像控件。

图3

用户窗体模块代码如下:

代码语言:javascript复制
Private Sub ComboBox1_Change()
    Dim img As Object
    Dim ad As String
    Dim f As Double
    Dim zf As Double
   
    Me.TextBox1 =Evaluate("=VLOOKUP(" & """" &Me.ComboBox1.Value & """" & _
        ",A2:C" &Split(Sheets("Sheet1").[A2].CurrentRegion.Address, "$")(4)& ",2)")
    ad = Evaluate("=VlOOKUP(" &"""" & Me.ComboBox1.Value &"""" & _
        ",A2:C" &Split(Sheets("Sheet1").[A2].CurrentRegion.Address, "$")(4)& ",3)")
    Set img = Me.Image1
    img.Picture = LoadPicture(ad)
    With Me
        With img
            .Left = 0
            .Top = 0
            .PictureAlignment =fmPictureAlignmentTopLeft
            .PictureSizeMode =fmPictureSizeModeClip
            .AutoSize = True
        End With
        .Width = img.Width
        Do While .InsideWidth <= img.Width
            .Width = .Width   3
        Loop
        .Height = img.Height
        Do While .InsideHeight <= img.Height
            .Height = .Height   3
        Loop
        .Height = .Height   .ComboBox1.Height  .TextBox1.Height   2
        .ComboBox1.Left = 0
       .ComboBox1.Top = img.Height   1
        .TextBox1.Left = 0
        .TextBox1.Top = img.Height  .ComboBox1.Height   1
        If .Height > 500 Then
            f = .Height / .Width
            zf = .Height / 400
            .Caption = ad & " (调整大小)"
            .Height = .Height / zf
            .Width = .Height / f
            .Zoom = .Zoom / zf
        End If
    End With
End Sub
 
Private Sub UserForm_Initialize()
    ComboBox1.List = [Sheet1!A2:A9].Value
    Me.ComboBox1.Height = 30
    Me.ComboBox1.Width = 80
    Me.ComboBox1.Font.Size = 20
    Me.TextBox1.Height = 30
    Me.TextBox1.Width = 100
    Me.TextBox1.Font.Size = 20
    Me.Caption = "类别"
    Me.BorderStyle = fmBorderStyleSingle
    Me.BorderColor = RGB(200, 50, 10)
End Sub

注:本文整理自mrexcel.com,供大家参考。

undefined

欢迎在下面留言,完善本文内容,让更多的人学到更完美的知识。

0 人点赞