[英]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.