Hello all,
Today's my first day using VBA; I'm trying to automate a somewhat mundane task for work.
I have a certain number of groups each with their own length, and I'd like to have a macro that creates a separate sheet for each element in each group, and renames it.
The sheet name should correspond to its group number followed by its own number within the group, i.e. if I have three groups with two elements in each group, the sheet naming would be:
1-1, 1-2, 2-1, 2-2, 3-1, 3-2
The first sheet needs to be exempt from this, so the automatic renaming of sheets should start from sheet #2.
I've written some code that mostly works just fine:
The idea is to provide the groups and their sizes in the "categories" array, find out if any additional sheets need to be made based on the current amount of sheets, then rename the sheets according to their group.
The renaming works fine, and if I were to add a group (i.e. categories = Array(11, 14, 3) ), the additional sheets would be made and renamed without issue.
The only problem arises when I increase the size of a pre-existing group (i.e. Array(11, 14) to Array(11, 15) )
For some reason this returns "Subscript out of range (Error 9)"
Does anybody know what's going wrong?
Today's my first day using VBA; I'm trying to automate a somewhat mundane task for work.
I have a certain number of groups each with their own length, and I'd like to have a macro that creates a separate sheet for each element in each group, and renames it.
The sheet name should correspond to its group number followed by its own number within the group, i.e. if I have three groups with two elements in each group, the sheet naming would be:
1-1, 1-2, 2-1, 2-2, 3-1, 3-2
The first sheet needs to be exempt from this, so the automatic renaming of sheets should start from sheet #2.
I've written some code that mostly works just fine:
VBA Code:
Sub シート名数字順()
Dim i As Integer
Dim j As Integer
Dim categories: categories = Array(11, 14) '各分類のサイズ
Dim sheetNum As Integer: sheetNum = 2
Dim catCount As Integer: catCount = 1
'必要になるシート数の計算
Dim neededSheetCount As Integer: neededSheetCount = 0
For Each cat In categories
neededSheetCount = neededSheetCount + cat
Next
'シート数足りてなければ足りない分を追加
If Sheets.Count < neededSheetCount Then
Dim sc As Integer: sc = Sheets.Count
For j = sc To neededSheetCount
Sheets.Add After:=Sheets(Sheets.Count)
Next j
End If
'名前変更
For Each cat In categories
For i = 1 To cat
Sheets(sheetNum).Name = catCount & "-" & i
sheetNum = sheetNum + 1
Next i
catCount = catCount + 1
Next
End Sub
The idea is to provide the groups and their sizes in the "categories" array, find out if any additional sheets need to be made based on the current amount of sheets, then rename the sheets according to their group.
The renaming works fine, and if I were to add a group (i.e. categories = Array(11, 14, 3) ), the additional sheets would be made and renamed without issue.
The only problem arises when I increase the size of a pre-existing group (i.e. Array(11, 14) to Array(11, 15) )
For some reason this returns "Subscript out of range (Error 9)"
Does anybody know what's going wrong?