使用多个工作簿从Hyperlink调用宏,而无需重新打开宏工作表

时间:2014-08-15 10:19:02

标签: vba excel-vba hyperlink excel

我有2本工作簿:

  

带宏的工作簿

     

要写入的工作簿。

我有2个潜艇。一种是在“工作簿中插入”中插入超链接,另一个Sub将在单击超链接时执行。

然而,我创建超链接的子项让我有些困惑。我需要在Macro Enabled Workbook中引用工作表,但这样做需要Workbook.Open命令。显然,Macro Enabled Workbook已经打开,因此关闭并重新打开它。我对此非常糊涂,有人能指出我正确的方向吗?

因此Macro Sheet将具有以下Sub,该链接指向同一工作表中的另一个Sub。从另一个写入外部电子表格的方法调用“CreateHyperlinks”。

显然下面的“ActiveSheet”是错误的。我想写一个不同的电子表格,所以我也需要打开它(我假设)?

或者,我可以从正在调用“CreateHyperlinks”的write方法传递正在写入的工作表,还是我将所有内容耦合得太多了?

' This is called elsewhere
Sub CreateHpyerlinks(rangeValue, textValue)
    Dim fileName As String
    Dim wb As Workbook
    Dim TheSheet As Worksheet


    fileName = "c:\blah\blah.xlsm"

    ' ** This is the part: How do i reference "TheSheet" without opening the XL?
    Set wb = Workbooks.Open(fileName:=fileName)
    Set TheSheet = wb.Sheets("MasterCard")
    TheSheet.UsedRange.Select       

   ActiveSheet.Hyperlinks.Add Anchor:=rangeValue, Address:=TheSheet!THISISMYMACROHERE(textValue), SubAddress:="", ScreenTip:="Go to Word Documebnt", TextToDisplay:=textValue

End Sub

更新:

我已更新了Sub,正在点击Object does not support his method or property

Sub CreateHpyerlinks(rangeValue, textValue)
    Dim fileName As String
    Dim wb As Workbook
    Dim wbWrite As Workbook
    Dim TheSheetWithMacros As Worksheet
    Dim TheSheetToWriteTo As Worksheet

    fileName = "c:\WorkbookToWriteTo.xlsx"

    Set wb = Application.Workbooks(ThisWorkbook.Name)
    Set TheSheetWithMacros = wb.Worksheets("Sheet1")

    Set wbWrite = Workbooks.Open(fileName:=fileName)
    Set TheSheetToWriteTo = wbWrite.Worksheets("Sheet1")

    ' This Line Errors:
    TheSheetToWriteTo.Hyperlinks.Add Anchor:=rangeValue, Address:="", SubAddress:=TheSheetWithMacros!Goto80BytePopulationGuide(textValue), ScreenTip:="Call Macro", TextToDisplay:=textValue

    wbWrite.Save
    wbWrite.Close

End Sub

TheSheetToWriteTo.Hyperlinks.Add Anchor:=rangeValue, Address:="", SubAddress:=TheSheetWithMacros!Goto80BytePopulationGuide(textValue), ScreenTip:="Call Macro", TextToDisplay:=textValue有问题,显然TheSheetWithMacors!CallMacro不能像我希望的那样工作。

1 个答案:

答案 0 :(得分:1)

您可以将模块的代码从一个工作簿复制到另一个工作簿。我现在假设工作表模块包含宏而没有其他内容(或者可以复制该模块中的所有内容)。

所以我提出这个的原因是,如果你可以复制它,那么你可以将超链接指向一个本地(在同一个文件中)宏 - 因为我们正在将模块复制到新文件 - 我想你已经能够做到了。

这要求您检查信任中心设置以允许访问VBProject。提示将要求您在继续操作之前执行此操作。

Sub writeit()
'copies a specified code module from one workbook, to
' a code module in another workbook
'
'ASSUMPTIONS:
'-the specified destination must already exist
'-the specified destination does not already contain
' any procedures that would conflict naming with the
' copied module procedures
MsgBox "Please make sure to enable access to ""Trust access to the VBA Project Object Model""", vbInformation
Application.CommandBars.ExecuteMso("MacroSecurity")


Dim macrobook As Workbook
Dim otherbook As Workbook

Dim lines As String
Dim destModule As Object 'VBComponent
Dim copyModule As Object 'VBComponent


Set macrobook = ThisWorkbook  'Modify as needed
Set copyModule = macrobook.VBProject.VBComponents("Sheet1")  'Name of your module to copy

Set otherbook = Workbooks(2)  'Modify as needed
Set destModule = otherbook.VBProject.VBComponents("Sheet1")      'name of where it will go

'Appends the lines from CopyModule to DestModule
With destModule.CodeModule
     .InsertLines .CountOfLines + 1, copyModule.CodeModule.lines(1, copyModule.CodeModule.CountOfLines)
End With


End Sub