2015-11-04 165 views
0

的陣列,我有以下工作表的數組:創建電子表格

Dim ArrayOne(11) As Worksheet 
ArrayOne(0) = "January 2015" 
ArrayOne(1) = "February 2015" 
ArrayOne(2) = "March 2015" 
ArrayOne(3) = "April 2015" 
ArrayOne(4) = "May 2015" 
ArrayOne(5) = "June 2015" 
ArrayOne(6) = "July 2015" 
ArrayOne(7) = "August 2015" 
ArrayOne(8) = "September 2015" 
ArrayOne(9) = "October 2015" 
ArrayOne(10) = "November 2015" 
ArrayOne(11) = "December 2015" 

,當我試圖做到以下幾點:

Set ArrayOne(1) = Sheets.Add 
Sheets.Add.Name = ArrayOne(1) 
ArrayOne(1).Protect DrawingObjects:=False, Contents:=False, _ 

它給了我一個錯誤:

Object variable or With block variable not set

而這裏的錯誤點:

ArrayTwo(0) = "January 2015" 

回答

0

您正在嘗試將String值分配給Worksheet數據類型。您可以爲工作表名稱聲明String變量,爲實際工作表聲明Worksheet變量。可能是這樣的:

Dim strArrayOne(11) As String 
Dim wsArrayOne(11) As Worksheet 

strArrayOne(0) = "January 2015" 
strArrayOne(1) = "February 2015" 
strArrayOne(2) = "March 2015" 
strArrayOne(3) = "April 2015" 

Set wsArrayOne(1) = Sheets.Add 
Sheets.Add.Name = strArrayOne(1) 
+0

謝謝!那完美的工作! – Jack

+0

只是一個簡單的問題,我是否甚至需要這個 - > strArrayone(1).Protect DrawingObjects:= False,內容:=假,_ – Jack

+0

如果你只是添加一個不受保護的工作表並命名它,那麼我沒有看到任何該行代碼的原因。你究竟想要做什麼? – BrandonCoder

0

您已聲明ArrayOne類型Worksheet,但您正在使用字符串填充它。

應該是這樣的:

Dim ArrayOne(11) As Worksheet 
Set ArrayOne(0) = Worksheets("January 2015") 
Set ArrayOne(1) = Worksheets("February 2015") 
'...etc 

你沒有表現,其中ArrayTwo從何而來。

相關問題