使用从工作表

时间:2017-06-19 16:16:02

标签: excel-vba excel-formula vba excel

第一篇文章 我有一个工作表,用户在单元格B2中输入员工的姓名(John Doe)。

然后我运行VBA代码将工作表重命名为B2中输入的名称。

Private Sub Worksheet_Change(ByVal Target As Range)
    'Specify the target cell whose entry shall be the sheet tab name.
    If Target.Address <> "$B$2" Then Exit Sub
        'If the target cell is empty (contents cleared) then do not change the shet name
    If IsEmpty(Target) Then Exit Sub

    'If the length of the target cell's entry is greater than 31 characters, disallow the entry.
    If Len(Target.Value) > 31 Then
        MsgBox "Worksheet tab names cannot be greater than 31 characters in length." & vbCrLf & _
        "You entered " & Target.Value & ", which has " & Len(Target.Value) & " characters.", , "Keep it under 31 characters"
        Application.EnableEvents = False
        Target.ClearContents
        Application.EnableEvents = True
        Exit Sub
    End If

    'Sheet tab names cannot contain the characters /, \, [, ], *, ?, or :.
    'Verify that none of these characters are present in the cell's entry.
    Dim IllegalCharacter(1 To 7) As String, i As Integer
    IllegalCharacter(1) = "/"
    IllegalCharacter(2) = "\"
    IllegalCharacter(3) = "["
    IllegalCharacter(4) = "]"
    IllegalCharacter(5) = "*"
    IllegalCharacter(6) = "?"
    IllegalCharacter(7) = ":"
    For i = 1 To 7
        If InStr(Target.Value, (IllegalCharacter(i))) > 0 Then
            MsgBox "You used a character that violates sheet naming rules." & vbCrLf & vbCrLf & _
            "Please re-enter a sheet name without the ''" & IllegalCharacter(i) & "'' character.", 48, "Not a possible sheet name !!"
            Application.EnableEvents = False
            Target.ClearContents
            Application.EnableEvents = True
            Exit Sub
        End If
    Next i

    'Verify that the proposed sheet name does not already exist in the workbook.
    Dim strSheetName As String, wks As Worksheet, bln As Boolean
    strSheetName = Trim(Target.Value)
    On Error Resume Next
    Set wks = ActiveWorkbook.Worksheets(strSheetName)
    On Error Resume Next
    If Not wks Is Nothing Then
        bln = True
    Else
        bln = False
        Err.Clear
    End If

    'If the worksheet name does not already exist, name the active sheet as the target cell value.
    'Otherwise, advise the user that duplicate sheet names are not allowed.
    If bln = False Then
        ActiveSheet.Name = strSheetName
    Else
        MsgBox "There is already a sheet named " & strSheetName & "." & vbCrLf & _
        "Please enter a unique name for this sheet."
        Application.EnableEvents = False
        Target.ClearContents
        Application.EnableEvents = True
    End If

End Sub

这部分效果很好。工作表现在名为John Doe。

现在我希望从B2(John Doe)派生的工作表名称填充主表中的单元格。在mastersheet =IF('John Doe'!B2="","",'John Doe'!B2)

中编写了一个简单的if语句

现在我需要一个新的工作表,所以我复制John Doe并粘贴到工作表的末尾,在复制的工作表单元格B2中输入Jimmy John来重命名工作表。返回到mastersheet,现在需要使用名为Jimmy John的工作表填充新单元格。

所以我想问题是如何动态更改主表上的公式=IF('John Doe'!B2="","",'John Doe'!B2)以使用工作表单元格B2中的任何文本。我想到了一个使用find和replace的命令按钮,但是我没有足够的经验来使它工作。

1 个答案:

答案 0 :(得分:0)

您可以使用INDIRECT()公式来解决此问题。

在B1中写入工作表的名称,如果可能的话没有空格(例如 John_Doe JohnDoe )。然后在另一个地方写下以下公式:

=IF(
   INDIRECT(B1&"!A2")="";
    "";
   INDIRECT(B1&"!A2"))

如果你需要空格(例如 John Doe ),公式如下:

=IF(
   INDIRECT("'"&B1&"'!A2")="";
    "";
   INDIRECT("'"&B1&"'!A2"))

公式采用表格中单元格A2的值,其名称在B1中指定。 每当您以某种方式更改目标工作表名称时,您必须在B1中更改工作表的名称。像这样:

enter image description here

您可以在此处查看有关Indirect()的更多信息:

https://support.office.com/en-us/article/INDIRECT-function-474b3a3a-8a26-4f44-b491-92b6306fa261