我需要遍历工作簿中的所有工作表。如果工作表的名称包含文本“Benefits”,那么我需要在该工作表上运行一个宏作为活动工作表,然后循环到下一个。
我有这个代码和(1)它以任何一种方式运行宏,(2)它不会循环到下一张表。
我是VBA的新手并且无法解决这个问题。
Sub CheckSheets()
Dim sh As Excel.Worksheet
For Each sh In ActiveWorkbook.Sheets
If InStr(sh.Name, "Benefits") <> 0 Then
Call AddCheckBoxesRange
End If
Next sh
End Sub
Sub AddCheckBoxesRange()
'by Dave Peterson
'add Form checkboxes
Dim c As Range
Dim myCBX As CheckBox
Dim wks As Worksheet
Dim rngCB As Range
Dim strCap As String
Set wks = ActiveSheet
Set rngCB = wks.Range("B3:E3")
'Set rngCB = Selection
strCap = "Select Plan"
For Each c In rngCB
With c
Set myCBX = wks.CheckBoxes.Add _
(Top:=.Top, Width:=.Width, _
Height:=.Height, Left:=.Left)
End With
With myCBX
.Name = "cbx_" & c.Address(0, 0)
.LinkedCell = c.Offset(1, 0) _
.Address(external:=True)
.Caption = strCap
' .OnAction = ThisWorkbook.Name _
' & "!mycbxMacro"
End With
Next c
End Sub
答案 0 :(得分:6)
Sub CheckSheets()
Dim sh As Excel.Worksheet
For Each sh In ActiveWorkbook.Sheets
If sh.Name Like "*benefits*" Then
Call AddCheckBoxesRange(sh.Name)
End If
Next sh
End Sub
Sub AddCheckBoxesRange(sName)
ActiveWorkbook.Sheets(sName).Activate
'by Dave Peterson
'add Form checkboxes
Dim c As Range
Dim myCBX As CheckBox
Dim wks As Worksheet
Dim rngCB As Range
Dim strCap As String
Set wks = ActiveSheet
Set rngCB = wks.Range("B3:E3")
'Set rngCB = Selection
strCap = "Select Plan"
For Each c In rngCB
With c
Set myCBX = wks.CheckBoxes.Add _
(Top:=.Top, Width:=.Width, _
Height:=.Height, Left:=.Left)
End With
With myCBX
.Name = "cbx_" & c.Address(0, 0)
.LinkedCell = c.Offset(1, 0) _
.Address(external:=True)
.Caption = strCap
'.OnAction = ThisWorkbook.Name _
' & "!mycbxMacro"
End With
Next c
End Sub
答案 1 :(得分:3)
对您已收到的好答案进行一些小的调整:
Lcase$
以确保捕获字符串Application.ScreenUpdating = False
关闭Excel(虽然在这种情况下不选择工作表会消除大部分屏幕闪烁)。调用sub
Sub CheckSheets()
Dim sh As Worksheet
Application.ScreenUpdating = False
For Each sh In ActiveWorkbook.Sheets
If LCase$(sh.Name) Like "*benefits*" Then Call AddCheckBoxesRange(sh)
Next sh
Application.ScreenUpdating = True
End Sub
主要
Sub AddCheckBoxesRange(ws As Worksheet)
'by Dave Peterson
'add Form checkboxes
Dim c As Range
Dim myCBX As CheckBox
Dim rngCB As Range
Dim strCap As String
Set rngCB = ws.Range("B3:E3")
'Set rngCB = Selection
strCap = "Select Plan"
For Each c In rngCB
With c
Set myCBX = ws.CheckBoxes.Add _
(Top:=.Top, Width:=.Width, _
Height:=.Height, Left:=.Left)
End With
With myCBX
.Name = "cbx_" & c.Address(0, 0)
.LinkedCell = c.Offset(1, 0) _
.Address(external:=True)
.Caption = strCap
End With
Next c
End Sub
答案 2 :(得分:1)
不使用Activate
(总是要避免的好事),您的代码应如下所示:
Sub CheckSheets()
Dim sh As Excel.Worksheet
'Use the Worksheets object instead of the Sheets object
' so that we don't try to process any Charts
For Each sh In ActiveWorkbook.Worksheets
If InStr(sh.Name, "Benefits") <> 0 Then
'Pass the sh worksheet, and avoid the old Call syntax
AddCheckBoxesRange sh
End If
Next sh
End Sub
'Change the subroutine to accept a Worksheet as a parameter
Sub AddCheckBoxesRange(wks As Worksheet)
'by Dave Peterson
'add Form checkboxes
Dim c As Range
Dim myCBX As CheckBox
'Don't need to declare wks anymore as it is declared as a parameter
'Dim wks As Worksheet
Dim rngCB As Range
Dim strCap As String
'Don't need to set wks anymore as it is passed as a parameter
'Set wks = ActiveSheet
Set rngCB = wks.Range("B3:E3")
'Set rngCB = Selection
strCap = "Select Plan"
For Each c In rngCB
With c
Set myCBX = wks.CheckBoxes.Add _
(Top:=.Top, Width:=.Width, _
Height:=.Height, Left:=.Left)
End With
With myCBX
.Name = "cbx_" & c.Address(0, 0)
.LinkedCell = c.Offset(1, 0) _
.Address(external:=True)
.Caption = strCap
' .OnAction = ThisWorkbook.Name _
' & "!mycbxMacro"
End With
Next c
End Sub