自动创建一个包含单元格值的工作表,并将单元格值超链接到它

时间:2014-04-18 10:13:36

标签: excel vba excel-vba

我正在使用此VBA代码为每个单元格值创建新工作表

Private Sub Worksheet_Change(ByVal Target As Range)
    Dim wsNew As Worksheet
    If Target.Cells.Count > 1 Then Exit Sub

        On Error Resume Next
        If Not Intersect(Target, Range("A1:A10")) Is Nothing Then
            Set wsNew = Sheets(Target.Text)
            If wsNew Is Nothing Then Sheets.Add().Name = Target.Text
        End If
End Sub

它工作正常,但工作表在我想要的主要工作表之前创建,我想将这些单元格值链接为各自的工作表的超链接。

由于

1 个答案:

答案 0 :(得分:2)

Private Sub Worksheet_Change(ByVal Target As Range)
  If Target.Cells.Count > 1 Then Exit Sub
  If Len(Target.Value) = 0 Then Exit Sub

  If Not Intersect(Target, Range("A1:A10")) Is Nothing Then
    Dim wsNew As Worksheet

    On Error Resume Next
    Set wsNew = Worksheets(Target.Text)
    On Error GoTo 0

    If wsNew Is Nothing Then
      With Worksheets.Add(after:=Me)
        .Name = Target.Text

        Target.Hyperlinks.Delete
        Target.Hyperlinks.Add Target, "", "'" & .Name & "'!" & .Range("A1").Address(False, False, xlA1)
      End With

      Me.Activate
    End If
  End If
End Sub