我有 4 個選項按鈕,它們的標題設定為不同的單元格。但是,有時該單元格值不包含任何文本,如果是這種情況,我想從用戶表單中隱藏選項按鈕。但是即使標題包含文本,我的代碼也會隱藏選項按鈕。我確信這很簡單,但我無法解決它。
Call ifBlank
OptionButton1.Caption = qRange1.Value
OptionButton2.Caption = qRange2.Value
OptionButton3.Caption = qRange3.Value
OptionButton4.Caption = qRange4.Value
Sub ifBlank()
If OptionButton3.Caption = "" Then
OptionButton3.Visible = False
If OptionButton4.Caption = "" Then
OptionButton4.Visible = False
End If
End If
uj5u.com熱心網友回復:
a)在設定字幕ifBlank 之后呼叫,而不是之前呼叫。
b)你可以簡單地寫
Sub ifBlank()
OptionButton1.Visible = (OptionButton1.Caption <> "")
OptionButton2.Visible = (OptionButton2.Caption <> "")
OptionButton3.Visible = (OptionButton3.Caption <> "")
OptionButton4.Visible = (OptionButton4.Caption <> "")
End If
uj5u.com熱心網友回復:
您可以通過在集合中整理您的選項按鈕和 QRange 來讓您的喜歡更輕松。設定用戶表單時,您的代碼會更廣泛,但稍后代碼會變得更簡單。
Option Explicit
Private Type State
Buttons As Collection
QRanges As Collection
End Type
Private s As State
Private Sub UserForm_Initialize()
Set s.Buttons = New Collection
With s.Buttons
.Add OptionButton1
.Add OptionButton2
.Add OptionButton3
.Add OptionButton4
End With
Set s.QRanges = New Collection
With s.QRanges
.Add QRange1
.Add QRange2
.Add QRange3
.Add QRange4
End With
'other initialisation code
End Sub
Public Sub UpdateButtonCaptions()
Dim myIndex As Long
For myIndex = 1 To s.Buttons.Count
' the test for an QRange may need to be more rigourous
If s.QRanges(myIndex) = "" Then
s.Buttons(myIndex).Visible = False
Else
s.Buttons(myIndex).Visible = True
s.Buttons(myIndex) = s.QRanges(myIndex).Value
End If
Next
End Sub
我不是普通的 excel/form 用戶,所以您可能需要調整上面的一些代碼。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/456763.html
