使用 excel VBA 的复选框的可变标题
Variable caption on checkboxes using excel VBA
我有以下代码根据记录集中的值创建复选框,记录集可以是 A、B、C 或 D。
我希望复选框标题显示这些字母的含义。例如,A= 优秀,B= 非常好,C= 好,D= 差。
我在 sheet 中有这些值并执行 vlookup 以获取相应的名称,因此代码当前正在执行所需的操作,但是有没有办法在 sheet 中不包含这些值,也许在变量或隐藏 sheet?
If Not rst.EOF And Not rst.BOF Then
i = 0
Do
With MultiPage1.Pages(2).Controls.Add("Forms.Checkbox.1", "Checkbox" & i)
.Top = yPos
.Left = 7
.Caption = Application.WorksheetFunction.VLookup(rst![Perspect], ThisWorkbook.Sheets("Sheet1").Range("b26:c30"), 2, False)
.Width = 450
.Height = 24
.WordWrap = True
.Value = False
yPos = yPos + 17
.Tag = rst![Perspect]
i = i + 1
rst.MoveNext
End With
Loop Until rst.EOF
rst.Close
End If
这样的事情可能会有所帮助
Function getVal(strLetter As String)
Dim a() As Variant
Dim b() As Variant
a = Array("Gold", "Silver", "Bronze")
b = Array("A", "B", "C")
Debug.Print Application.WorksheetFunction.Index( _
a, 1, _
Application.WorksheetFunction.Match(strLetter, b, 0))
End Function
这样调用
getVal("B")
获得银牌,getVal("C")
获得铜牌等
最好的选择是将其放入您的记录集中。如果你没有那个选项,那么我认为这是最干净的方式:
将.Caption = ...
替换为
.Caption = GradeCaption(rst![Perspect])
然后创建您的函数:
Function GradeCaption(Grade As String) As String
Select Case Grade
Case "A"
GradeCaption = "Excellent"
Case "B"
GradeCaption = "Very Good"
Case "C"
GradeCaption = "Good"
Case "D"
GradeCaption = "Bad"
End Select
End Function
实现目标的可能性有很多种:sheet、隐藏的sheet、数组、字典...附加了 select 案例的可能性:
Sub Call_GetDescr()
MsgBox "A: " & getDescription("A")
MsgBox "B: " & getDescription("B")
MsgBox "C: " & getDescription("C")
MsgBox "D: " & getDescription("D")
MsgBox "X: " & getDescription("X")
End Sub
Function getDescription(inputStr As String) As String
Dim result As String
Select Case inputStr
Case "A"
result = "very good"
Case "B"
result = "good"
Case "C"
result = "sufficient"
Case "D"
result = "bad"
Case Else
result = "not defined"
End Select
getDescription = result
End Function
对于这些类型的问题,我一直是字典的粉丝。
我可以建议吗
Sub GradeDictionary()
Dim dict As Object, key, val
Set dict = CreateObject("Scripting.Dictionary")
key = "A": val = "Excellent"
dict.Add key, val
key = "B": val = "Very Good"
dict.Add key, val
key = "C": val = "Good"
dict.Add key, val
key = "D": val = "Bad"
dict.Add key, val
For Each k In dict.Keys
' Print key and value
Debug.Print k, dict(k)
Next
End Sub
我有以下代码根据记录集中的值创建复选框,记录集可以是 A、B、C 或 D。 我希望复选框标题显示这些字母的含义。例如,A= 优秀,B= 非常好,C= 好,D= 差。 我在 sheet 中有这些值并执行 vlookup 以获取相应的名称,因此代码当前正在执行所需的操作,但是有没有办法在 sheet 中不包含这些值,也许在变量或隐藏 sheet?
If Not rst.EOF And Not rst.BOF Then
i = 0
Do
With MultiPage1.Pages(2).Controls.Add("Forms.Checkbox.1", "Checkbox" & i)
.Top = yPos
.Left = 7
.Caption = Application.WorksheetFunction.VLookup(rst![Perspect], ThisWorkbook.Sheets("Sheet1").Range("b26:c30"), 2, False)
.Width = 450
.Height = 24
.WordWrap = True
.Value = False
yPos = yPos + 17
.Tag = rst![Perspect]
i = i + 1
rst.MoveNext
End With
Loop Until rst.EOF
rst.Close
End If
这样的事情可能会有所帮助
Function getVal(strLetter As String)
Dim a() As Variant
Dim b() As Variant
a = Array("Gold", "Silver", "Bronze")
b = Array("A", "B", "C")
Debug.Print Application.WorksheetFunction.Index( _
a, 1, _
Application.WorksheetFunction.Match(strLetter, b, 0))
End Function
这样调用
getVal("B")
获得银牌,getVal("C")
获得铜牌等
最好的选择是将其放入您的记录集中。如果你没有那个选项,那么我认为这是最干净的方式:
将.Caption = ...
替换为
.Caption = GradeCaption(rst![Perspect])
然后创建您的函数:
Function GradeCaption(Grade As String) As String
Select Case Grade
Case "A"
GradeCaption = "Excellent"
Case "B"
GradeCaption = "Very Good"
Case "C"
GradeCaption = "Good"
Case "D"
GradeCaption = "Bad"
End Select
End Function
实现目标的可能性有很多种:sheet、隐藏的sheet、数组、字典...附加了 select 案例的可能性:
Sub Call_GetDescr()
MsgBox "A: " & getDescription("A")
MsgBox "B: " & getDescription("B")
MsgBox "C: " & getDescription("C")
MsgBox "D: " & getDescription("D")
MsgBox "X: " & getDescription("X")
End Sub
Function getDescription(inputStr As String) As String
Dim result As String
Select Case inputStr
Case "A"
result = "very good"
Case "B"
result = "good"
Case "C"
result = "sufficient"
Case "D"
result = "bad"
Case Else
result = "not defined"
End Select
getDescription = result
End Function
对于这些类型的问题,我一直是字典的粉丝。 我可以建议吗
Sub GradeDictionary()
Dim dict As Object, key, val
Set dict = CreateObject("Scripting.Dictionary")
key = "A": val = "Excellent"
dict.Add key, val
key = "B": val = "Very Good"
dict.Add key, val
key = "C": val = "Good"
dict.Add key, val
key = "D": val = "Bad"
dict.Add key, val
For Each k In dict.Keys
' Print key and value
Debug.Print k, dict(k)
Next
End Sub