[英]excel vba: coping cell with hyperlink formula fails when copy from one the another excel instance
[英]Excel VBA how to copy information from one cell to another between work sheets using a hyperlink
我有一个我正在尝试创建的日历,它将突出显示有活动的日历。 每年的视图显示特定年份的所有月份。 在此视图中,我已设置如果任何一天都有计划活动,那么该日将突出显示。 我想要完成的是如果你从这个视图中选择一个日期,它将带你到另一张表,该表将有更详细的一周的时间表。 我已经设置了所有工具来显示该日期的详细信息,但是我的问题是从年度日历表中提取该日期并将其输入到每周工作表中的给定单元格。
这是我一直在使用的代码,如果是一个单元格超链接的情况,但我不能让它与任何超链接的单元格一起工作。
Dim GSourceCell As String
Private Sub workbook_SheetFollowHyperlink(ByVal Sh As Object, ByVal Target As Hyperlink)
If Sh.Name = "Year" Then
If GSourceCell = "C8" Then
Sheets("Current Week").Range("G4").Value = Sheets("Year").Range("C8").Value
Else
Sheets("Current Week").Range("G4").Value = ""
End If
End If
End Sub
Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
If Sh.Name = "Year" Then
'Capture last active cell on Hyperlinks worksheet and store in global variable
GSourceCell = Target.address(False, False)
End If
End Sub
这是我认为适用于通用案例的内容。
Dim GSourceCell As String
Private Sub workbook_SheetFollowHyperlink(ByVal Sh As Object, ByVal Target As Hyperlink)
If Sh.Name = "Year" Then
If GSourceCell = Sheets("Year").Cells(ActiveCell.Row, ActiveCell.Column).address Then
Sheets("Current Week").Range("G4").Value = Sheets("Year").Range(Cells(ActiveCell.Row, ActiveCell.Column)).Value
Else
Sheets("Current Week").Range("G4").Value = ""
End If
End If
End Sub
Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
If Sh.Name = "Year" Then
'Capture last active cell on Hyperlinks worksheet and store in global variable
GSourceCell = Target.address(False, False)
End If
End Sub
任何有关这方面的帮助将非常感激。 谢谢
一些想法:
如果您将事件从ThiwWorkbook
模块移动到“Year”工作表模块中,您可以更好地控制事件何时触发。 (你可以删除你的一个If
语句。)
由于您已经在使用Worksheet_SelectionChange
事件,为什么不使用该子来检查新选择是否具有超链接? 然后,您可以在同一个子计划中执行所有计算。 例如:
Option Explicit
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
'This code is located in the sheet module for your "Year" sheet.
If Target.Hyperlinks.Count = 1 Then
'If the new selection is a hyperlinked cell, put the cell's value in G4 on the _
'Current Week sheet.
Sheets("Current Week").Range("G4").Value = Target.Value
Else
'If the cell isn't hyperlinked, put nothing in G4.
Sheets("Current Week").Range("G4").Value = vbNullString
End If
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.