使活动单元格成为新创建的选项卡

时间:2017-01-06 21:57:43

标签: excel-vba vba excel

好的,所以我想创建一个允许用户点击按钮的宏,在输入框中输入他们的名字,复制工作表模板,将新工作表移到书的末尾,重命名为匹配他们的名字,并用一个超链接更新索引表,在列表中显示他们的名字,将他们跳到他们的工作表上的单元格A4。超链接部分是我正在努力的地方,虽然我还需要对其余部分进行一些错误处理。我尝试了各种解决方案,但我不能让它适合我。

Sub CopyIIDTemplate()

    Dim MySheetName As String



    MySheetName = InputBox("Please type your first and last names.")

    Sheets("Template").Copy after:=Sheets("Template")

    ActiveSheet.Name = MySheetName

    ActiveSheet.Range("B1") = MySheetName

    ActiveSheet.Move after:=Worksheets(Worksheets.Count)

    Sheets("Index").Activate

    Sheets("Index").Range("B51").End(xlUp).Offset(1).Activate

    ActiveCell = MySheetName

    Sheets("Index").Hyperlinks.Add anchor:=Excel.Selection, Address:=ActiveCell.Value, SubAddress:="'" & Sheets("Index").ActiveCell.Text & "'!A4"



End Sub

2 个答案:

答案 0 :(得分:1)

创建超链接时,Address参数对于现有工作簿中的某个位置应为空白,并且SubAddress参数可以使用您的MySheetName变量而非无效{{}创建1}}(因为Sheets("Index").ActiveCell.Text是活动单元格而无效 - 它不是ActiveCell对象的属性。)

您的代码的重构版本将是:

Worksheet

注意:如果您的任何员工的名称中包含Sub CopyIIDTemplate() Dim MySheetName As String MySheetName = InputBox("Please type your first and last names.") 'Make a copy of the "Template" sheet and place it after the last worksheet Worksheets("Template").Copy After:=Worksheets(Worksheets.Count) 'Use a "With" block to make typing easier (i.e. we can use '".xxx" instead of "Worksheets(Worksheets.Count).xxx" everywhere 'within the block ' 'Worksheets.Count gives the number of worksheets in the workbook. 'Worksheets(Worksheets.Count) therefore gives the last worksheet 'in the workbook, which is where we placed the copy of "Template". With Worksheets(Worksheets.Count) 'Set this sheet's name .Name = MySheetName 'Set cell B1's value to be the person's name .Range("B1").Value = MySheetName End With 'Use another "With" block to save typing. This time we are 'using the cell one row below the last non-empty cell above cell 'B51 on the worksheet called "Index". ' 'Note: Using Range("B" & Worksheets("Index").Rows.Count) instead of 'Range("B51") may be better - that would find the last non-empty cell 'in the entire column B. ' With Worksheets("Index").Range("B51").End(xlUp).Offset(1) 'Set the cell's value to be the person's name .Value = MySheetName 'Add the hyperlink ' 'Anchor (i.e. where to place the hyperlink): '".Cells(1,1)" when applied to a range will give the top-left 'corner of the range. The range that we are applying it to is 'a single cell, so this will simply point to that same cell. 'It would have been nice to be able to just say "." to point to 'the cell, but that would undoubtedly not work. ' 'Address (i.e. file/url to refer to): 'Just use "" for a reference to the existing workbook ' 'SubAddress (i.e. location within the Address): 'Need to create an address similar to "'Ian'!A4" 'Wrap the person's name in "'" characters, so that embedded 'spaces don't cause issues, and then stick a "!A4" at the end. ' 'Note: If the person's name contains any "'" characters (e.g. '"Antonio D'amore") the simplified version used here won't work 'because "'Antonio D'amore'!A4" actually needs to be '"'Antonio D''amore'!A4", but let's ignore that for now. ' .Hyperlinks.Add Anchor:=.Cells(1, 1), _ Address:="", _ SubAddress:="'" & mySheetName & "'!A4" End With End Sub ,则子地址参数必须为'

答案 1 :(得分:0)

最后一行中有错误的引用:

Sheets("Index").Hyperlinks.Add anchor:=Excel.Selection, Address:=ActiveCell.Value, SubAddress:="'" & Sheets("Index").ActiveCell.Text & "'!A4"

Sheets("Index").ActiveCell替换为ActiveCell,它应该有效。