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

半世苍凉 提交于 2019-12-08 09:18:16

问题


I have found code for creating and naming new tabs from a list in a "Master" tab, but I need to also have the name be populated in a cell in each new tab. Further, I need each new tab to contain a template (different from the master tab) which is merely a blank table with headers and some formulas built in to some of the columns. Each new tab should have the exact same template but with one cell (a title for the table) being populated to match the name of the tab.

In the end, I need the user to open the workbook, populate a list in the master tab (will not always be the same length and may be just 1), then press a button (run a macro), and have the tabs be created per the above. It seems that maybe I need to create perhaps a hidden tab that contains the template to be copied? Is this possible? Any guidance here would be greatly appreciated. Thanks!


回答1:


Assuming your master sheet is named "Master" and template is named "Hidden". You should be able to adjust the code below to your needs.

(A bit late to submit my answer, but i think this will provide you with more flexibility as it is clearer what is happening)

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



回答2:


Welcome to StackOverflow. Here are few points to get started with your problem:

  1. Yes, this is possible!
  2. There are no tabs in Excel, but Sheets.
  3. Template with table is a great idea. You can put it in a hidden sheet, but remember to set very unusual name for this sheet, like zZzmyVeryHiddenSheetzZz. That way you can be quite sure that no one will try to add a sheet with exactly same name.
  4. With VBA you can make your template veryhidden, so it will be inaccessible even from "Unhide sheets" menu.
  5. Use macro recorder! Start recording a macro, do what you want to be done, stop recording and check your code. You can examine how it is working by putting cursor inside your macro code and pressing F8. Every change made by code will be immediately seen in workbook.
  6. Try to use keyboard shortcuts while recording macro, e.g. instead of selecting range by mouse, try to use shift+arrow and ctrl+shift+arrow combinations. This will generate more dynamic code.

Good luck!



来源:https://stackoverflow.com/questions/34881535/macro-to-create-new-tabs-in-excel-from-a-list-in-a-master-tab-and-populate-a-cel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!