vba 双击单元格后,将单元格值复制到同一行的另一个单元格

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20949529/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 01:13:18  来源:igfitidea点击:

Upon double clicking a cell, copy cell value to another cell on that same row

excelvbaexcel-vba

提问by Iykeln

This VBA code does what I need but only on one row:

这个 VBA 代码做我需要的,但只在一行上:

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
If Not Intersect(Target, Range("C1:H1")) Is Nothing Then
    Cancel = True

    If Application.CountIf(Sheets("Sheet2").Range("B:B"), Target.Value) = 0 Then
        Range("B1").Value = Target.Value
    End If
End If
End Sub

I have on my Excel worksheet:

我的 Excel 工作表上有:

A    B   C    D    E   F   G   H
MAN     NN   NNP  JJ  POS
CAN     MOD  NN   VV   JJ  RB  NON
...     ...

up to 300,000 data.

多达 300,000 条数据。

I want when I double click any cell in C:H range, its value will be copied to B. The above code handles one row only. If I change C1:H1 to C2:H2 and B1 to B2, it will handle the second row.

我想当我双击 C:H 范围内的任何单元格时,它的值将被复制到 B。上面的代码只处理一行。如果我将 C1:H1 更改为 C2:H2 并将 B1 更改为 B2,它将处理第二行。

How do I make it work for all rows?

如何使它适用于所有行?

采纳答案by Floris

I think the following change will work (not tested, on iPhone):

我认为以下更改将起作用(未测试,在 iPhone 上):

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)

    If Not Intersect(Target, Range("C:H")) Is Nothing Then
        Cancel = True

        If Application.CountIf(Sheets("Sheet2").Range("B:B"), Target.Value) = 0 Then
            Cells(Target.Row, 2).Value = Target.Value
        End If
    End If

End Sub