简体   繁体   English

根据第三个单元格的值将单元格的值复制到另一个单元格中

[英]Copy value of cell into another cell based on the value of a 3rd cell

I need a VBA script to copy the value of a cell (on another sheet) into a blank cell that is to the right of the original reference cell.我需要一个 VBA 脚本来将单元格(在另一张纸上)的值复制到原始参考单元格右侧的空白单元格中。 The reference cell is a Drop Down with three values (Verbal, Written, Demonstrated).参考单元格是具有三个值(口头、书面、演示)的下拉列表。 I need VBA because once the value has been entered into the cell i need to add additional comments into the cell as well.我需要 VBA,因为一旦将值输入到单元格中,我还需要在单元格中添加其他注释。

Col G is the reference cell with a drop down. Col G 是带有下拉菜单的参考单元格。 Col I is the destination of the VBA code Col I 是 VBA 代码的目的地

The look up range is: Sheets.("DO NOT DELETE") Range("C2:D4")查找范围是: Sheets.("DO NOT DELETE") Range("C2:D4")

Any help is greatly appreciated!任何帮助是极大的赞赏!

Make certain that your activesheet is the one with the dropdown values.确保您的活动表是具有下拉值的活动表。 You must make a call to run this function.您必须调用才能运行此函数。

'  This is meant to be run in a module and must be called
Sub tester()
        Dim lastRow As Long, testString As String, rng2Search As Range


            lastRow = ActiveSheet.Range("G65536").End(xlUp).Row
            For i = 1 To lastRow
                If ActiveSheet.Range("G" & i) = "Verbal" Or _
                    ActiveSheet.Range("G" & i) = "Written" Or _
                    ActiveSheet.Range("G" & i) = "Demonstrated" Then
                    Set rng2Search = Sheets("DO NOT DELETE").Range("C2:C4").Find(ActiveSheet.Range("G" & i), LookIn:=xlValues)
                        If Not rng2Search Is Nothing Then
                            ActiveSheet.Range("I" & i).Value = Sheets("DO NOT DELETE").Range("D" & rng2Search.Row).Value
                        End If
                End If
            Next i
            Set rng2Search = Nothing

        End Sub

You can also alter this to run on your worksheet module whenever a cell in column G is changed.您还可以更改此设置以在 G 列中的单元格发生更改时在您的工作表模块上运行。 Open up the worksheet module that contains your dropdown values and paste this in there.打开包含下拉值的工作表模块并将其粘贴到那里。

'  This will run automatically if in the worksheet module    
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim rng2Search As Range

        If Mid(Target.Address, 1, 2) = "$G" Then
            Set rng2Search = Sheets("DO NOT DELETE").Range("C2:C4").Find(Target.Value, LookIn:=xlValues)
                    If Not rng2Search Is Nothing Then
                        ActiveSheet.Range("I" & Target.Row).Value = Sheets("DO NOT DELETE").Range("D" & rng2Search.Row).Value
                    End If
        End If

    End Sub

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

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