简体   繁体   English

Excel根据单元格值复制到特定工作表

[英]Excel copy to specific sheet depending on cell value

I'm currently trying to copy text from my front sheet to a variant sheet. 我目前正在尝试将文本从我的首页复制到变体表单。

What I want is to copy cells J19,J20,J21 to cells A1,B1,C1 of the different sheets. 我要复制单元格J19,J20,J21到不同工作表的单元格A1,B1,C1。 J19 decides which sheet to copy to as each agent has their own sheet which has a macro pulling the agent names into a data validation onto J19. J19决定要复制到哪个工作表,因为每个业务代表都有自己的工作表,该工作表具有一个宏,该宏将业务代表的姓名拉入J19的数据验证中。

J19 = Agent Name J19 =代理商名称

J20 = Holiday Start Date J20 =假期开始日期

J21 = Holiday End Date J21 =假期结束日期

How do I change Set wsDestin = Sheets("Agent1") so that it looks at J19 to decide the destination cell. 如何更改Set wsDestin = Sheets("Agent1")以便它查看J19以确定目标单元格。

Sub CopyColumnP()
    Dim wsSource As Worksheet
    Dim wsDestin As Worksheet
    Dim lngDestinRow As Long
    Dim rngSource As Range
    Dim rngCel As Range

    Set wsSource = Sheets("Front")
    Set wsDestin = Sheets("Agent1")

    With wsSource

        Set rngSource = .Range(.Cells(19, "J"), .Cells(.Rows.Count, "J").End(xlUp))
    End With

    For Each rngCel In rngSource
        If rngCel.Value = "Design" Then
            With wsDestin
                lngDestinRow = .Cells(.Rows.Count, "A").End(xlUp).Offset(1, 0).Row
                rngCel.EntireRow.Copy Destination:=wsDestin.Cells(lngDestinRow, "A")
            End With
        End If
    Next rngCel
    End Sub

It's easy like this: 这样很容易:

Set wsDestin = ThisWorkbook.Worksheets(wsSource.Range("J19").Value)

To check if the sheet exists end prevent errors use: 要检查工作表是否存在,请使用以下方法防止错误:

On Error Resume Next
    Set wsDestin = ThisWorkbook.Worksheets(wsSource.Range("J19").Value)
    If Not Err.Number = 0 Then 
        MsgBox "Sheet '" & wsSource.Range("A1").Value & "' not found."
        Exit Sub
    End If
On Error GoTo 0

Or if J19 doesn't contain the destination worksheet name but you need to select a worksheet based on J19's value then use: 或者,如果J19不包含目标工作表名称,但您需要根据J19的值选择一个工作表,则使用:

Select Case wsSource.Range("J19").Value
    Case "AAA" 'if value of J19 is AAA select sheet A
        Set wsDestin = Sheets("A")

    Case "B", "C" 'if value of J19 is B or C select sheet BC
        Set wsDestin = Sheets("BC")

    Case Else
        MsgBox "no corresponding worksheet found."
End Select

暂无
暂无

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

相关问题 根据主工作表中的单元格值,将特定范围从“命名”工作表复制到主工作表中的特定范围 - Copy a specific Range from a 'named' sheet to specific Range in Primary sheet depending on Cell Value in Primary sheet Excel 根据单元格值在特定工作表上复制粘贴范围 - Excel copy-paste range on specific sheet according to cell value 如何根据excel中另一个工作表单元格值的特定值填充单元格背景颜色? - How to Fill cell background color depending a specific value from another sheet cell value in excel? Excel:值表1(在表2中查找)将值复制到单元格旁边 - Excel : Value Sheet 1 (Find at Sheet 2) Copy the Value next to the cell 根据单元格中的值将行从一张表复制到另一张表中 - Copy rows from one sheet into another depending on value in cell Excel如果单元格&lt;=值,如何将数据行从工作表1复制到工作表2 - Excel how copy row of data from sheet 1 to sheet 2 if a cell <= Value 复制并粘贴 excel 表,要在代码中引用的表单元格数据的值 - Copy and Paste a excel sheet , Value of Sheet Cell Data to be referenced in code Excel VBA 根据整个工作表中的多项选择更改单元格值 - Excel VBA to change a cell value depending on multiple selection throughout the sheet 搜索范围Sheet1对Sheet2中的范围根据横向单元格中的值复制到Sheet3 - Searching Range Sheet1 Against Range in Sheet2 Copy to Sheet3 depending on value in lateral cell Excel VBA将特定的单元格复制并粘贴到另一个工作表 - Excel VBA copy and paste a specific cell to another Sheet
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM