简体   繁体   English

根据超链接单击将单元格值复制并粘贴到另一个工作表

[英]Copy and paste cell value to another sheet based on hyperlink click

Hope you can help me.希望您能够帮助我。

I want to copy and paste a cells value based on when you click a hyperlink on that cell.我想根据您单击该单元格上的超链接的时间复制和粘贴单元格值。

So for example, I have a sheet called Form1, I want to click on an ID in column A it will then copy the value of that cell and paste it to B2 in sheet1 and then take me to sheet 1.因此,例如,我有一个名为 Form1 的工作表,我想单击 A 列中的一个 ID,然后它将复制该单元格的值并将其粘贴到工作表 1 中的 B2,然后将我带到工作表 1。

Currently I have a macro that allows me to click on an active cell and then press a button which then does what is mentioned above.目前我有一个宏,允许我点击一个活动单元格,然后按下一个按钮,然后执行上面提到的操作。 I just think a hyperlink press would be more user friendly and would result in less errors.我只是认为超链接印刷机会更加用户友好并且会导致更少的错误。

Here is the code I have at the moment:这是我现在的代码:

Sub Rectangle13_Click()

ActiveCell.Copy Destination:=Sheets(“Sheet1”).range(“B2”)

Worksheets(“Sheet1”).Activate

End Sub

Any help would be appreciated!任何帮助,将不胜感激! Thank you谢谢

Worksheet FollowHyperLink工作表跟随超链接

  • Copy this code to the sheet module of worksheet Form1 .将此代码复制到工作表Form1的工作表模块。 From there, run the second sub to convert the A column to hyperlinks.从那里运行第二个 sub 将A列转换为超链接。
  • Click away.点击离开。
Option Explicit

Private Sub Worksheet_FollowHyperlink(ByVal Target As Hyperlink)
    If Not Intersect(Columns("A"), Target.Range) Is Nothing Then
        Me.Parent.Worksheets("Sheet1").Range("B2").Value = Target.Range.Value
    End If
End Sub

' Run this to create hyperlinks out of the values in column 'A'
' which will point to cell 'B2' in 'Sheet1'. You can then reformat
' the cells (remove underline, change font color, ...).
Private Sub CreateHyperlinks()
    Dim lRow As Long: lRow = Range("A" & Rows.Count).End(xlUp).Row
    Dim cell As Range
    For Each cell In Range("A2:A" & lRow).Cells
        cell.Hyperlinks.Add cell, "", "Sheet1!B2", , CStr(cell.Value)
    Next cell
End Sub

It is inconvenient to use Hyperlik , I think.我认为使用Hyperlik不方便。 If you try changing the cell value, you cannot simple click it and write something... But if you want that, you can create such a hyperlink in the next way:如果您尝试更改单元格值,您不能简单地单击它并写一些东西......但是如果你想要那样,你可以通过下一种方式创建这样的超链接:

Sub testAddHyperlink()
   Dim DestSh As Worksheet
   Set DestSh = Sheets("Sheet1") 'you can use any sheet name here
   ActiveSheet.Hyperlinks.Add Range("A1"), Address:="", SubAddress:="'" & DestSh.name & "'" & "!A1"
   'it will keep the existing cell value
End Sub

Then, please copy the next event code in the sheet code module (where the hyperlink exists):然后,请复制表单代码模块中的下一个事件代码(超链接所在的位置):

Option Explicit

Private Sub Worksheet_FollowHyperlink(ByVal Target As Hyperlink)
  Dim rngCopy As Range, shDest As Worksheet
   Set shDest = Sheets("Sheet1") 'you may use here any sheet name
   shDest.Range("B2").value = Target.Parent.Value 'the sheet where the hyperlink targets, is already activated...
End Sub

If you want to use the BeforeDoubleClick approach, paste this code into your Form1 worksheet object in the VBA editor...如果您想使用BeforeDoubleClick方法,请将此代码粘贴到 VBA 编辑器中的Form1工作表 object 中...

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
    If Target.Cells.Count = 1 And Target.Cells(1, 1).Column = 1 Then
        Cancel = True
        
        With ThisWorkbook.Worksheets("Sheet1")
            .Range("B2") = Target.Value
            .Activate
        End With
    End If
End Sub

... naturally, this is a base example and you may need to modify it accordingly. ...当然,这是一个基本示例,您可能需要相应地对其进行修改。 For example, when you double click, you may want to ignore the first row if it's a header and not invoke the core parts of the logic.例如,当您双击时,如果第一行是 header,您可能希望忽略它并且不调用逻辑的核心部分。

代码

That will then do what you want.那将做你想做的。

暂无
暂无

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

相关问题 根据单个单元格值将项目复制/粘贴到另一个工作表上 - Copy/Paste Item onto another sheet based on a single cell value 从另一张纸上的单元格值复制同一张纸中范围的一部分的粘贴范围 - Copy Range From One Sheet Paste Part of Range In Same Sheet Based On Cell Value On Another Sheet 基于一个单元格值复制行并引用另一个单元格值并粘贴到新工作表上 - Copy rows based on one cell value and in reference to another cell value and paste on a new sheet 双击复制单元格并自动粘贴到另一张纸上的不同单元格 - Copy A Cell On Double Click And Paste to A Different Cell on Another Sheet Automatically 根据单元格值将数据从一张表复制并粘贴到另一张表 - Copy and paste data from one sheet to another sheet(s) based on cell value 根据单元格中的值将行复制到另一张工作表 - Copy row to another sheet based on value in a cell VBA 根据日期将工作表 1 中的单元格值复制/粘贴到工作表 2 的宏 - VBA Macro to copy/paste cell value from sheet 1 to 2 based on date 根据单元格值复制行并粘贴到新工作表上 - Copy rows based on cell value and paste on a new sheet Excel宏,要基于另一个单元格值复制和粘贴单元格值? - Excel macro, to copy and paste a cell value based on another cell value? 在每个工作表匹配的单元格上复制一个单元格并将其粘贴到另一个VBA - Copy a cell on one sheet and paste it on another VBA based on cell from each sheet matching
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM