2017-03-07 4 views
0

シート名に「benefits」というテキストを含むワークシートにチェックボックスを追加する次のコードがあります。Loop内のCountA関数

行5にテキストが含まれている列ごとに、行3のチェックボックスが必要です。各ワークシートの列数は異なります。

次のコードは機能しますが、すべてのワークシートの最初のワークシートに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 

答えて

1

columnCountを修飾するだけで、常にActiveSheetが表示されます。

作品

columnCount = WorksheetFunction.CountA(ws.Range("5:5")) + 1

+0

に変更

columnCount = WorksheetFunction.CountA(Range("5:5")) + 1

!!説明をしてくれてありがとう。これを初めて知り、なぜ将来の仕事に役立つのか理解する。 – JordanCA57

+0

良いと、答えを受け入れることに感謝 - 多くの人々はしないでください。 –

関連する問題