带空格的超链接

Hyperlink with spaces

我有一个项目sheet。 第一个 sheet 是主要的,包含所有项目的名称。 作者 sheet 用于每个项目。

在主要的 sheet 项目名称中有指向他 sheet 的超链接。

当 运行 代码时,我得到一个弹出消息框,我写了新项目名称(寻找 "project_name")。 代码做的东西。 但接近尾声时有超链接代码。 (寻找 ActiveSheet.Hyperlinks.Add...)

所以我的问题是: 当我选择像 "abcd" 这样的项目名称时,一切正常。但是当我选择 "ab cd" 这样的名字时。代码运行但超链接不起作用。

我意识到在项目名称中包含 space 会使代码无法运行。

感谢您的帮助。

p.s。 注释为希伯来文。

Sub New_project()


'--------------------------------------------------------------------------------------------------תחילת ריצת קוד
    Dim Start, Finish, TotalTime As Date

    Start = Timer


'--------------------------------------------------------------------------------------------------ביטול חישובים ועדכוני מסך והתראות
    Application.Calculation = xlCalculationManual
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
    Application.AskToUpdateLinks = False


'--------------------------------------------------------------------------------------------------החזרת חישובים ועדכוני מסך והתראות
    'Application.Calculation = xlCalculationAutomatic
    'Application.ScreenUpdating = True
    'Application.DisplayAlerts = True
    'Application.AskToUpdateLinks = True


'--------------------------------------------------------------------------------------------------פתיחת חלונית והקלדת שם הפרויקט
'--------------------------------------------------------------------------------------------------אם לחצו cancel אז יציאה מהקוד
    Dim project_name As String
    project_name = InputBox("נא להקליד את שם הפרויקט החדש")

    If Len(project_name) < 1 Then

        Application.Calculation = xlCalculationAutomatic
        Application.ScreenUpdating = True
        Application.DisplayAlerts = True
        Application.AskToUpdateLinks = True

        MsgBox ("יציאה מהקוד")

        Exit Sub

    End If


'--------------------------------------------------------------------------------------------------בדיקה האם שם הגיליון לפרויקט החדש כבר קיים
    Dim curSheet As Worksheet
    Dim ArraySheets() As String
    Dim x As Variant
    Dim sheet_existing As Integer

    x = 0
    sheet_existing = 0
    For Each curSheet In ActiveWorkbook.Worksheets

        If curSheet.Name Like project_name Then

            Worksheets(project_name).Activate
            sheet_existing = 1

            Finish = Timer
            TotalTime = Format((Finish - Start) / 86400, "hh:mm:ss")

            Application.Calculation = xlCalculationAutomatic
            Application.ScreenUpdating = True
            Application.DisplayAlerts = True
            Application.AskToUpdateLinks = True

            MsgBox ("שם הפרויקט כבר קיים" & vbNewLine & "זמן ריצת קוד: " & TotalTime)

            Exit Sub

        End If

    Next curSheet
    'iComp = StrComp(str1, str2, vbBinaryCompare)

'--------------------------------------------------------------------------------------------------הוספת גיליון חדש בסוף הקובץ
    If sheet_existing = 0 Then

        Sheets.Add After:=Sheets(Sheets.Count)
        ActiveSheet.Name = project_name

    End If


'--------------------------------------------------------------------------------------------------הוספת כותרות
    Range("A1") = "#"
    Range("B1") = "תאריך"
    Range("C1") = "שלב"
    Range("D1") = "איש קשר"
    Range("E1") = "הערות"
    Range("F1") = "מסמכים"
    Range("G1") = "ימים"
    Range("H1") = "צבירה"


'--------------------------------------------------------------------------------------------------רוחב עמודה
    Columns("A").ColumnWidth = 9
    Columns("B").ColumnWidth = 11
    Columns("C").ColumnWidth = 30
    Columns("D").ColumnWidth = 16
    Columns("E").ColumnWidth = 17
    Columns("F").ColumnWidth = 9
    Columns("G").ColumnWidth = 6
    Columns("H").ColumnWidth = 10


