Copy if match - offset

emukiss10

Board Regular
Joined
Nov 17, 2017
Messages
201
Hello!

VBA needed.

In sheets(5) column "A" every 3rd row (A3, A6, A9 and so on) I have some numbers.
I'd like to search for those numbers in column "F" of sheets(3). (F2:F)
If match I would like to copy contains of cells from column "DG" and "DI" sheets(3) to columns "M" and "N" sheets(5) to the rows that was matched.

Best Regards
W.
 
My mistake again.
Not trying to make excuses for myself but.
It's always easier when you refer to your sheets by name and not number.
Like sheet named "Master" and sheet named "Update"


Try this:
Code:
Sub My_Sub()
'Modified 3-1-18 2:50 AM EST
Application.ScreenUpdating = False
Dim i As Long
Dim c As Range
Dim Lastrow As Long
Dim Lastrowa As Long
Sheets(5).Activate
Lastrow = Cells(Rows.Count, "A").End(xlUp).Row
Lastrowa = Cells(Rows.Count, "F").End(xlUp).Row
    For i = 3 To Lastrow Step 3
        
        For Each c In Range("F2:F" & Lastrowa)
        If c.Value = Cells(i, 1).Value Then
            Sheets(5).Cells(i, "DG").Copy Sheets(3).Cells(i, "M")
            Sheets(5).Cells(i, "DI").Copy Sheets(3).Cells(i, "N")
        End If
        Next
Next
Application.ScreenUpdating = True
End Sub
 
Upvote 0

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
I think we are missing something.. :(

I am very sorry for all the comotion. But we are getting there :) I will keep in mind to use names instead of numbers (in this case lets leave it as it is)

Check every 3rd cell from Sheets("fifth") column "A" against column "F" from Sheets("third")
(two sheets are being compared)
and if value from "fifth" is found in "third" than copy cells DG and DI from "third" to N and M of Fifth
Ofc the same row. If Sheets("fifth").Range("A30") is found in Sheets("third").Range("F822") than copy DG822 and DI822 from "third" to M30 and N30 of "fifth"
 
Upvote 0
You can try this:
Code:
Sub My_Sub()
'Modified 3-1-18 5:35 AM EST
Application.ScreenUpdating = False
Dim i As Long
Dim c As Range
Dim Lastrow As Long
Dim Lastrowa As Long
Sheets(5).Activate
Lastrow = Cells(Rows.Count, "A").End(xlUp).Row
Lastrowa = Sheets(3).Cells(Rows.Count, "F").End(xlUp).Row
    For i = 3 To Lastrow Step 3
        
        For Each c In Sheets(3).Range("F2:F" & Lastrowa)
        If c.Value = Sheets(5).Cells(i, 1).Value Then
            Sheets(5).Cells(i, "DG").Copy Sheets(3).Cells(i, "M")
            Sheets(5).Cells(i, "DI").Copy Sheets(3).Cells(i, "N")
        End If
        Next
Next
Application.ScreenUpdating = True
End Sub
 
Upvote 0
Doesnt work.. I dont know.. Ill try to explain one more time. part of the code is not what I described. (in red)


Code:
Sub My_Sub()
'Modified 3-1-18 5:35 AM EST
Application.ScreenUpdating = False
Dim i As Long
Dim c As Range
Dim Lastrow As Long
Dim Lastrowa As Long
Sheets(5).Activate
Lastrow = Cells(Rows.Count, "A").End(xlUp).Row
Lastrowa = Sheets(3).Cells(Rows.Count, "F").End(xlUp).Row
    For i = 3 To Lastrow Step 3
        
        For Each c In Sheets(3).Range("F2:F" & Lastrowa)
        If c.Value = Sheets(5).Cells(i, 1).Value Then
           [B] Sheets(5).Cells(i, "DG").Copy Sheets(3).Cells(i, "M") - [COLOR=#ff0000]COPY Sheets(3).Cells(i, "DG").Copy Sheets(5).Cells(i, "M")[/COLOR][/B][B][COLOR=#000000]   
             Sheets(5).Cells(i, "DI").Copy Sheets(3).Cells(i, "N")   - [/COLOR][COLOR=#ff0000]COPYSheets(3).Cells(i, "DI").Copy Sheets(5).Cells(i, "N")[/COLOR][/B]
        End If
        Next
Next
Application.ScreenUpdating = True
End Sub

I tryied to make it work but I failed.
 
Last edited:
Upvote 0
In this example

Sheets("FIFTH") A3 (654) was found in Sheets("THIRD") F5 and I need to copy from Sheets("THIRD") DG5 and DI5 to M3 and N3 Sheets("FIFTH")
the same with A6 (159) and so on till the last row.


SHEETS("THIRD")
ABCDEFDGDHDI
1datadatadatadatadata123WHITEdataGREEN
2datadatadatadatadata456CARdataPLANE
3datadatadatadatadata789APPLEdataBANANNA
4datadatadatadatadata987CATdataMOUSE
5datadatadatadatadata654FLYdataDIG
6datadatadatadatadata321HOLEdataSPACE
7datadatadatadatadata159TARdataDIRT
8datadatadatadatadata753BLEACHdataCLEAN
9datadatadatadatadata852WALTZdataPOLONEZ

<tbody>
</tbody>


SHEETS("FIFTH")
ABCMNO
11111datadatadatadatadata
22222datadatadatadatadata
3654datadata..............data
43333datadatadatadatadata
54444datadatadatadatadata
6159datadata..............data
75555datadatadatadatadata
86666datadatadatadatadata

<tbody>
</tbody>
 
Upvote 0

Forum statistics

Threads
1,214,812
Messages
6,121,699
Members
449,048
Latest member
81jamesacct

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