简体   繁体   中英

Link to Excel Worksheet with Linked Cell in Upper Left

I am generating an Excell spreadsheet in a .NET application. The file has two sheets. The first is a cover sheet with deep links into the data, which is in the second sheet. When a link is clicked, the second sheet opens to that cell, with the position of the cell in the lower left corner. I need it to open with the cell in the upper right corner.

I'm pretty sure there is no way to make this happen in the .NET code that is generating the file. (But if I'm wrong, please tell me.) However, I did find some other discussions about this, and the solution seems to be to add a little but of VB code to the Excel file itself. Meaning, to the template file that is used to seed the data and produce the final file.

Here's the code:

Private Sub Worksheet_FollowHyperlink(ByVal Target As Hyperlink)
    Application.Goto ActiveCell, True
End Sub

Thing is, I really don't know what to do with this code. I could benefit from some step-by-step instructions... like, assume I'm a total and complete idiot newbie when it comes to Excel (which actually, I am.)

Thanks!!

The following code will first go to the cell which is one column to the right of the target, placing it in the top left corner. It then shifts the window one page to the left, and then selects the original target again without scrolling.

Private Sub Worksheet_FollowHyperlink(ByVal Target As Hyperlink)
    Application.Goto ActiveCell.Offset(0, 1), True
    ActiveWindow.LargeScroll ToLeft:=1
    Application.Goto ActiveCell.Offset(0, -1), False
End Sub

I'm not sure what will happen if your target is, for instance, cell B100 - the window shift to the left may or may not work.

PS This code should be placed into the code module for the worksheet containing the hyperlinks.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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