2014-07-15 3 views
-1

사용자 서식에 확인란이 있고 확인란의 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 

최종 하위 자신에 의해

답변

0

찾을 솔루션입니다. 모든 사람이 직면하면 이러한 종류의 문제에 다음 코드를 사용하십시오.

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 
관련 문제