2016-08-23 181 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