【问题标题】:Activation of sheets based on selection of checkbox in userform基于用户表单中复选框的选择激活工作表
【发布时间】:2014-07-15 07:32:22
【问题描述】:

我的用户表单中有复选框,并且基于从复选框中选择的 ID,我想为我的工作簿中的特定用户激活工作表。我遇到了以下代码的某些部分,但它无法正常工作。

Option Explicit
Private Sub Add_Click()
 Dim ctrl As Control
 For Each ctrl In UserForm1.Controls
If TypeName(ctrl) = "CheckBox" Then
TransferValues ctrl
End If
Next
End Sub

Sub TransferValues(cb As MSForms.CheckBox)
Dim ws As Worksheet
Dim emptyRow As Long

If cb.Value = True Then
   'Define the worksheet based on the CheckBox.Name property:
    Set ws = Sheets(Left(cb.Name, 1))
    emptyRow = WorksheetFunction.CountA(ws.Range("F:F")) + 1
       With ws
            If Trim(Me.ComboBox3.Value) = "" Or Trim(Me.ComboBox6.Value) = "" Then
            MsgBox ("Please enter text in all fields")
            Exit Sub
            End If
                If WorksheetFunction.CountIf(ws.Range("F:F"), ComboBox3.Value) = 0 Or WorksheetFunction.CountIf(ws.Range("G:G"), ComboBox6.Value) = 0 Then
                    Cells(emptyRow, 6).Value = ComboBox3.Value
                    Cells(emptyRow, 7).Value = ComboBox6.Value
                    Cells(emptyRow, 8).Value = TextBox1.Value
            Else
                MsgBox ("Warning:Duplicate Entries found. Please update the existing entries")
        End If
       End With
End If

结束子

【问题讨论】:

    标签: excel combobox userform vba


    【解决方案1】:

    自己找到解决方案。如果有人遇到此类问题,请使用以下代码。

    Private Sub CommandButton1_Click()
    Dim ctrl As Control
    For Each ctrl In Userform1.Controls
      If TypeName(ctrl) = "CheckBox" Then
      TransferValues ctrl
      End If
      Next
    End Sub
    
    Sub TransferValues(cb As MSForms.CheckBox)
    Dim ws As Worksheet
    Dim emptyRow As Long
    'Dim ID As String
    
    If cb.Value = True Then
        Set ws = Sheets(Left(cb.Caption, 6))
            If Trim(Me.ComboBox3.Value) = "" Or Trim(Me.ComboBox6.Value) = "" Then
            MsgBox ("Please Enter the text in All Fields")
            End If
        emptyRow = WorksheetFunction.CountA(ws.Range("F:F")) + 1
          With ws
            If WorksheetFunction.CountIf(ws.Range("F:F"), ComboBox3.Value) = 0 Or WorksheetFunction.CountIf(ws.Range("G:G"), ComboBox6.Value) = 0 Then
    
            .Cells(emptyRow, 6).Value = ComboBox3.Value
            .Cells(emptyRow, 7).Value = ComboBox6.Value
            .Cells(emptyRow, 8).Value = TextBox1.Value
            Else
            MsgBox ("Warning:Duplicate Entries Found. Please edit existing entries")
            End If
    
          End With
    End If
    
    End Sub
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-19
      • 2016-02-28
      • 1970-01-01
      • 1970-01-01
      • 2012-07-29
      相关资源
      最近更新 更多