1

I have 2 Workbooks:

Workbook With Macros

Workbook to write to.

I have 2 subs. One is to insert a hyperlink in the 'workbook to write to' and the other Sub will execute when the hyperlink is clicked.

However, my sub to create the Hyperlink is causing me some confusion. I need to reference the worksheet in the Macro Enabled Workbook, but doing that requires a Workbook.Open command. Obviously the Macro Enabled Workbook will already be open so this closes and re-opens it. I've gotten very muddled with this, can someone point me in the right direction?

So Macro Sheet will have the below Sub, the link is to another Sub in the same sheet. "CreateHyperlinks" is called from another method which writes to an external spreadsheet.

Obviously "ActiveSheet" below is wrong. I want to write to a different spreadsheet, so I will need to open it also (I assume)?

Or, can I pass the worksheet that is being written to from it's write method which is calling "CreateHyperlinks" or am I coupling everything too much as it is?

' 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

UPDATED:

I have updated the Sub, and am hitting 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

The line TheSheetToWriteTo.Hyperlinks.Add Anchor:=rangeValue, Address:="", SubAddress:=TheSheetWithMacros!Goto80BytePopulationGuide(textValue), ScreenTip:="Call Macro", TextToDisplay:=textValue is at fault, clearly TheSheetWithMacors!CallMacro doesn't work like I had hoped.

Community
  • 1
  • 1
RossC
  • 1,200
  • 2
  • 11
  • 24
  • 1
    Good place to start, [avoid using Select](http://stackoverflow.com/a/10717999/445425) – chris neilsen Aug 15 '14 at 10:43
  • @chrisneilsen thanks, I've done that now, it was (sort of) necessary elsewhere but I've removed that now, it's bad in general! – RossC Aug 15 '14 at 10:44
  • I've made a lot of modifications now, and updated my question. However, the syntax of the hyperlink seems to be tripping me up. Does anyone know what I'm missing here! – RossC Aug 15 '14 at 11:02
  • 1
    I think the problem is in the `SubAddress` argument. TheSheetWithMacros is in *another* workbook, so you need to qualify that, something like `SubAddress:=[Workbook_name.xlsm]SheetName!MacroName(args)` – David Zemens Aug 15 '14 at 12:22
  • 1
    Why does this need to be a hyperlink? Couldn't you just use the Worksheet's `Selection_Change` procedure to auto-run a macro any time the cell is selected? – David Zemens Aug 15 '14 at 12:35
  • @DavidZemens I didn't know that was an option. I've tried every combination of `SubAddress:=[Workbook_name.xlsm]SheetName!MacroName(args)` that I can, and all I get is either Syntax error, or "Object Required". I'm not sure this can actually be done this way at all. I'm at the posting to freelancer to pay someone stage with this now. Five hours of my life wasted. I'm not a programmer AT ALL so this is all very very new and confusing to me. I don't knwo about Selection_Change on a workbook that is manually created by VBA to be honest, probably too much to get done before deadline in 2 hours. – RossC Aug 15 '14 at 13:01
  • Realistically what you want to do is distribute the macro so that it's available in *any* workbook. This is done typically through an Add-In (.xlam or .xla) file... although it can also be done through pure VBA. But even if you can get the hyperlink or `Selection_Change` event to work, that still requires that the macro book exists **and** is open when the user(s) click the link. – David Zemens Aug 15 '14 at 13:24
  • Thanks again, addins are blocked in the office, so I can't do that as an option. I'm going to tell them I can't do it at this stage, no point in losing a whole day. I appreciate you taking the time out to help, but sadly this is a very specific issue that has all these constraints around it. The only solution I can see is to get Hyperlinks.Add working somehow. – RossC Aug 15 '14 at 13:31
  • well the other issue is that *worksheet* level macros are typically used as private macros, that are generally only used on that particular worksheet. I'm certain it will be easier to access the macro even from Hyperlinks if you put it in a "normal" code module. – David Zemens Aug 15 '14 at 13:35
  • 1
    BTW, "add-ins are blocked"? What sort of organization do you work for?? SMH man... Add-ins expose all of the most powerful features of VBA to your projects. SMH... I never understood corporate policies that restrict you from using the best tools for the job... even to relax the restrictions slightly and require digitally signed add-ins, or some approval process, etc. – David Zemens Aug 15 '14 at 13:37
  • Financial institution / payment processing. I'm only new in the office so hitting walls everywhere. I think I'll go with the fact I can't create an Add in as the issue, and see what they want. I'm just hitting brick walls everywhere. I have a macro to open a word document and go to the relevant section of it working fine (that's the one I want to link to ) but they'll have to come up with different functionality, I'm at the end of my rope at this stage. Thanks very much again! – RossC Aug 15 '14 at 13:44

1 Answers1

1

You may be able to copy the Module's code from one workbook to the other workbook. I assume for now that the Worksheet module only contains the macro and nothing else (or that it is OK to copy everything in that module).

So the reason I propose this, is that if you can copy it, then you can point the hyperlink to a local (within the same file) macro -- because we're copying the module to the new file -- which I think you have already been able to do.

This requires you to check the trust center setting to allow access to the VBProject. A prompt will ask you to do that before it can proceed.

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
David Zemens
  • 53,033
  • 11
  • 81
  • 130
  • Thanks very much. I'll have to spend a good bit of time with this. The module is 2500+ lines of code long! I'm going to refactor the whole damn thing. If this doesn't work, nothing will! MUCH appreciated. – RossC Aug 15 '14 at 14:23
  • 1
    I'll have to modify the above, but yeah it's working already (not fully of course) Hugely appreciated, marked as accepted now as I can work from here! Brilliant! – RossC Aug 15 '14 at 14:31
  • 1
    No problem! If you know the specific line #s that need to be copied, that could be done too, which would avoid copying the entire 2500+ lines of code if the rest aren't needed. For example you would just do: `.InsertLines .CountOfLines + 1, copyModule.CodeModule.lines(105, 139)` which would only add in the lines between 105 and 139, etc. – David Zemens Aug 15 '14 at 14:36
  • thank you yet again, that's actually really, really useful for other things that are being worked on! :) Looking good here, and the powers that be are happy to allow a few more day's work on this! – RossC Aug 15 '14 at 15:22