excel 我 如何 也 超 链接 创建 工作 表 在 我 下面 的 vba ?

nfeuvbwi  于 2022-11-18  发布在  其他
关注(0)|答案(1)|浏览(197)

我已经为单元格指定了一个宏,因此,当单击该宏时,它会生成一个模板工作表的副本,询问您要为它命名,然后将该名称添加到列中的下一个空白单元格中。
我已经在下面做了一个尝试,它没有错误,但它也没有超链接。
我现在如何也使单元格的名称去超链接到该工作表?完整的vba在主工作表:

Public Sub CopySheetAndRenameByCell()
  Dim newName As String
  Dim Emrange As Range
  Set Emrange = Application.Range("C" & Rows.Count).End(xlUp).Offset(1)
  On Error Resume Next
  newName = InputBox("Enter the name of the new project", "Copy worksheet", ActiveCell.Value)

  If newName <> "" Then
    Sheets("Project Sheet BLANK").Copy After:=Worksheets(Sheets.Count)
    On Error Resume Next
    ActiveSheet.Name = newName
    Emrange.Value = newName
    Worksheets(newName).Select
    Emrange.Select
    ActiveCell.Hyperlinks.Add Anchor:=Selection, Address:="", SubAddress:="newName!A1", TextToDisplay:="New sheet"
  End If
End Sub
yhxst69z

yhxst69z1#

就像这样:

Public Sub CopySheetAndRenameByCell()
    Dim newName As String, Emrange As Range, wsNew As Worksheet, wb As Workbook
    Dim wsIndex As Worksheet
    
    newName = InputBox("Enter the name of the new project", _
                       "Copy worksheet", ActiveCell.Value)
    
    If newName <> "" Then
        Set wb = ThisWorkbook
        wb.Worksheets("Project Sheet BLANK").Copy _
                      After:=wb.Worksheets(wb.Worksheets.Count)
        Set wsNew = wb.Worksheets(wb.Worksheets.Count)
        On Error Resume Next 'ignore error on rename
        wsNew.Name = newName
        On Error GoTo 0     'stop ignoring errors
        
        Set wsIndex = wb.Worksheets("Index") 'for example
        Set Emrange = wsIndex.Range("C" & Rows.Count).End(xlUp).Offset(1)
        wsIndex.Hyperlinks.Add Anchor:=Emrange, _
                           Address:="", SubAddress:="'" & wsNew.Name & "'!A1", _
                           TextToDisplay:=wsNew.Name
        'reset font style
        Emrange.Font.Underline = xlUnderlineStyleNone 
        Emrange.Font.ColorIndex = xlAutomatic

        If wsNew.Name <> newName Then 'in case sheet could not be renamed....
            MsgBox "Name provided '" & newName & _
                    "' is not valid as a worksheet name!", vbExclamation
        End If
    End If
End Sub

相关问题