Consolidated Report - VBA Copy from one sheet and insert copied cells (shift cells down) in another sheet respecting the main column

BorisTheCat84

New Member
Joined
Apr 30, 2021
Messages
17
Office Version
  1. 2019
Platform
  1. Windows
Hi Team,
I'm looking to run a VBA code from Sheet3 (Consolidated Report) that will check ID1 in sheet2 and when found it has to copy from Sheet2 ID2,Owner,Role,Role Status, and Role Title and insert copied cells (Cells shift down) respecting the cell in column A as the copied data starts from column B but the cell in column A should shift down as well // If it doesn't find ID1 in sheet2 then skip to the other.
In the current example, we have a total of 5 rows but actually the script should detect the last row and stop on it, this report would have in the future x number of rows.
Both Sheet2 and Consolidated Report are in the same workbook:

Sheet2:
Mr. Excel.xls
ABCDEF
1Up and Coming Tournament
2ID1ID2OwnerRoleRole StatusRole Title
3S001TK001BorisClownNEWClown Ready
4S002TK002KieraJunglerNEWJungler Sleepy
5S003TK003TartaFlyerNEWFlyer on the fly
6AntonSpyApprovedSpy mode activated
7CaroAsherApprovedAsher is Ashing
8SinnaSpypendingSpy mode activated
9S004TK004VictorClownApprovedClown Ready
10CaroAsherApprovedAsher is Ashing
11TK005ArseneClownpendingClown Ready
12S006TK006BorisAsherpendingAsher is Ashing
Sheet2


Consolidated Report:
Mr. Excel.xls
ABCDEF
1Consolidated Report
2ID1ID2OwnerRoleRole StatusRole Title
3S001
4S004
5S003
6S005
7S006
8S002
Consolidated Report


Expected Result after running the VBA Code on Sheet3 (Consolidated Report):

Consolidated Report
ID1ID2OwnerRoleRole StatusRole Title
S001TK001BorisClownNEWClown Ready
S004TK004VictorClownApprovedClown Ready
CaroAsherApprovedAsher is Ashing
S003TK003TartaFlyerNEWFlyer on the fly
AntonSpyApprovedSpy mode activated
CaroAsherApprovedAsher is Ashing
SinnaSpypendingSpy mode activated
S005
S006TK006BorisAsherpendingAsher is Ashing
S002TK002KieraJunglerNEWJungler Sleepy




Your help will be truly appreciated
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
Try this in a standard module:
VBA Code:
Sub TransferDataBasedOnCriteria()
    Dim sh2 As Worksheet, lr As Long, i As Long, fnd As Range, cnt As Long
    
    Set sh2 = Worksheets("Sheet2")
    Application.ScreenUpdating = False
    
    With Worksheets("Consolidated Report")
        If .Cells(3, "A") = "" Then GoTo errHandler
        lr = sh2.Range("A:F").Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
        For i = .Cells(Rows.Count, "A").End(xlUp).Row To 3 Step -1
            Set fnd = Range(sh2.Cells(3, "A"), sh2.Cells(lr, "A")).Find(.Cells(i, "A").Value, , xlValues, xlWhole)
            If Not fnd Is Nothing Then
                cnt = fnd.MergeArea.Count
                If cnt <> 1 Then
                    .Rows(.Cells(i, "A").Offset(1).Row & ":" & .Cells(i, "A").Offset(1).Row + cnt - 2).Insert (xlDown)
                End If
                fnd.MergeArea.Resize(, 6).Copy .Cells(i, "A")
            End If
            Set fnd = Nothing
        Next i
    End With
    
    Application.ScreenUpdating = True
    MsgBox "Done."
    
Exit Sub
errHandler:
    MsgBox "No IDs are in column A on Consolidated Report.", vbExclamation, "Error"
    
End Sub
 
Upvote 0
Try this in a standard module:
VBA Code:
Sub TransferDataBasedOnCriteria()
    Dim sh2 As Worksheet, lr As Long, i As Long, fnd As Range, cnt As Long
   
    Set sh2 = Worksheets("Sheet2")
    Application.ScreenUpdating = False
   
    With Worksheets("Consolidated Report")
        If .Cells(3, "A") = "" Then GoTo errHandler
        lr = sh2.Range("A:F").Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
        For i = .Cells(Rows.Count, "A").End(xlUp).Row To 3 Step -1
            Set fnd = Range(sh2.Cells(3, "A"), sh2.Cells(lr, "A")).Find(.Cells(i, "A").Value, , xlValues, xlWhole)
            If Not fnd Is Nothing Then
                cnt = fnd.MergeArea.Count
                If cnt <> 1 Then
                    .Rows(.Cells(i, "A").Offset(1).Row & ":" & .Cells(i, "A").Offset(1).Row + cnt - 2).Insert (xlDown)
                End If
                fnd.MergeArea.Resize(, 6).Copy .Cells(i, "A")
            End If
            Set fnd = Nothing
        Next i
    End With
   
    Application.ScreenUpdating = True
    MsgBox "Done."
   
Exit Sub
errHandler:
    MsgBox "No IDs are in column A on Consolidated Report.", vbExclamation, "Error"
   
End Sub
Works like a charm, the only request remains if there is an empty cell in Column A in "Consolidated Report" (let's say between row 6 and row 7), it has to skip to the other ID1 and not copy row 11 in Sheet2 // can we adjust accordingly? Thanks a lot!
 
Upvote 0
Here:
VBA Code:
Sub TransferDataBasedOnCriteria()
    Dim sh2 As Worksheet, lr As Long, i As Long, fnd As Range, cnt As Long
    
    Set sh2 = Worksheets("Sheet2")
    Application.ScreenUpdating = False
    
    With Worksheets("Consolidated Report")
        If .Cells(3, "A") = "" Then GoTo errHandler
        lr = sh2.Range("A:F").Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
        For i = .Cells(Rows.Count, "A").End(xlUp).Row To 3 Step -1
            If .Cells(i, "A") = "" Then GoTo NextIteration
            Set fnd = Range(sh2.Cells(3, "A"), sh2.Cells(lr, "A")).Find(.Cells(i, "A").Value, , xlValues, xlWhole)
            If Not fnd Is Nothing Then
                cnt = fnd.MergeArea.Count
                If cnt <> 1 Then
                    .Rows(.Cells(i, "A").Offset(1).Row & ":" & .Cells(i, "A").Offset(1).Row + cnt - 2).Insert (xlDown)
                End If
                fnd.MergeArea.Resize(, 6).Copy .Cells(i, "A")
            Else
                GoTo NextIteration
            End If
            Set fnd = Nothing
NextIteration:
        Next i
    End With
    
    Application.ScreenUpdating = True
    MsgBox "Done."
    
Exit Sub
errHandler:
    MsgBox "No IDs are in column A on Consolidated Report.", vbExclamation, "Error"
    
End Sub
 
Upvote 0
Solution
And this may come in handy when you need to reset the table on Consolidated Report:
VBA Code:
Sub ClearTable()
    Dim i As Long
    With Worksheets("Consolidated Report")
        With .Range("A3:F" & Rows.Count)
            .UnMerge
            .ClearContents
        End With
        'Comment out the below 3 lines if you don't need to fill ID1
        For i = 3 To 8
            .Cells(i, "A") = Choose(i, , , "S001", "S004", "S003", "S005", "S006", "S002")
        Next i
    End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,467
Messages
6,124,984
Members
449,201
Latest member
Lunzwe73

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top