VBA循环功能n次

时间:2016-08-23 15:48:26

标签: excel vba excel-vba

我正在尝试插入一个宏,该宏在我的选择中计算空白值,然后使用该计数来循环宏以插入新的表格" mycount"次数。我如何做第二个循环部分?谢谢

     'cnt blank cells in col U
     Dim mycount As Long
     mycount = Application.WorksheetFunction.CountBlank(Selection)


    ‘add sheets for number of mycount
    Dim looper as integer
    Looper=mycount
    Do while looper <=mycount
    Sheets.add after:=ActiveSheet
    Loop

3 个答案:

答案 0 :(得分:1)

这将进入无限循环。您需要增加控制变量

Do while looper <=mycount
    Sheets.add after:=ActiveSheet
    looper=looper +1
 Loop

答案 1 :(得分:1)

我认为你想要的是For Loop

Dim mycount As Long
Dim i       as Long
mycount = Application.WorksheetFunction.CountBlank(Selection)

'This will add 'MyCount' number of sheets
for i = 1 to myCount
    Sheets.add after:=ActiveSheet
Next i`

答案 2 :(得分:1)

这将添加新的工作表mycount次:

Sub Test()
Dim mycount As Long
    mycount = Application.WorksheetFunction.CountBlank(Selection)
For i = 1 To mycount
    Sheets.Add after:=Sheets(Sheets.Count)
Next i
End Sub