【问题标题】:Do Calculations using Check Box checked values使用复选框选中的值进行计算
【发布时间】:2015-05-21 06:23:30
【问题描述】:

如果在 E 列中的文本显示“是”,我有一个代码可以将信息从一个工作表复制到另一个工作表。然后它在每一行中添加一个复选框,直到最后一行在以下列中包含数据。

我需要以下帮助:我想在另一张工作表中进行一些计算。如果选中了复选框,我想计算选中复选框的行数。在我有了这个计数之后,我想计算(Count 除以 总行数)的平均值,这个总行数包括已检查和未检查的行数。

这是复制信息并添加复选框的代码:

Sub Copysheet_And_CheckBox2() '<-- you run this
 CopySheet '<-- you call this first
 Dim i As Integer
Dim LastRow As Integer
Dim Search As String
Dim Column As Integer

  Sheets("Audit Items").Activate
   Sheets("Audit Items").Range("A1").Select
  'Sets an Autofilter to sort out only your Yes rows.
   Selection.AutoFilter
   'Change Field:=5 to the number of the column with your Y/N.
    Sheets("Audit Items").Range("$A$1:$G$2000").AutoFilter Field:=4,   Criteria1:="Yes"

'Finds the last row
LastRow = Sheets("Audit Items").Cells(Sheets("Audit Items").Rows.Count, "A").End(xlUp).Row

i = 1
'Change the 3 to the number of columns you got in Sheet2
 Do While i <= 3
  Search = Sheets("Form").Cells(1, i).Value
  Sheets("Audit Items").Activate
  'Update the Range to cover all your Columns in Sheet1.
  If IsError(Application.Match(Search, Sheets("Audit Items").Range("A1:G1"), 0))  Then
      'nothing
   Else
    Column = Application.Match(Search, Sheets("Audit Items").Range("A1:G1"), 0)
    Sheets("Audit Items").Cells(2, Column).Resize(LastRow, 1).Select
    Selection.Copy
    Sheets("Form").Activate
    Sheets("Form").Cells(2, i).Select
    ActiveSheet.Paste
    End If
    i = i + 1
    Loop

   CheckBox '<-- and this second into the same call
   Dim ToRow As Long
   Dim LastRow2 As Long
   Dim MyLeft As Double
   Dim MyTop As Double
   Dim MyHeight As Double
   Dim MyWidth As Double

    LastRow2 = Range("A65536").End(xlUp).Row
    For ToRow = 2 To LastRow2
    If Not IsEmpty(Cells(ToRow, "A")) Then
    Sheets("Form").Activate
        MyLeft = Cells(ToRow, "C").Left
        MyTop = Cells(ToRow, "C").Top
        MyHeight = Cells(ToRow, "C").Height
        MyWidth = MyHeight = Cells(ToRow, "C").Width
        '-
        ActiveSheet.CheckBoxes.Add(MyLeft, MyTop, MyWidth, MyHeight).Select
         With Selection
            .Caption = "Yes"
            .Value = xlOff
            .LinkedCell = "E" & ToRow
            .Display3DShading = False
            If CheckBoxes.Value = True Then
            CheckBoxes.Value = "1"

        End With
        End If
       Next

     End Sub

【问题讨论】:

    标签: vba excel


    【解决方案1】:

    现在您正在添加控件(即定义它们的属性,例如 x 和 y 坐标,它们应该在屏幕上可见等并将其添加到控件集合中),但是您没有创建任何事件处理程序(即“当控件被单击时,执行此操作”,“如果控件被禁用,执行此其他操作”等)

    在 StackOverflow 上 Assign event handlers to controls on user form created dynamically in VBA 有一个如何添加事件处理程序的示例

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-01-20
      • 2023-03-09
      • 2021-10-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-10
      • 1970-01-01
      相关资源
      最近更新 更多