'--------------------------------------------------------------------------------------------------הוספת מסגרת לתאים
    Dim rng1 As Range

    Set rng1 = Range(Cells(1, 1), Cells(27, 8))
        With rng1.Borders
            .LineStyle = xlContinuous
            .Color = vbBlack
            .Weight = xlThin
        End With


    Range("A:H").HorizontalAlignment = xlCenter
    Range("A:H").VerticalAlignment = xlCenter


    Rows(1).Font.Bold = True
    Columns(1).Font.Bold = True


    Range("A1:H1").Interior.Color = RGB(0, 176, 240)


    Range("A2") = 1
    Range("B2") = Date
    'Range("C2") = "רשום כאן את השלב הראשון"
    Range("G2") = 0
    Range("H2") = 0


    Range("N1:Q1").Merge
    Range("N2:Q12").Merge

    Range("N1:Q1").Interior.Color = RGB(0, 176, 240)

    Range("N1:Q1") = "הערות"


 '--------------------------------------------------------------------------------------------------הוספת מסגרת לתאים
    Dim rng2 As Range

    Set rng2 = Range(Cells(1, 14), Cells(12, 17))
        With rng2.Borders
            .LineStyle = xlContinuous
            .Color = vbBlack
            .Weight = xlThin
        End With


    Range("N:Q").HorizontalAlignment = xlCenter
    Range("N:Q").VerticalAlignment = xlCenter


'--------------------------------------------------------------------------------------------------ספירת גיליונות בקובץ
    Dim SheetCountA As Integer
    SheetCountA = Application.Sheets.Count

'--------------------------------------------------------------------------------------------------העתקת כפתור חזרה לגיליון החדש
    Sheets(SheetCountA - 1).Select
    ActiveSheet.Shapes.Range(Array("Rectangle 1")).Select
    Selection.Copy
    Sheets(SheetCountA).Select
    ActiveSheet.Paste Destination:=Worksheets(SheetCountA).Range("K1")


    Sheets(SheetCountA - 1).Select
    Range("B1").Copy
    Sheets(SheetCountA).Select
    Range("B1").PasteSpecial Paste:=xlPasteFormats

    Application.CutCopyMode = False

    Range("A1").Select

    Sheets(SheetCountA - 1).Select
    Range("A1").Select

    Sheets("סיכום").Select


    Dim LastRowA As Integer
    LastRowA = Application.CountA(Range("B:B"))


'--------------------------------------------------------------------------------------------------הוספת מספור לפרויקט החדש
    Cells(LastRowA + 1, 1) = Cells(LastRowA, 1) + 1


'--------------------------------------------------------------------------------------------------הוספת היפר-לינק
    ActiveSheet.Hyperlinks.Add Anchor:=Cells(LastRowA + 1, 2), Address:="", SubAddress:= _
        project_name & "!A1", TextToDisplay:=project_name

    Cells(LastRowA + 1, 2).HorizontalAlignment = xlCenter
    Cells(LastRowA + 1, 2).VerticalAlignment = xlCenter

    Range("A1").Select


 '--------------------------------------------------------------------------------------------------זמן סיום ריצת קוד וחישוב
    Finish = Timer
    TotalTime = Format((Finish - Start) / 86400, "hh:mm:ss")

    MsgBox ("הדו''ח מוכן" & vbNewLine & "זמן ריצת קוד: " & TotalTime)


'--------------------------------------------------------------------------------------------------שאלה האם לעבור לקוד שמרענן את הקובץ
    Dim answer2 As Integer

    answer2 = MsgBox("?האם לרענן את הקובץ", vbYesNo + vbQuestion, "מעבר לקוד הבא")

    If answer2 = vbYes Then

        Application.Calculation = xlCalculationAutomatic
        Application.ScreenUpdating = True
        Application.DisplayAlerts = True
        Application.AskToUpdateLinks = True

        Call Refresh_file

    End If

'--------------------------------------------------------------------------------------------------שמירת הקובץ
    ThisWorkbook.Save


'--------------------------------------------------------------------------------------------------החזרת חישובים ועדכוני מסך והתראות
    Application.Calculation = xlCalculationAutomatic
    Application.ScreenUpdating = True
    Application.DisplayAlerts = True
    Application.AskToUpdateLinks = True


End Sub

要使超链接有效,您只需将 sheet 名称用引号引起来,因此:

ActiveSheet.Hyperlinks.Add Anchor:=Cells(LastRowA + 1, 2), Address:="", SubAddress:= _
    project_name & "!A1", TextToDisplay:=project_name

变成

ActiveSheet.Hyperlinks.Add Anchor:=Cells(LastRowA + 1, 2), Address:="", SubAddress:="'" & _
    project_name & "'" & "!A1", TextToDisplay:=project_name