Copy select column cells for selected rows in one table into another table in another sheet VBA

Viewed 30

I have a set of data in a sheet with available staff and after the user has set whether they wish them to be added to a project in column E and with the project defined in cell I5 I'd like the user to click a button which will create a new line after the last entry in a pre-defined table on another sheet, and then copy the name of the employee from the rows marked with a yes into the corresponding employee (column A) and also copy the Project from I5 in to the Project Name (Column E), then continue to loop through all entries and do this for all marked yes.

The list of available employees in one sheet :

Available Staff

Employee Project Allocation Table:

Employee Project Table

I have created the button and a wrapper for the code to run the looping copy code and have it calling an add new row function to insert a new row after the last entry in the Employee Project Allocation Table which works fine.

Sub AddEmployeesToProject()
    'For Each Row in the available employee list
    'Check if Add To Project column is set to yes
    'Add new row after last entry in Staff Project Allocation Table
    'Copy row employee name to associated column and copy in the Project Name
    AddRow ("Staff Project Allocation")
End Sub

Sub AddRow(strSheet As String)
    Dim wb As Workbook: Set wb = ThisWorkbook ' workbook containing this code
    Dim lastRow As Long

    If Not IsTextEmpty(strSheet) Then
        If WorkSheetExists(strSheet, wb) Then
            Dim SheetSource As Worksheet
            Set SheetSource = wb.Worksheets(strSheet)
                
            lastRow = SheetSource.Cells(SheetSource.Rows.Count, "A").End(xlUp).row
            SheetSource.Range("A" & lastRow + 1).EntireRow.Insert
        End If
    End If
End Sub
0 Answers
Related