2012-08-16 3 views
9

IF-THEN 문에 확인란의 값을 사용해야합니다. 사용자가 확인한 내용에 따라 사물을 계산하는 방식이 바뀝니다. 그러나 확인란 값을 사용하는 방법이나이를 탐지하는 방법을 알 수 없습니다. 다음 코드는 내가 지금까지 가지고 있습니다 :Excel VBA 2010의 IF-THEN 문에서 확인란을 사용하려면 어떻게해야합니까?

Private Sub Workbook_Open() 
    Range("E1:F7,A1:A4,B1:B4,C1:C3").Select 
    With Selection.Borders(xlEdgeLeft) 
     .LineStyle = xlContinuous 
     .ColorIndex = 0 
     .TintAndShade = 0 
     .Weight = xlThin 
    End With 
    With Selection.Borders(xlEdgeTop) 
     .LineStyle = xlContinuous 
     .ColorIndex = 0 
     .TintAndShade = 0 
     .Weight = xlThin 
    End With 
    With Selection.Borders(xlEdgeBottom) 
     .LineStyle = xlContinuous 
     .ColorIndex = 0 
     .TintAndShade = 0 
     .Weight = xlThin 
    End With 
    With Selection.Borders(xlEdgeRight) 
     .LineStyle = xlContinuous 
     .ColorIndex = 0 
     .TintAndShade = 0 
     .Weight = xlThin 
    End With 
    With Selection.Borders(xlInsideVertical) 
     .LineStyle = xlContinuous 
     .ColorIndex = 0 
     .TintAndShade = 0 
     .Weight = xlThin 
    End With 
    With Selection.Borders(xlInsideHorizontal) 
     .LineStyle = xlContinuous 
     .ColorIndex = 0 
     .TintAndShade = 0 
     .Weight = xlThin 
    End With 
    Range("A1").Select 
    Range("A1") = "Time" 
    Range("B1") = "Specimen Shape" 
    Range("C1") = "Data Type" 
    Range("A1:C1").Font.Bold = True 
    Range("E1") = "Owner" 
    Range("E2") = "Experiment Date" 
    Range("E3") = "Specimen ID" 
    Range("E4") = "Contaminant" 
    Range("E5") = "Leachant" 
    Range("E6") = "Temperature" 
    Range("E7") = "Regression Title" 
    Range("E1:E7").Font.Bold = True 
    Columns("A:E").EntireColumn.EntireColumn.Autofit 
    'Formatting Column A 
    Columns("A").EntireColumn.ColumnWidth = 9.71 
    ActiveSheet.CheckBoxes.Add(4, 14.5, 72, 17.25).Select 
    Selection.Characters.Text = "Days" 
    Range("A6").Select 
    ActiveSheet.CheckBoxes.Add(4, 30.5, 73.5, 17.25).Select 
    Selection.Characters.Text = "Hours" 
    ActiveSheet.CheckBoxes.Add(4, 45.75, 52.5, 17.25).Select 
    Selection.Characters.Text = "Minutes" 
    'Formatting Column B 
    ActiveSheet.CheckBoxes.Add(58, 14.5, 72, 17.25).Select 
    Selection.Characters.Text = "Cylinder" 
    ActiveSheet.CheckBoxes.Add(58, 30.5, 73.5, 17.25).Select 
    Selection.Characters.Text = "Wafer" 
    ActiveSheet.CheckBoxes.Add(58, 45.75, 52.5, 17.25).Select 
    Selection.Characters.Text = "Irregular" 
    'Formatting Column C 
    Columns("C").EntireColumn.ColumnWidth = 12.71 
    ActiveSheet.CheckBoxes.Add(140.5, 14.5, 72, 17.25).Select 
    Selection.Characters.Text = "Incremental" 
    ActiveSheet.CheckBoxes.Add(140.5, 30.5, 72, 17.25).Select 
    Selection.Characters.Text = "Cumulative" 
    Columns("F").EntireColumn.ColumnWidth = 60 
    Range("A1:C1").HorizontalAlignment = xlCenter 
    Range("F1").Select 
    Dim btn As Button 
    Dim rng As Range 
    With Worksheets("Sheet1") 
     Set rng = .Range("A9:C9") 
      Set btn = .Buttons.Add(rng.Left, rng.Top, rng.Width, rng.Height) 
     With btn 
      .Caption = "After making your selections above, click this button to continue." 
      .AutoSize = True 
      .OnAction = "DataInput" 
     End With 
    End With 
End Sub 

는 내가하고 싶은 것은, 단지 테스트로 "시간"체크 박스를 체크 한 후 버튼을 계속 누르면, 내가 원하는 IF-THEN 문을 사용하여 "YAY"와 같은 것을 말하십시오. '시간'확인란을 선택하지 않고 계속을 누르면 'AWW ...'라고 말하고 싶습니다.

이것은 내가 그런 일을하려고 시도한 것이며 작동하지 않습니다.

Sub DataInput() 
    If ActiveSheet.Shapes.Range(Array("Check Box 1")).Value = True Then 
    MsgBox ("Yay") 
    Else: MsgBox ("Aww") 
    End If 
End Sub 

내가 뭘 잘못하고 있니?

답변

26

나는 Tim이 옳다고 생각한다. 당신은 Form Control을 가지고 있습니다. 이를 위해이

If ActiveSheet.Shapes("Check Box 1").ControlFormat.Value = 1 Then 
+0

, "워크 시트 클래스의 OLEObjects 속성을 가져올 수 없습니다." ? – TheTreeMan

+0

1) "Sheet1"을 관련 시트 이름으로 대체 했습니까? 2) "CheckBox1"을 관련 체크 박스 이름으로 대체 했습니까? –

+0

예, 여전히 작동하지 않습니다 : \ 매크로를 기록하고 해당 확인란을 삭제했을 때 매크로는 다음과 같습니다 :'ActiveSheet.Shapes.Range (Array ("Check Box 1")) 선택하십시오. – TheTreeMan

5
당신이 뭔가를 시도 할 수 있습니다

....

Dim cbTime 

Set cbTime = ActiveSheet.CheckBoxes.Add(100, 100, 50, 15) 
With cbTime 
    .Name = "cbTime" 
    .Characters.Text = "Time" 
End With 

If ActiveSheet.CheckBoxes("cbTime").Value = 1 Then 'or just cbTime.Value 
    'checked 
Else 
    'unchecked 
End If 
4

체크 박스를 사용할 필요가있는 체크 박스의 상태를 나타내는 참/거짓을 포함하는 연결된 셀, . 이 셀의 값을 체크 상자 인 임베디드 오브젝트의 값보다 훨씬 쉽게 참조 할 수 있습니다.

수동 : 확인란을 마우스 오른쪽 버튼으로 클릭하고 형식을 선택한 다음 연결된 셀 상자를 클릭하고 확인란 값을 포함 할 셀을 선택하십시오. 코드에서

: 나는 오류를 가지고

Set cbTime = ActiveSheet.CheckBoxes.Add(100, 100, 50, 15) 
With cbTime 
    .Value = xlOff ' = unchecked xlOn = checked 
    .LinkedCell = "$A$1" 
End With 
2
Sub Datainput() 
'Checkbox values are 0 (false), 1 (true), 2 (changed or grey) 
    If activesheet.CheckBoxes("Check Box 1").value = 1 then 
     Msgbox ("Yay") 
     Else: Msgbox("Aww") 
    End if 
End Sub 
관련 문제