简体   繁体   中英

VBA to copy cell from Sheet“A” to merged cells in Sheet“B”

How to copy single cell raw from worksheet"A" to merged cells raw in Worksheet"B" and this suppose to go to the next line in merged cells? I have the following VBA but it only work for the row 5 not other rows.

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

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

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

I tested 2 sections of the above subroutine and they worked fine (other than missing line returns in the first part of your 'if' statement).

The above script checks column N of the current line in sheet Baseline_RA and based on what it finds (Health, Task..., Environment..., etc) ... it copies and pastes into the appropriate sheet.

If it only works on row 5 .... then that's the only row that correctly matches (Health, Task..., Environment..., etc).

Please confirm your other rows has something in column N that matches one of the following: • "Health risk assessment" • "Task risk assessment" • "Environment risk assessment" • "Non-Process risk assessment"

Note: the above values must be exact (ie not extra spaces at the end, etc)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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