用于从主选项卡中的列表在 Excel 中创建新选项卡并在每个选项卡中使用相同名称填充单元格的宏
Macro to create new tabs in Excel from a list in a Master tab and populate a cell in each tab with the same name
我找到了用于从 "Master" 选项卡中的列表创建和命名新选项卡的代码,但我还需要在每个新选项卡的单元格中填充名称。此外,我需要每个新选项卡都包含一个模板(不同于主选项卡),它只是一个空白 table 和 headers 以及某些列中内置的一些公式。每个新选项卡都应具有完全相同的模板,但填充一个单元格(table 的标题)以匹配选项卡的名称。
最后,我需要用户打开工作簿,在主选项卡中填充一个列表(不会总是相同的长度,可能只有 1),然后按一个按钮(运行一个宏),并根据上述创建选项卡。看来我可能需要创建一个包含要复制的模板的隐藏选项卡?这可能吗?此处的任何指导将不胜感激。谢谢!
欢迎来到 Whosebug。以下是开始解决您的问题的几点:
- 是的,这是可能的!
- Excel 中没有标签,只有表格。
- 带有 table 的模板是个好主意。你可以把它放在一个隐藏的 sheet 中,但是记得给这个 sheet 设置一个非常不寻常的名字,比如
zZzmyVeryHiddenSheetzZz
。这样您就可以非常确定没有人会尝试添加具有完全相同名称的 sheet。
- 使用 VBA 您可以制作模板
veryhidden
,因此即使从 "Unhide sheets" 菜单也无法访问它。
- 使用宏录制器!开始录制宏,做你想做的事,停止录制并检查你的代码。您可以通过将光标放在宏代码中并按 F8 来检查它是如何工作的。代码所做的每项更改都会立即在工作簿中看到。
- 录制宏时尝试使用键盘快捷键,例如不要通过鼠标选择范围,而是尝试使用
shift+arrow
和 ctrl+shift+arrow
组合。这将生成更多动态代码。
祝你好运!
假设您的主 sheet 名为 "Master",模板名为 "Hidden"。您应该能够根据需要调整下面的代码。
(提交我的答案有点晚,但我认为这将为您提供更大的灵活性,因为它更清楚正在发生的事情)
Private Sub CommandButton1_Click()
Dim masterSheet As Worksheet
Dim hiddenSheet As Worksheet
Dim NewSheet As Worksheet
Dim myBook As Workbook
Dim lastRow As Long
Dim i As Long
Dim namesColumn
'Define your workbook - here set as the active workbook, assuming it contains masterSheet and hiddenSheet
Set myBook = ActiveWorkbook
'Define your worksheets - The sheets are named "Master" and "Hidden" respectively
Set masterSheet = myBook.Worksheets("Master")
Set hiddenSheet = myBook.Worksheets("Hidden")
'Define which column in your master tab the list is - here it's A i.e. column 1
namesColumn = 1
'Find the last row of the sheets list
lastRow = masterSheet.Cells(masterSheet.Rows.Count, namesColumn).End(xlUp).Row
'Cycle through the list - Assuming the list starts in column "A" from the 2nd row
For i = 2 To lastRow
With myBook
'New sheet
Set NewSheet = .Worksheets.Add(After:=.Worksheets("Master"))
End With
'Find name of the tab and naming the tab
tabName = masterSheet.Cells(i, namesColumn)
NewSheet.Name = tabName
'Copy from hidden template - You can choose the ranges if predefined or use .Cells(r,c) to do something fancier
hiddenSheet.Range("A1:F6").Copy _
Destination:=NewSheet.Range("A2:F7")
'Paste in e.g. cell A1 i.e. (1,1) the tab name
NewSheet.Cells(1, 1).Value = tabName
Next i
End Sub
我找到了用于从 "Master" 选项卡中的列表创建和命名新选项卡的代码,但我还需要在每个新选项卡的单元格中填充名称。此外,我需要每个新选项卡都包含一个模板(不同于主选项卡),它只是一个空白 table 和 headers 以及某些列中内置的一些公式。每个新选项卡都应具有完全相同的模板,但填充一个单元格(table 的标题)以匹配选项卡的名称。
最后,我需要用户打开工作簿,在主选项卡中填充一个列表(不会总是相同的长度,可能只有 1),然后按一个按钮(运行一个宏),并根据上述创建选项卡。看来我可能需要创建一个包含要复制的模板的隐藏选项卡?这可能吗?此处的任何指导将不胜感激。谢谢!
欢迎来到 Whosebug。以下是开始解决您的问题的几点:
- 是的,这是可能的!
- Excel 中没有标签,只有表格。
- 带有 table 的模板是个好主意。你可以把它放在一个隐藏的 sheet 中,但是记得给这个 sheet 设置一个非常不寻常的名字,比如
zZzmyVeryHiddenSheetzZz
。这样您就可以非常确定没有人会尝试添加具有完全相同名称的 sheet。 - 使用 VBA 您可以制作模板
veryhidden
,因此即使从 "Unhide sheets" 菜单也无法访问它。 - 使用宏录制器!开始录制宏,做你想做的事,停止录制并检查你的代码。您可以通过将光标放在宏代码中并按 F8 来检查它是如何工作的。代码所做的每项更改都会立即在工作簿中看到。
- 录制宏时尝试使用键盘快捷键,例如不要通过鼠标选择范围,而是尝试使用
shift+arrow
和ctrl+shift+arrow
组合。这将生成更多动态代码。
祝你好运!
假设您的主 sheet 名为 "Master",模板名为 "Hidden"。您应该能够根据需要调整下面的代码。
(提交我的答案有点晚,但我认为这将为您提供更大的灵活性,因为它更清楚正在发生的事情)
Private Sub CommandButton1_Click()
Dim masterSheet As Worksheet
Dim hiddenSheet As Worksheet
Dim NewSheet As Worksheet
Dim myBook As Workbook
Dim lastRow As Long
Dim i As Long
Dim namesColumn
'Define your workbook - here set as the active workbook, assuming it contains masterSheet and hiddenSheet
Set myBook = ActiveWorkbook
'Define your worksheets - The sheets are named "Master" and "Hidden" respectively
Set masterSheet = myBook.Worksheets("Master")
Set hiddenSheet = myBook.Worksheets("Hidden")
'Define which column in your master tab the list is - here it's A i.e. column 1
namesColumn = 1
'Find the last row of the sheets list
lastRow = masterSheet.Cells(masterSheet.Rows.Count, namesColumn).End(xlUp).Row
'Cycle through the list - Assuming the list starts in column "A" from the 2nd row
For i = 2 To lastRow
With myBook
'New sheet
Set NewSheet = .Worksheets.Add(After:=.Worksheets("Master"))
End With
'Find name of the tab and naming the tab
tabName = masterSheet.Cells(i, namesColumn)
NewSheet.Name = tabName
'Copy from hidden template - You can choose the ranges if predefined or use .Cells(r,c) to do something fancier
hiddenSheet.Range("A1:F6").Copy _
Destination:=NewSheet.Range("A2:F7")
'Paste in e.g. cell A1 i.e. (1,1) the tab name
NewSheet.Cells(1, 1).Value = tabName
Next i
End Sub