Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
VBA-指向新工作表的超链接;命名问题_Vba_Excel_Hyperlink - Fatal编程技术网

VBA-指向新工作表的超链接;命名问题

VBA-指向新工作表的超链接;命名问题,vba,excel,hyperlink,Vba,Excel,Hyperlink,我有一个带有输入框(用于公司名称)的工作表,该输入框在表格中创建一个新选项卡和一个新条目,并带有指向新工作表的超链接。超链接有效,但仅当名称中没有空格时(即如果名称为“公司”)。超链接工作正常,但如果名称为“新公司”,则超链接不工作。我可以手动编辑后的事实,使它再次工作的链接,但我希望找到一个解决问题的办法在这里。代码示例附在下面 Sub NewCompany() Dim strName As String Dim strLink As String 'get the name

我有一个带有输入框(用于公司名称)的工作表,该输入框在表格中创建一个新选项卡和一个新条目,并带有指向新工作表的超链接。超链接有效,但仅当名称中没有空格时(即如果名称为“公司”)。超链接工作正常,但如果名称为“新公司”,则超链接不工作。我可以手动编辑后的事实,使它再次工作的链接,但我希望找到一个解决问题的办法在这里。代码示例附在下面

Sub NewCompany()

Dim strName As String

Dim strLink As String

'get the name

    'InputBox(prompt[, title] [, default] [, xpos] [, ypos] [, helpfile, context])
    strName = InputBox("Enter Company Name.", "NAME COLLECTOR")
    'Exit sub if Cancel button used or no text entered
    If strName = vbNullString Then Exit Sub

    MsgBox "Creating Tab " & strName

'create new row

    Rows("4:4").Select
    Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove

'put Company name in column A

    Cells(4, 1).Value = strName

' create new tab, input name in new tab cell, rename new tab

    Sheets("Blank").Select
    Application.Run "BLPLinkReset"
    Sheets("Blank").Select
    Application.CutCopyMode = False
    Sheets("Blank").Copy After:=Sheets(5)
    Application.Run "BLPLinkReset"
    Sheets("Blank (2)").Select
    Application.Run "BLPLinkReset"
    Cells(3, 3).Value = strName
    Sheets("Blank (2)").Name = strName
    Sheets("Home").Select
    Application.Run "BLPLinkReset"
    Range("B4").Select
    Application.CutCopyMode = False

'create hyperlink to new tab

    strLink = strName & "!A1"
    Range("B4").Select
    ActiveSheet.Hyperlinks.Add Anchor:=Selection, Address:="", SubAddress:= _
        strLink, TextToDisplay:=strName
    Range("A4").Select

End Sub
总之,当公司名称中没有空格时,超链接可以工作,但我想找到一种方法来更改代码,使其与公司名称中的空格一起工作。任何帮助都将不胜感激。

更改此行:

strLink = strName & "!A1"


谢谢你,成功了。您能告诉我为什么和它是如何工作的吗?@Jon0311链接的名称需要包含在单引号中才能正确声明如果Excel中的公式包含对另一工作表上单元格的引用,并且该工作表的名称中(例如)有空格,Excel会自动在公式引用中的工作表名称周围添加单引号:=“工作表1”!A1。由于您事先不知道新工作表的名称中是否有空格,因此最好始终添加单引号。
strLink = "'" & strName & "'!A1"