0
我有以下代碼,將複選框添加到工作表中,並在工作表名稱中帶有「優點」文本。循環中的CountA函數
我需要第3行中的複選框,以便在第5行中包含文本的每個列。每個工作表都有不同數量的列。
下面的代碼可以工作,但是會爲所有工作表的第一個工作表設置並使用columnCount。
我想我有正確的代碼,但它有錯誤的順序?
'Checks for the text "Benefits" in the sheetname.
'If true then runs AddCheckBoxesRange Macro to add selection checkboxes for each plan.
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
'Macro CheckSheets looks for the text benefits in sheetname, if it exists it calls this macro
Sub AddCheckBoxesRange(ws As Worksheet)
'add Form checkboxes
Dim c As Range
Dim myCBX As CheckBox
Dim rngCB As Range
Dim strCap As String
Dim columnCount As Integer
columnCount = WorksheetFunction.CountA(Range("5:5")) + 1
Set rngCB = ws.Range("B3", ws.Cells(3, columnCount))
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(37, 0) _
.Address(external:=True)
.Caption = strCap
End With
Next c
End Sub
這作品!非常感謝解釋。這是新的 - 所以理解爲什麼將有助於未來的工作。 – JordanCA57
很好,謝謝你接受答案 - 很多人不知道。 –