excel - 需要根据所选的下拉菜单将新信息复制到其他两个工作表之一

标签 excel vba copy-paste auto-update

我有 3 个工作表,由 3 个不同的人使用。如果选择了“Res”,“Builder Contact”工作表需要输入到“Res Jobs”工作表中;如果选择了“Comm”,则需要输入到“Comm Jobs”工作表中。正在复制的信息不会复制到同一列(例如,“Builder Contact”第 1、10、2、4、5 列将分别是“Res Jobs”第 1、2、3、7、8 列)。

当从“Builder Contact”表的下拉菜单中选择“Res”或“Comm”时,我还需要自动更新此信息。我当前的代码目前可以做到这一点,但我每次都必须点击运行,并且由于循环,它会重复所有内容。但循环是我当前获取“x”值的方式,我需要找到复制所有信息的行。

Sub Res_Comm()
    Sheets("Builder Contact").Select
    ' Find the last row of data
    FinalRow = Cells(Rows.Count, 1).End(xlUp).Row
    ' Loop through each row
    For x = 2 To FinalRow
        ' Decide if to copy based on column K (column with the drop down menu to select "Res" or "Comm")
        ThisValue = Cells(x, 11).Value
        If ThisValue = "Res" Then
            Cells(x, 1).Copy
            Sheets("Res Jobs").Select
            NextRow = Cells(Rows.Count, 1).End(xlUp).Row + 1
            Cells(NextRow, 1).Select
            ActiveSheet.Paste
            Sheets("Builder Contact").Select
            
            Cells(x, 10).Copy
            Sheets("Res Jobs").Select
            NextRow = Cells(Rows.Count, 1).End(xlUp).Row
            Cells(NextRow, 2).Select
            ActiveSheet.Paste
            Sheets("Builder Contact").Select
            
            Cells(x, 2).Copy
            Sheets("Res Jobs").Select
            NextRow = Cells(Rows.Count, 1).End(xlUp).Row
            Cells(NextRow, 3).Select
            ActiveSheet.Paste
            Sheets("Builder Contact").Select
            
            Cells(x, 4).Copy
            Sheets("Res Jobs").Select
            NextRow = Cells(Rows.Count, 1).End(xlUp).Row
            Cells(NextRow, 7).Select
            ActiveSheet.Paste
            Sheets("Builder Contact").Select
            
            Cells(x, 5).Copy
            Sheets("Res Jobs").Select
            NextRow = Cells(Rows.Count, 1).End(xlUp).Row
            Cells(NextRow, 8).Select
            ActiveSheet.Paste
            ' This column is asking for the source, which in this case would be the name of the user for "Builder Contact"
            Cells(NextRow, 6).Value = "Dan"
            
            
            
        ElseIf ThisValue = "Comm" Then
            Cells(x, 1).Copy
            Sheets("Comm Jobs").Select
            NextRow = Cells(Rows.Count, 1).End(xlUp).Row + 1
            Cells(NextRow, 1).Select
            ActiveSheet.Paste
            Sheets("Builder Contact").Select
            
            Cells(x, 10).Copy
            Sheets("Comm Jobs").Select
            NextRow = Cells(Rows.Count, 1).End(xlUp).Row
            Cells(NextRow, 3).Select
            ActiveSheet.Paste
            Sheets("Builder Contact").Select
            
            Cells(x, 2).Copy
            Sheets("Comm Jobs").Select
            NextRow = Cells(Rows.Count, 1).End(xlUp).Row
            Cells(NextRow, 4).Select
            ActiveSheet.Paste
            Sheets("Builder Contact").Select
            
            Cells(x, 4).Copy
            Sheets("Comm Jobs").Select
            NextRow = Cells(Rows.Count, 1).End(xlUp).Row
            Cells(NextRow, 8).Select
            ActiveSheet.Paste
            Sheets("Builder Contact").Select
            
            Cells(x, 5).Copy
            Sheets("Comm Jobs").Select
            NextRow = Cells(Rows.Count, 1).End(xlUp).Row
            Cells(NextRow, 9).Select
            ActiveSheet.Paste
            
            Cells(NextRow, 7).Value = "Dan"
        End If
    Next x
End Sub

[建筑商联系方式][1][Res Jobs][2]

它还不允许我直接添加照片,但希望链接能够正常工作。 [1]:/image/ynDvD.png [2]:/image/1bokm.png

最佳答案

看起来好像您的用户在 K 列中输入了“Res”或“Comm”。下面的代码应将“Builder Contact”表的相应列中的值写入“Res Jobs”表的相应列中或“通讯工作”。您需要将此代码放入“Builder Contact”表的模块中。为此,请双击“Microsoft Excel 对象”下的“Builder 联系人”,如下所示。

enter image description here

然后粘贴此代码:

Private Sub Worksheet_Change(ByVal Target As Range)

    Dim s As Worksheet
    Dim source_columns As Variant
    Dim dest_columns As Variant
    Dim next_row As Long
    Dim x As Long
    
    If Target.Column = 11 Then
        If Target.Value = "Res" Then
            Set s = Sheets("Res Jobs")
            dest_columns = Array(1, 2, 3, 7, 8)
        ElseIf Target.Value = "Comm" Then
            Set s = Sheets("Comm Jobs")
            dest_columns = Array(1, 3, 4, 8, 9)
        Else
            Exit Sub
        End If
        
        source_columns = Array(1, 10, 2, 4, 5)
        
        next_row = s.Cells(s.Rows.Count, 1).End(xlUp).Row + 1
        
        For x = 0 To UBound(source_columns)
             s.Cells(next_row, dest_columns(x)).Value = Cells(Target.Row, source_columns(x))
        Next

        s.Cells(next_row, 6).Value = "Dan"
        
    End If

End Sub

关于excel - 需要根据所选的下拉菜单将新信息复制到其他两个工作表之一,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71682471/

相关文章:

excel - 无法在 Excel 中更改 VBA 表单的 ListBox 属性

excel - 通过vba在excel中打开邮件模板并用excel数据填充模板

Excel 公式 - 2 countif 一个在另一个?

excel - 如何创建 kml 来对 Google 地球中的地址进行地理编码?

excel - 如何将多个文本文件导入单个excel工作表的列

c++ - 将动态分配的数组从 C++ dll 返回到 VBA

Android - 每当用户复制到剪贴板时从剪贴板获取文本

macos - zsh 中的视觉 (Vim) 模式下的 Yank 不会复制到剪贴板以便在其他应用程序中使用 Ctrl + d 进行粘贴

drag-and-drop - 如何在网页或 C# 中读取原始 (CF_HTML) 剪贴板数据?

excel - 计算游戏中单位的值(value)