Copy rows to another Sheet position

S42k20

New Member
Joined
Jul 12, 2020
Messages
8
Office Version
  1. 2013
Platform
  1. Windows
Hello :)

I am wondering how this can be done:
I want to copy a specific rows from Sheet A into a specific row-position in Sheet B with variables.

I think the idea is simple but I can't solve it:

I am looking in Sheet B for values > 0 through a column ,top down.
If there is a value (e.g. = 4), take this value and copy all rows out of Sheet A with this value in column A under the position where the value = 4 was found in Sheet B ...

Could anybody help here? Thank you really much for helping!!
Best regards
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
How about this:

VBA Code:
Option Explicit

Sub ImportRows()
'https://www.mrexcel.com/board/threads/copy-rows-to-another-sheet-position.1146273/
Dim shtA As Worksheet, shtB As Worksheet
Dim RngA As Range, RngB As Range, d As Range
Dim LRowA As Long, LRowB As Long
Dim i As Double, j As Double

On Error GoTo EH

With Application
    .ScreenUpdating = False
    .DisplayAlerts = False
End With

LRowA = Sheets("SheetA").Cells(Rows.Count, "A").End(xlUp).Row
LRowB = Sheets("SheetB").Cells(Rows.Count, "A").End(xlUp).Row
Set shtA = Sheets("SheetA") 'Amend for your Sheet to extract FROM
Set shtB = Sheets("SheetB") 'Amend for your Sheet to extract TO

'The 1 in the loop below assumes checking in column A, B would be 2, C 3 and so on
For i = LRowB To 1 Step -1
    For j = 1 To LRowA
        If shtA.Cells(j, 1).Value > 0 And shtA.Cells(j, 1).Value = shtB.Cells(i, 1).Value Then
            shtB.Range(i + 1 & ":" & i + 1).Insert Shift:=xlDown
                shtA.Range("A" & j & ":B" & j).Copy shtB.Range("A" & i + 1 & ":B" & i + 1) 'Amend range B column to suit your data set
        End If
    Next j
Next i

EH:
With Application
    .ScreenUpdating = True
    .DisplayAlerts = True
End With

End Sub
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,327
Members
452,635
Latest member
laura12345

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