简体   繁体   English

Excel VBA创建到同一单元格的超链接

[英]Excel VBA creating a Hyperlink to the same cell

I have a sheet with the string "Title" repeating several times in Column B. For each instance of Title I want to place a hyperlink to the same cell in the value a row directly under it (ie if title is in B1 I would like a hyperlink in B2). 我有一个工作表,其中的字符串“ Title”在B列中重复了几次。对于“ Title”的每个实例,我想在该值的直接下一行放置一个指向同一单元格的超链接(即,如果title在B1中,我想B2中的超链接)。

So if the hyperlink was in B2 effectively it would do nothing but say on the same cell when clicked. 因此,如果超链接有效地位于B2中,则单击该链接时只会在同一单元格上说什么。

However, I need the exsting value in cell B2 to not change, all that will happen is it will go from a regular value to a hyperlinked value. 但是,我需要更改单元格B2中的现有值,所有要做的就是将其从常规值更改为超链接值。

Below is what I have come up with thus far, keep in mind im pretty new to VBA so pointers are appreciated. 以下是到目前为止我要提出的内容,请记住,VBA尚不成熟,因此对指针表示赞赏。

Sub RunThis()
    'Declare workbook and worksheets:
    Dim mainFile As Workbook, titleDetailSheet As Worksheet    
    Set mainFile = ActiveWorkbook    
    Set titleDetailSheet = mainFile.Sheets("Title Detail")
    Dim searchString As String

    searchString = "Title"

    For r = 1 To 200
        If titleDetailSheet.Range("B" & r) = searchString Then
            titleDetailSheet.Range("B" & r + 1) = **'ActiveSheet.Hyperlinks.Add     Anchor:=Selection, Address:="", SubAddress:=titleDetailSheet.Name & "!A1", TextToDisplay:="Title"**
        End If
    Next r
End Sub

I'm not sure what the correct syntax would be for the bolded script. 我不确定粗体脚本的正确语法是什么。

you would need something like this 你需要这样的东西

If titleDetailSheet.Range("B" & r) = searchString Then
    mainFile.ActiveSheet.Hyperlinks.Add _
         Anchor:=titleDetailSheet.Range("B" & r + 1), _
         Address:="", _
         SubAddress:="'" & titleDetailSheet.Name & "'!" & titleDetailSheet.Range("B" & r).Address, _
         TextToDisplay:=titleDetailSheet.Range("B" & r + 1).Value
End If

Kindly see if you can understand the syntax and just ask me if there are unclear items. 请看看您是否能理解语法,然后问我是否有不清楚的项目。 ^-^ ^ - ^

HyperlinkBColumn HyperlinkBColumn

In

Set mainFile = ActiveWorbook

you misspelled Activeworkbook. 您拼错了Activeworkbook。

The part 那个部分

SubAddress:=titleDetailSheet.Name & "!A1"

is a tricky one: 是一个棘手的问题:

SubAddress:="'" & titleDetailSheet.Name & "'!A1"

Your variables are miles long. 您的变量长一英里。 Try to shorten them. 尝试缩短它们。

Option Explicit
'With Project ==================================================================
'  .Title: HyperlinkBColumn
'  .Author: YMG
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'  With .Contents
'    Sub HyperlinkBColumn
'  End With
'===============================================================================
'
'-------------------------------------------------------------------------------
Sub HyperlinkBColumn()
'
'Description:
'  Loops through a specified column and when a specified value is found, puts
'  a hyperlink in the cell below.
'Arguments
'  None
'Returns
'  Hyperlinks on worksheet, Debugging info in the Immediate Window
'
'--Customize BEGIN ---------------------
  Const cWsName As String = "Title Detail"
  Const cSearch As String = "Title"
   Const cRow1 As Integer = 1
      Const cRow2 As Long = 200
     Const cCol As String = "B"
  Const cHeader As String = "Processing rows..." 'Immdediate Window
  Const cFooter As String = "...finished processing." 'Immediate Window
'--Customize END -----------------------
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  Dim oWb As Workbook
  Dim oWs As Worksheet
  Dim rCell1 As Range
  Dim rCell2 As Range
  Dim iR As Integer
  Dim strText As String
  Dim strAddr As String
  Dim str1 As String 'Immediate Window
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  Set oWb = ActiveWorkbook
  Set oWs = oWb.Worksheets(cWsName)
  For iR = cRow1 To cRow2
    Set rCell1 = oWs.Range(cCol & iR)
    Set rCell2 = oWs.Range(cCol & iR + 1)
    strText = rCell2.Text 'What's written in the cell.
    strAddr = rCell2.Address 'The address e.g. B1, B13 ...
    If rCell1 = cSearch Then
      If strText <> "" Then
        'Anchor is the place where to put the hyperlink, cell or object.
        'Notice the single quotes (') in the SubAddress.
        'Readability is very important, notice every argument on its own line.
        'It's much easier to find a mistake.
        rCell2.Hyperlinks.Add _
        Anchor:=rCell2, _
        Address:="", _
        SubAddress:="'" & oWs.Name & "'!" & strAddr, _
        TextToDisplay:=strText 'The same text as requested
        str1 = str1 & vbCrLf & iR & ". " & rCell1.Address & " " _
          & strText & " - at " & strAddr 'Immediate Window
       Else
        'Put in here what to do if the cell below the Title cell is empty.
        'I've chosen to skip the line.
      End If
    End If
  Next
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    str1 = cHeader & str1 & vbCrLf & cFooter 'Immediate Window
    Debug.Print str1 'Immediate Window
'
End Sub
'-------------------------------------------------------------------------------
'
'With Idea Source --------------------------------------------------------------
'  .Title: Excel VBA creating a Hyperlink to the same cell
'  .TitleURL: https://stackoverflow.com/questions/52527595/excel-vba-creating-a-hyperlink-to-the-same-cell
'  .Author: Nayan
'  .AuthorURL: https://stackoverflow.com/users/10416060/nayan
'End With ----------------------------------------------------------------------
'
'End With ======================================================================

One question only: Why would you create the hyperlink to point to the same cell where it is? 仅一个问题:为什么要创建超链接以指向它所在的相同单元格?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM