2016-08-23 180 views
-1

我试图插入一个宏,它在我的选择中计算空白值,然后使用该计数来循环宏以插入新工作表“mycount”次数。我如何做第二个循环部分?谢谢VBA循环函数n次

 '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 
+2

有在若干变化[文档](http://stackoverflow.com/documentation/vba/1873/flow-control-结构#吨= 201608231550302751355)。 – Comintern

回答

1

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

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

谢谢你,工作很棒! – TarasM

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` 
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