VBA将单元格从“A”复制到工作表“B”中的合并单元格

时间:2014-11-11 11:42:48

标签: excel-vba vba excel

如何从工作表中复制单个单元格" A"在工作表中合并单元格" B"这假设要进入合并单元格的下一行?我有以下VBA,但它只适用于第5行而不适用于其他行。

Private Sub Decisionbtn_Click()
If Sheets("Baseline_RA").Cells(ActiveCell.Row, 14).Value = "Health risk assessment" Then
    Sheets("Health RA").Range("A5").Value = Sheets("Baseline_RA").Cells(ActiveCell.Row, 1)
    Sheets("Health RA").Range("B5").Value = Sheets("Baseline_RA").Cells(ActiveCell.Row, 2)
    Sheets("Health RA").Range("I5").Value = Sheets("Baseline_RA").Cells(ActiveCell.Row, 8)
    Sheets("Health RA").Range("O5").Value = Sheets("Baseline_RA").Cells(ActiveCell.Row, 11)
    Sheets("Health RA").Range("P5").Value = Sheets("Baseline_RA").Cells(ActiveCell.Row, 12)
    Sheets("Health RA").Range("Q5").Value = Sheets("Baseline_RA").Cells(ActiveCell.Row, 13)

    ElseIf Sheets("Baseline_RA").Cells(ActiveCell.Row, 14).Value = "Task risk assessment" Then
    Sheets("Task RA").Range("A5").Value = Sheets("Baseline_RA").Cells(ActiveCell.Row, 1)
    Sheets("Task RA").Range("B5").Value = Sheets("Baseline_RA").Cells(ActiveCell.Row, 2)
    Sheets("Task RA").Range("J5").Value = Sheets("Baseline_RA").Cells(ActiveCell.Row, 8)
    Sheets("Task RA").Range("P5").Value = Sheets("Baseline_RA").Cells(ActiveCell.Row, 11)
    Sheets("Task RA").Range("Q5").Value = Sheets("Baseline_RA").Cells(ActiveCell.Row, 12)
    Sheets("Task RA").Range("R5").Value = Sheets("Baseline_RA").Cells(ActiveCell.Row, 13)

    ElseIf Sheets("Baseline_RA").Cells(ActiveCell.Row, 14).Value = "Environment risk assessment" Then
    Sheets("Environment RA").Range("A5").Value = Sheets("Baseline_RA").Cells(ActiveCell.Row, 1)
    Sheets("Environment RA").Range("B5").Value = Sheets("Baseline_RA").Cells(ActiveCell.Row, 2)
    Sheets("Environment RA").Range("H5").Value = Sheets("Baseline_RA").Cells(ActiveCell.Row, 8)
    Sheets("Environment RA").Range("N5").Value = Sheets("Baseline_RA").Cells(ActiveCell.Row, 11)
    Sheets("Environment RA").Range("O5").Value = Sheets("Baseline_RA").Cells(ActiveCell.Row, 12)
    Sheets("Environment RA").Range("P5").Value = Sheets("Baseline_RA").Cells(ActiveCell.Row, 13)

    ElseIf Sheets("Baseline_RA").Cells(ActiveCell.Row, 14).Value = "Non-Process risk assessment" Then
    Sheets("Non-Process RA").Range("A5").Value = Sheets("Baseline_RA").Cells(ActiveCell.Row, 1)
    Sheets("Non-Process RA").Range("B5").Value = Sheets("Baseline_RA").Cells(ActiveCell.Row, 2)
    Sheets("Non-Process RA").Range("H5").Value = Sheets("Baseline_RA").Cells(ActiveCell.Row, 8)
    Sheets("Non-Process RA").Range("N5").Value = Sheets("Baseline_RA").Cells(ActiveCell.Row, 11)
    Sheets("Non-Process RA").Range("O5").Value = Sheets("Baseline_RA").Cells(ActiveCell.Row, 12)
    Sheets("Non-Process RA").Range("P5").Value = Sheets("Baseline_RA").Cells(ActiveCell.Row, 13)
    End If

   End Sub

2 个答案:

答案 0 :(得分:1)

要在合并区域中粘贴某些内容,您应该这样做: 假设mergearea是A1:A6

Range("A1:A6").MergeArea.Cells(1, 1).Value = 'the value you want to paste.

答案 1 :(得分:0)

我测试了上述子程序的2个部分并且它们工作正常(除了在' if'语句的第一部分中缺少行返回)。

上面的脚本检查工作表Baseline_RA中当前行的N列,并根据它找到的内容(Health,Task ...,Environment ...等)...复制并粘贴到相应的工作表中。< / p>

如果它仅适用于第5行....那么这是唯一正确匹配的行(健康,任务......,环境......等)。

请确认您的其他行在N列中的内容与以下内容之一匹配: •&#34;健康风险评估&#34; •&#34;任务风险评估&#34; •&#34;环境风险评估&#34; •&#34;非流程风险评估&#34;

注意:上述值必须精确(即最后不要有额外的空格等)