11

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

私は何を間違っていますか?

4

6 に答える 6

33
If Sheets("Sheet1").OLEObjects("CheckBox1").Object.Value = True Then

ティムは正しいと思います。フォームコントロールがあります。そのためにはこれを使用する必要があります

If ActiveSheet.Shapes("Check Box 1").ControlFormat.Value = 1 Then
于 2012-08-16T16:14:12.003 に答える
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
于 2012-08-16T16:36:09.420 に答える
4

チェックボックスには、チェックボックスの状態を表す True/False を含むリンクされたセルがあります。チェックボックスである埋め込みオブジェクトの値よりも、このセルの値を参照する方がはるかに簡単です。

手動: チェックボックスを右クリックして [フォーマット] を選択し、[リンクされたセル] ボックスをクリックして、チェックボックスの値を含むセルを選択します。

コード内:

Set cbTime = ActiveSheet.CheckBoxes.Add(100, 100, 50, 15)
With cbTime
    .Value = xlOff ' = unchecked  xlOn = checked
    .LinkedCell = "$A$1"
End With
于 2012-10-27T14:50:21.887 に答える
3
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
于 2014-01-16T14:33:57.983 に答える
0

Worksheets("SheetName").CB_Checkboxname.value 追加のオブジェクトに関係なく、直接使用してチェックボックスに直接アクセスできることがわかりました。

于 2018-06-25T09:51:23.010 に答える