简体   繁体   English

Excel VBA:创建新工作表的超链接

[英]Excel VBA: Hyperlink that creates a new sheet

Wondering if this is even possible, but: 想知道这是否可能,但是:

So I got a macro button that inserts values into the sheet from a UserForm, and here's a visual example how it approximately looks like: 因此,我得到了一个宏按钮,该按钮可将值从UserForm插入到工作表中,这是一个直观的示例,其外观大致类似于:

 <table style="width:100%"> <tr> <td>OrderID:</td> <td>Product</td> <td>Q</td> <td>Invoice</td> </tr> <tr> <td>1</td> <td>Potato</td> <td>1337</td> <td><a href="url">Open Invoice</a></td> </tr> </table> 

The "Open Invoice" link I create with this code: 我使用以下代码创建的“未结发票”链接:

    Sheets("Sales").Hyperlinks.Add Sheets("Sales").Cells(emptyRow, 9), "", Sheets("Sales").Name, "", "Open Invoice"

I would like that, whenever I click "Open Invoice", it would create a new Sheet (preferably a new Excel window) and then the data transfers into a template (the transferring and template I think I will manage myself) 我想,每当我单击“打开发票”时,它将创建一个新的工作表(最好是一个新的Excel窗口),然后将数据传输到一个模板中(我认为我将管理自己的传输和模板)

Thanks! 谢谢!

Do this to set your hyperlink: 这样做设置您的超链接:

Dim hlRange as Range
Set hlRange = Sheets("Sales").Cells(emptyRow, 9)
With hlRange.Hyperlinks
    .Delete
    .Add Anchor:=hlRange, _
         Address:="", _
         SubAddress:=hlRange.Address, _
         TextToDisplay:="Open Invoice"
End With

This creates a hyperlink that links back to itself, so it's a valid reference but it doesn't go anywhere else . 这将创建一个链接回自身的超链接,因此它是有效的引用,但不会在其他任何地方使用

Then, using the Worksheet_FollowHyperlink event procedure to create a new Workbook from your template, and from there you can insert the data as needed: 然后,使用Worksheet_FollowHyperlink事件过程从您的模板创建一个新的工作簿,然后您可以根据需要插入数据:

Private Sub Worksheet_FollowHyperlink(ByVal Target As Hyperlink)
Dim wb as Workbook
Dim ws as Worksheet
' If there are other hyperlinks in this sheet which should NOT trigger the new file, then you will need to add an Intersect test and exit sub early.

'Add a new workbook
Set wb = Workbooks.Open("path to template file")  '## Modify as needed
Set ws = wb.Worksheets(1)                         '## Modify as needed

'Add the data to the new sheet in new workbook
'  -- code here, or call another procedure to do the operation --

End Sub

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

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