2015-10-14 33 views
0

我想通過工作簿中的指定工作表循環並指定標籤等。我得到消息「運行時錯誤'9'下標超出範圍下面是我正在使用的代碼.. 。循環指定的工作表

Sub Labels() 

Dim intStartRow As Integer 
Dim intRowCount As Integer 
Dim intRow As Integer 
Dim WshtNames As Variant 
Dim intSheet As Variant 

intStartRow = 32 

WshtNames = Array("shtAngola", "shtBotswana", "shtMozambique", "shtNamibia", "shtSA", "shtZambia", "shtZimbabwe", "shtEritrea", "shtEthiopia", "shtKenya", "shtRwanda", "shtTanzania", "shtUganda", "shtAlgeria", "shtEgypt", "shtMorocco", "shtTunisia", "shtGhana", "shtIvoryCoast", "shtMauritius", "shtNigeria", "shtSenegal") 

For Each intSheet In WshtNames 

    With Worksheets(intSheet) 

     For intRowCount = 7 To 21 
      For intRow = 1 To 3 
       .Cells(intStartRow, 1) = shtSelections.Cells(intRowCount, 1) 

將是你的幫助,非常感謝。

最佳, 亞當

+2

我的猜測將是一個拼寫問題,似乎最有可能受到巨大SHEETNAME陣列做你已判斷。仔細檢查這些拼寫。對於我來說超出範圍的錯誤通常意味着我拼寫錯了,或者我試圖遍歷不存在的列或行號0。 – WouldBeNerd

+0

另外,您應該可能會顯示代碼中'shtSelections'的定義位置。 – WouldBeNerd

+0

同意@WouldBeNerd,但如果這些工作表與您的工作簿不在同一工作簿中,則也會拋出錯誤9,因此請確保「工作表」的父項也已設置。 – Ambie

回答

0

也許你會一排標記標誌聯繫起來。所以你使用循環第二汽車變種intRowCount inst廣告intStartRow

.Cells(intRowCount,1)= shtSelections.Cells(intRowCount,1)