简体   繁体   English

使用宏在Excel中添加链接

[英]Adding links in Excel with macro

i have script which is doing wright thing for me, but I can use it just in single cell. 我有为我做事的脚本,但是我可以在单个单元格中使用它。

    Sub Macro1()
'
' Macro1 Macro
'
Dim CName As String

CName = ActiveCell
'
    ActiveSheet.Hyperlinks.Add Anchor:=Selection, Address:= _
        "SWFdataEN\DOKUMENTE\C00\04%20TB031\" & CName, TextToDisplay _
        :=CName
End Sub

It is adding link to filename which is written in this cell (CName) and sets TextToDisplay in same name (CName). 它添加了指向文件名的链接,该文件名写在该单元格(CName)中,并将TextToDisplay设置为相同的名称(CName)。

How can I apply this macro in next cell in column (like i hit enter and run macro again) automatically? 如何在该列的下一个单元格中自动应用此宏(例如我再次按下Enter键并再次运行宏)? I tried with For-Next loop, but no good results. 我尝试使用For-Next循环,但效果不佳。 Script always puts link of same first filename, but TextToDisply is correct. 脚本总是将具有相同第一个文件名的链接放入,但是TextToDisply是正确的。 Help please 请帮助

I've worked out this: 我已经解决了这个问题:

In a public module, you insert your macro: 在公共模块中,插入宏:

Sub sofMacro1()
'
  Dim CName As String

  CName = ActiveCell
'
  If (CName <> "") Then
    ActiveSheet.Hyperlinks.Add Anchor:=Selection, Address:= _
      "SWFdataEN\DOKUMENTE\C00\04%20TB031\" & CName, TextToDisplay _
      :=CName
  End If
'
End Sub

Then in the current Worksheet, you add an event handler: 然后在当前工作表中,添加一个事件处理程序:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
  sofMacro1
End Sub

It adds a Hyperlink to any cell that contains text (not empty), when you move your cursor anywhere in the ActiveSheet. 当您将光标移到ActiveSheet中的任意位置时,它会将超链接添加到任何包含文本的单元格(不为空)。

在此处输入图片说明

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

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