VBA Excel宏清除所有组合框和一个复选框
问题描述:
我基本上试图将一个宏指定给一个命令按钮,该按钮清除某个单元格,还有一些组合框和活动工作表上的复选框,但无法让我的宏/命令成功运行。请帮忙!这将不胜感激。我的代码看起来像这样至今:VBA Excel宏清除所有组合框和一个复选框
Sub Macro4()
Sheet4.Select
Range("B3").Select
Selection.ClearContents
Range("B4").Select
Selection.ClearContents
Range("C13").Select
Selection.ClearContents
Range("C14").Select
Selection.ClearContents
Range("F8").Select
Selection.ClearContents
Range("J8").Select
Selection.ClearContents
Range("B23").Select
Selection.ClearContents
Range("J3").Select
Selection.ClearContents
Range("G4").Select
Selection.ClearContents
Sheet4.ComboBox4.Select
ComboBox4 = ""
End Sub
答
尝试
Sheet4.ComboBox4.Value = ""
另外,作为一个一般的提示,而不是使用像选择:
range("A1").select
selection.clearcontents
你可以结合这些线路,只是告诉它直接清除内容:
range("A1").clearcontents
答
我设置了一个名为的清单ClearRng,它由您想清除内容的所有单元组成。
此外,代码通过所有OLEObjects
在“Sheet4”循环,并检查它们是否ComboBox
类型,如果他们是它会清除它的内容。
(此代码旨在ActiveX Controls
,不User_Form Controls
)
Option Explicit
Sub Macro4()
Dim Sht As Worksheet
Dim ClearRng As Range
Dim Obj As OLEObject
' set Sht variable to "Sheet4"
Set Sht = ThisWorkbook.Sheets("Sheet4")
' set all the cells you want to clear in one Range
Set ClearRng = Sht.Range("B3,B4,C13,C14,F8,J8,B23,J3,G4")
ClearRng.ClearContents
' loop thourgh all OLE objects in "Sheet4"
For Each Obj In Sht.OLEObjects
' for debug > check name if ActiveX Control
Debug.Print Obj.Name
' if OLE Object is type ComboBox
If Obj.progID = "Forms.ComboBox.1" Then
Obj.ListFillRange = "" ' clear all contents of the ComboBox
End If
End Sub
选项2:到清除内容的ComboBox
,使用Sheet4对象类型,则使用下面的代码:
With Sheet4.ComboBox4
.Clear
End With
是你的“表单控件”或“ActiveX控件”的组合框? –
如果它是一个表单控件,它应该是我认为的DropBox4? – jamheadart