我的目標是創建一段代碼,爲數組中的每個字符串創建一個模板表的副本 - 重命名複製的表以匹配字符串。Excel Visual Basic複製工作表
這是我第一次使用Microsoft Visual Basic for Applications。我在「ThisWorkbook」下添加了我的代碼。然而,當我運行的代碼,我收到了
下標越界
錯誤。
Sub generateStationTabs()
Dim stringNames() As Variant
Dim currentString As String
Dim previousSheet As String
previousSheet = "Template "
stringNames() = Array("String 1", "String 2", "String 3", "String 4", "String 5", "String 6", "String 7", "String 8", "String 9", "String 10", "String 11", "String 12", "String 13", "String 14", "String 15", "String 16", "String 17", "String 18", "String 19", "String 20", "String 21", "String 22", "String 23", "String 24", "String 25", "String 26", "String 27", "String 28", "String 29", "String 30")
For indexVariable = 0 To UBound(stringNames)
' Pad String Name to 20 Characters Length currentString = Left(stringNames(indexVariable) & Space(8), 20)
' Copy the Template worksheet to after the Previous Sheet
ActiveWorkbook.Sheets("Template ").Copy after:=ActiveWorkbook.Sheets(previousSheet)
' Rename the copied worksheet
ActiveWorkbook.ActiveSheet.Name = currentString
' Update the previous sheet
previousSheet = currentString
Next
End Sub