Excel VBA - 复制模板工作表并链接单元格和命名表

时间:2014-08-21 20:00:15

标签: excel vba

我在工作表中的A列中有数据名为“摘要”。有几个月它有50行,而有时则有500行。

我有一个名为“模板”的模板表。我想创建一个“模板”表的副本,在Summary的每一行之后命名它(这样一个循环),然后将行单元格数据放在工作表的单元格A1中。最后回到摘要表中,我想在指向工作表的行中创建一个超链接。

这是我希望它看起来像的图像: enter image description here

2 个答案:

答案 0 :(得分:1)

搜索将为您提供大量答案,尤其是在Stackoverflow上。以下是我搜索过的一些示例,也许它会帮助您。

我知道已经发布了一个答案,但由于我已经有了一些东西,并且它略有不同,所以我想发布它,因为它有一些额外的功能,你可能会从中收集到的。它包括:

  1. 错误检查(如果存在相同名称的工作表)
  2. 子例程在传递变量时在单独的例程中被调用
  3. 尝试让我知道你的想法。

     Function SheetExists(shtName As String, Optional wb As Workbook) As Boolean
     'Created by Tim Williams from Stackoverflow.com
     'https://stackoverflow.com/questions/6688131/excel-vba-how-to-test-if-sheet-exists
        Dim sht As Worksheet
    
         If wb Is Nothing Then Set wb = ThisWorkbook
         On Error Resume Next
         Set sht = wb.Sheets(shtName)
         On Error GoTo 0
         SheetExists = Not sht Is Nothing
     End Function
    
    Sub CreateSummarySheets(SummaryWS As Worksheet, TemplateWS As Worksheet)
    
        Dim newWS As Worksheet
        Dim rCell As Range
        Dim lastRow As Long
        Dim answer  As Long
    
        lastRow = SummaryWS.Cells(Rows.Count, "A").End(xlUp).Row
    
        For Each rCell In SummaryWS.Range("$A$1:$A$" & lastRow)
          'Add copy of template
            TemplateWS.Copy After:=Sheets(Sheets.Count)
            Set newWS = Sheets(Sheets.Count)
    
          'Sheet exists error checking
            answer = 1
            If SheetExists(newWS.Name) = False Then
                answer = vbNo
                answer = MsgBox("Sheet with the name " & rCell.Value & " already exists.  Delete it?", vbYesNo, rCell.Value & " Sheet Exists")
            End If
    
            If answer = vbYes Then
                Sheets(rCell.Value).Delete
            End If
            If answer = 1 Or answer = vbYes Then
                newWS.Name = rCell.Value
            End If
    
          'Populate newWS's cell A1
            newWS.Cells(1, "A") = rCell.Value
          'Add Hyperlink from summary to newWS
            newWS.Hyperlinks.Add Anchor:=rCell, Address:="", _
                SubAddress:="'" & newWS.Name & "'" & "!A1", TextToDisplay:=newWS.Name
        Next rCell
    
    End Sub
    
    Sub test()
        Dim s_ws As Worksheet
        Set s_ws = Sheets("Summary")
    
      'Two ways to run this function
        Call CreateSummarySheets(s_ws, Sheets("Template"))
    End Sub
    

答案 1 :(得分:0)

在Excel中玩弄一些后,我相信这将满足您的需求。只需放入一个新模块并执行即可。

Sub CreateLinkedSheets()

    Dim rngCreateSheets As Range
    Dim oCell As Range

    Dim oTemplate As Worksheet
    Dim oSummary As Worksheet
    Dim oDest As Worksheet

    Set oTemplate = Worksheets("Template")
    Set oSummary = Worksheets("Summary")
    Set rngCreateSheets = Worksheets("Summary").Range("A1", Range("A1").End(xlDown))
    'Above line assumes NO blank cells in your list of school supplies

    For Each oCell In rngCreateSheets.Cells

        oTemplate.Copy After:=Worksheets(Sheets.Count)
        Set oDest = ActiveSheet
        oDest.Name = oCell.Value

        oDest.Range("A1").Value = oCell.Value

        oSummary.Hyperlinks.Add Anchor:=oCell, Address:="", SubAddress:= _
            oDest.Name & "!A1", TextToDisplay:=oDest.Name
    Next oCell

End Sub

我支持我最初使用macro recorder检查代码输出然后根据您的需求进行调整的评论。这就是我为获取添加超链接的代码所做的工作,例如。

要使此代码生效,您的工作表必须命名为"摘要"和"模板" (如图片中所示)和列A中的列表必须是连续的,也就是说您不能在列表中留下任何空白单元格。如果您执行第Set rngCreateSheets = Worksheets("Summary").Range("A1", Range("A1").End(xlDown))行,则无法正确设置范围,并且您将丢失项目。