A
A
Alexander Diunov2016-12-09 10:07:48
Google Sheets
Alexander Diunov, 2016-12-09 10:07:48

How to insert a selection from another sheet into a sheet?

Google spreadsheet has two sheets "Tasks" and "Actions"
In "Tasks"
Fields:
Number,
Line Task
1, Make Kitchen
2, Make Room
In "Actions"
Fields: Task Number,
Line Action:
1, Order Kitchen
1, Bring a kitchen
1, Assemble a kitchen
Question - is it possible to display rows from the "Actions" sheet, where the "Task number" field has a value of 1, under the line with task number 1 in the "Tasks" sheet? If yes, how to do it?
Screenshot as I see it :) -c703cf3d32894744a153863c172ad9c2.jpg

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Fedoryan, 2016-12-09
@AnnTHony

Sub ToDo()
    Dim WB As Workbook
    Dim WS_Task As Worksheet
    Dim WS_Action As Worksheet
    
    Dim TaskRowIndex As Long
    Dim ActionRowIndex As Long
    
    Set WB = Excel.ActiveWorkbook
    Set WS_Task = WB.Worksheets("Задачи")
    Set WS_Action = WB.Worksheets("Действия")
    
    For TaskRowIndex = WS_Task.Rows.Count To 2 Step -1
        If WS_Task.Cells(TaskRowIndex, 1) <> "" Then
            Exit For
        End If
    Next TaskRowIndex
    
    For ActionRowIndex = WS_Action.Rows.Count To 2 Step -1
        If WS_Action.Cells(ActionRowIndex, 1) <> "" Then
            Exit For
        End If
    Next ActionRowIndex
    
    For TaskRow = TaskRowIndex To 1 Step -1
        TaskNumber = WS_Task.Cells(TaskRow, 1)
        If TaskNumber <> "" And IsNumeric(TaskNumber) Then
            TaskRowShift = TaskRow + 1
            For ActionRow = ActionRowIndex To 2 Step -1
                If WS_Action.Cells(ActionRow, 2) = TaskNumber Then
                    WS_Task.Rows(Int(TaskRowShift)).Select
                    Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
                    WS_Task.Cells(TaskRowShift, 3) = WS_Action.Cells(ActionRow, 1)
                    WS_Task.Cells(TaskRowShift, 4) = WS_Action.Cells(ActionRow, 2)
                    WS_Task.Cells(TaskRowShift, 5) = WS_Action.Cells(ActionRow, 3)
                End If
            Next ActionRow
        End If
    Next TaskRow
End Sub

Sheet "TASKS"
Sheet "Actions"
Result
ffee80edeb08429eb34220ae9f5d9603.png

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question