选项按钮编号循环
OptionButton numbering loop
希望你有一个可能是简单问题的优雅解决方案!
我正在使用 ActiveX 选项按钮,但由于 sheet 的设计方式,所以在作品sheet 中而不是在用户窗体或组框中。该代码作为选项按钮代码表单中的子项包含。
这段代码对我想做的事情的解释很清楚:
Public Sub SectionD_Click()
If OptionButton1.Value = True Then
ThisWorkbook.Sheets("Boolean").Range("B2").Value = 1
ElseIf OptionButton2.Value = True Then
ThisWorkbook.Sheets("Boolean").Range("B2").Value = 0
End If
If OptionButton3.Value = True Then
ThisWorkbook.Sheets("Boolean").Range("B3").Value = 1
ElseIf OptionButton4.Value = True Then
ThisWorkbook.Sheets("Boolean").Range("B3").Value = 0
End If
If OptionButton5.Value = True Then
ThisWorkbook.Sheets("Boolean").Range("B4").Value = 1
ElseIf OptionButton6.Value = True Then
ThisWorkbook.Sheets("Boolean").Range("B4").Value = 0
End If
End Sub
我想让 "OptionButton" 后面的数字使用简单的 'i = i + 2' 类型语句更改值,但似乎有些 VBA variable/expression/object 限制不会让我(抱歉,我是菜鸟,不确定正确的术语应该是什么)。
如果有人能在这里指出正确的方向,我将不胜感激!我必须查看 25 个左右的选项按钮对,我非常希望代码只有 5 行而不是一百多行做同样的事情!
5行?真的吗? :)
这是我能做的最好的事情了:
Option Explicit
Public Sub SectionD_Click()
With ThisWorkbook.Sheets("Boolean")
Call CheckValue(.OptionButton1, .OptionButton2, .Range("B2"))
Call CheckValue(.OptionButton3, .OptionButton4, .Range("B3"))
End With
End Sub
Sub CheckValue(btn1 As Object, btn2 As Object, my_cell As Range)
If btn1.Value Then
my_cell.Value = 1
ElseIf btn2.Value Then
my_cell = 0
End If
End Sub
我可以用一行代码来命名那首曲子!!
Public Sub SectionD_Click(): Dim i As Integer: Dim rw As Long: rw = 2: With Worksheets("Sheet1"): For i = 1 To 10 Step 2: If .OLEObjects("OptionButton" & i).Object.Value Then: Worksheets("Boolean").Cells(rw, "B").Value = 0: ElseIf .OLEObjects("OptionButton" & i).Object.Value Then: Worksheets("Boolean").Cells(rw, "B").Value = 0: End If: rw = rw + 1: Next: End With:End Sub:
不过我觉得16行更漂亮
Public Sub SectionD_Click()
Dim i As Integer
Dim rw As Long
rw = 2
With Worksheets("Sheet1")
For i = 1 To 10 Step 2
If .OLEObjects("OptionButton" & i).Object.Value Then
Worksheets("Boolean").Cells(rw, "B").Value = 0
ElseIf .OLEObjects("OptionButton" & i).Object.Value Then
Worksheets("Boolean").Cells(rw, "B").Value = 0
End If
rw = rw + 1
Next
End With
End Sub
希望你有一个可能是简单问题的优雅解决方案!
我正在使用 ActiveX 选项按钮,但由于 sheet 的设计方式,所以在作品sheet 中而不是在用户窗体或组框中。该代码作为选项按钮代码表单中的子项包含。
这段代码对我想做的事情的解释很清楚:
Public Sub SectionD_Click()
If OptionButton1.Value = True Then
ThisWorkbook.Sheets("Boolean").Range("B2").Value = 1
ElseIf OptionButton2.Value = True Then
ThisWorkbook.Sheets("Boolean").Range("B2").Value = 0
End If
If OptionButton3.Value = True Then
ThisWorkbook.Sheets("Boolean").Range("B3").Value = 1
ElseIf OptionButton4.Value = True Then
ThisWorkbook.Sheets("Boolean").Range("B3").Value = 0
End If
If OptionButton5.Value = True Then
ThisWorkbook.Sheets("Boolean").Range("B4").Value = 1
ElseIf OptionButton6.Value = True Then
ThisWorkbook.Sheets("Boolean").Range("B4").Value = 0
End If
End Sub
我想让 "OptionButton" 后面的数字使用简单的 'i = i + 2' 类型语句更改值,但似乎有些 VBA variable/expression/object 限制不会让我(抱歉,我是菜鸟,不确定正确的术语应该是什么)。
如果有人能在这里指出正确的方向,我将不胜感激!我必须查看 25 个左右的选项按钮对,我非常希望代码只有 5 行而不是一百多行做同样的事情!
5行?真的吗? :) 这是我能做的最好的事情了:
Option Explicit
Public Sub SectionD_Click()
With ThisWorkbook.Sheets("Boolean")
Call CheckValue(.OptionButton1, .OptionButton2, .Range("B2"))
Call CheckValue(.OptionButton3, .OptionButton4, .Range("B3"))
End With
End Sub
Sub CheckValue(btn1 As Object, btn2 As Object, my_cell As Range)
If btn1.Value Then
my_cell.Value = 1
ElseIf btn2.Value Then
my_cell = 0
End If
End Sub
我可以用一行代码来命名那首曲子!!
Public Sub SectionD_Click(): Dim i As Integer: Dim rw As Long: rw = 2: With Worksheets("Sheet1"): For i = 1 To 10 Step 2: If .OLEObjects("OptionButton" & i).Object.Value Then: Worksheets("Boolean").Cells(rw, "B").Value = 0: ElseIf .OLEObjects("OptionButton" & i).Object.Value Then: Worksheets("Boolean").Cells(rw, "B").Value = 0: End If: rw = rw + 1: Next: End With:End Sub:
不过我觉得16行更漂亮
Public Sub SectionD_Click()
Dim i As Integer
Dim rw As Long
rw = 2
With Worksheets("Sheet1")
For i = 1 To 10 Step 2
If .OLEObjects("OptionButton" & i).Object.Value Then
Worksheets("Boolean").Cells(rw, "B").Value = 0
ElseIf .OLEObjects("OptionButton" & i).Object.Value Then
Worksheets("Boolean").Cells(rw, "B").Value = 0
End If
rw = rw + 1
Next
End With
End Sub