zoukankan      html  css  js  c++  java
  • VBA-使用ActiveX控件

    Active控件的在设计模式下才能更改,进行操作:按钮主要用到的属性有 caption "按钮什么名字";visible“按钮是否可见”  enable“按钮是否可编辑” 这三个属性值,其余都比较简单常用

    Sub test()
    Sheet1.CommandButton1.Caption = "结束" '更改名字
    Range("a1") = Sheet1.CommandButton1.Caption '将名字赋值给a1
    Sheet1.CommandButton1.Top = Sheet1.CommandButton1.Top + Sheet1.CommandButton1.Height '移动
    End Sub

      针对commandbutton 按钮主要使用两个事件 click 和 mousemove

    Private Sub CommandButton1_Click()
    Range("e3") = 8
    End Sub
    --------------------------------------
    Private Sub CommandButton1_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
    MsgBox "你指我干嘛"
    End Sub

    标签控件label:caption主要决定里面写什么内容 其余 enable,visible作用与按钮相同:主要是用来看的不是用来写的,如果需要用户写那就需要文本框

                      文本框

    Sub test()
    Sheet1.Label1.Caption = "完美"
    End Sub

    选择按钮OptionButton  这个控件比其他多了两个  GroupName:组名 (这样同样的组名按钮只能选择一个);Value:返回值

            

    Sub test()
    With Sheet1
        If .OptionButton1.Value = True Then
            MsgBox "你选择的是男"
        ElseIf .OptionButton2.Value = True Then
            MsgBox "你选择的是女"
        Else
            MsgBox "你没有选择"
        End If
    End With
    End Sub

      它也有自己的事件 主要是click事件

    Private Sub OptionButton1_Click()
    MsgBox ""
    End Sub

    微调按钮事件 :没有caption属性,但是有其他,value,min,max 主要属性

    Sub xieru()
    Range("a1") = Sheet1.SpinButton1.Value
    End Sub             '只有在运行的时候才会有值
    Private Sub SpinButton1_Change()
    Call xieru
    End Sub  '使用自己的时间 再点击按钮,值就直接改变了   改变值的范围是 min 到 max

    几个控件联合制作一个考试系统

     

    Sub xieru()
    Dim i As Integer
    i = Sheet2.SpinButton1.Value
    
    With Sheet2
        '清空题目
        .OptionButton1.Value = False
        .OptionButton2.Value = False
        .OptionButton3.Value = False
        .OptionButton4.Value = False
        '写入题目
        .Label2.Caption = i
        .Label3.Caption = Sheet3.Range("a" & i + 1)
        .Label4.Caption = Sheet3.Range("b" & i + 1)
        .Label5.Caption = Sheet3.Range("c" & i + 1)
        .Label6.Caption = Sheet3.Range("d" & i + 1)
        .Label7.Caption = Sheet3.Range("e" & i + 1)
        '隐藏
        If .Label6.Caption = "" Then
            .OptionButton3.Visible = False
        Else
            .OptionButton3.Visible = True
        End If
        If .Label6.Caption = "" Then
            .OptionButton4.Visible = False
        Else
            .OptionButton4.Visible = True
        End If
        '返回之前的答案
        If Sheet3.Range("g" & i + 1) = "A" Then
            .OptionButton1.Value = True
        ElseIf Sheet3.Range("g" & i + 1) = "B" Then
            .OptionButton2.Value = True
        ElseIf Sheet3.Range("g" & i + 1) = "C" Then
            .OptionButton3.Value = True
        ElseIf Sheet3.Range("g" & i + 1) = "D" Then
            .OptionButton4.Value = True
        End If
    End With
    End Sub

    sheet2考试界面里的

    Private Sub OptionButton1_Click()
    Sheet3.Range("g" & Sheet2.SpinButton1.Value + 1) = "A"
    End Sub
    -------------------------------------
    Private Sub OptionButton2_Click()
    Sheet3.Range("g" & Sheet2.SpinButton1.Value + 1) = "B"
    End Sub
    -------------------------------------
    Private Sub OptionButton3_Click()
    Sheet3.Range("g" & Sheet2.SpinButton1.Value + 1) = "C"
    End Sub
    -------------------------------------
    Private Sub OptionButton4_Click()
    Sheet3.Range("g" & Sheet2.SpinButton1.Value + 1) = "D"
    End Sub
    -------------------------------------
    Private Sub SpinButton1_Change()
    Call xieru
    End Sub

    写入这个主要是想说明性下带参数的过程的用处:如下如果考试界面多出两个按钮,那么这个时候,使用带参数的过程能省很多代码

    最终版

    Sub xieru(i)
    With Sheet2
        '清空题目
        .OptionButton1.Value = False
        .OptionButton2.Value = False
        .OptionButton3.Value = False
        .OptionButton4.Value = False
        '写入题目
        .Label2.Caption = i
        .Label3.Caption = Sheet3.Range("a" & i + 1)
        .Label4.Caption = Sheet3.Range("b" & i + 1)
        .Label5.Caption = Sheet3.Range("c" & i + 1)
        .Label6.Caption = Sheet3.Range("d" & i + 1)
        .Label7.Caption = Sheet3.Range("e" & i + 1)
        '隐藏
        If .Label6.Caption = "" Then
            .OptionButton3.Visible = False
        Else
            .OptionButton3.Visible = True
        End If
        If .Label6.Caption = "" Then
            .OptionButton4.Visible = False
        Else
            .OptionButton4.Visible = True
        End If
        '返回之前的答案
        If Sheet3.Range("g" & i + 1) = "A" Then
            .OptionButton1.Value = True
        ElseIf Sheet3.Range("g" & i + 1) = "B" Then
            .OptionButton2.Value = True
        ElseIf Sheet3.Range("g" & i + 1) = "C" Then
            .OptionButton3.Value = True
        ElseIf Sheet3.Range("g" & i + 1) = "D" Then
            .OptionButton4.Value = True
        End If
    End With
    End Sub

    sheet2中的按钮

    Private Sub CommandButton1_Click()
    Call xieru(1)
    Sheet2.SpinButton1.Value = 1
    End Sub
    ----------------------------------
    Private Sub CommandButton2_Click()
    Call xieru(8)
    Sheet2.SpinButton1.Value = 8
    End Sub
    ----------------------------------
    Private Sub OptionButton1_Click()
    Sheet3.Range("g" & Sheet2.SpinButton1.Value + 1) = "A"
    End Sub
    ----------------------------------
    Private Sub OptionButton2_Click()
    Sheet3.Range("g" & Sheet2.SpinButton1.Value + 1) = "B"
    End Sub
    ----------------------------------
    Private Sub OptionButton3_Click()
    Sheet3.Range("g" & Sheet2.SpinButton1.Value + 1) = "C"
    End Sub
    ----------------------------------
    Private Sub OptionButton4_Click()
    Sheet3.Range("g" & Sheet2.SpinButton1.Value + 1) = "D"
    End Sub
    ----------------------------------
    Private Sub SpinButton1_Change()
    Call xieru(Sheet2.SpinButton1.Value)
    End Sub

     VBA-窗体与控件 :用到了在学习

  • 相关阅读:
    友链大集合
    雅礼学习10.7
    雅礼学习10.6
    雅礼学习10.5
    雅礼学习10.4
    雅礼学习10.3
    雅礼学习10.4
    雅礼学习10.2
    雅礼学习10.1
    如何让SublimeText3更好用
  • 原文地址:https://www.cnblogs.com/xiao-xuan-feng/p/12680440.html
Copyright © 2011-2022 走看看