VBA Loop Move specific data from one sheet to another

lfcreds11

New Member
Joined
Oct 12, 2009
Messages
29
Hi all,

I am trying to write a script to lookup and copy specific info from one worksheet to another if certain criteria matches.

Worksheet A is called "Absent" and has 8 columns with A1 called CM

CM ID Number Date Shift Position Reason Hours PTO Hours


Worksheet B is called "Data" and has 8 columns with A1 called Date

Date CM Position Status Termination Reason Absent Reason Hours PTO Hours

I would like to do the following:

1- have the script look for matching names and dates
2-If both match, copy from the "Absent" worksheet data in column F (reason), column G (Hours) and column H (PTO hours) to the matching rows in worksheet "Data"
3-each time the script is run to refresh any new information

Notes:

Worksheet Data currently has 29,459 rows of data
Worksheet Absent currently has 1,781 rows of data


Thank you all!
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
See if this will work for you. The code should be run from the standard module1.

Code:
Sub t()
Dim sh1 As Worksheet, sh2 As Worksheet, fn As Range, c As Range, adr As String
Set sh1 = Sheets("Absent")
Set sh2 = Sheets("Data")
    With sh1
        For Each c In .Range("A2", .Cells(Rows.Count, 1).End(xlUp))
            Set fn = sh2.Range("B:B").Find(c.Value, , xlValues, xlWhole)
                If Not c Is Nothing Then
                    adr = fn.Address
                    Do
                        If c.Offset(, 2).Value = fn.Offset(, -1).Value Then
                            fn.Offset(, 4).Resize(, 3).Copy c.Offset(, 5)
                            Exit Do
                        End If
                        Set fn = sh2.Range("B:B").FindNext(fn)
                    Loop While adr <> fn.Address
                End If
        Next
    End With
End Sub
 
Upvote 0
Thank you for your quick reply. I ran into one error

adr = fn.Address

I receive the run-time error '91'
Object variable or With block variable not set
 
Upvote 0
Had a typo in there. This should run better.

Code:
Sub t2()
Dim sh1 As Worksheet, sh2 As Worksheet, fn As Range, c As Range, adr As String
Set sh1 = Sheets("Absent")
Set sh2 = Sheets("Data")
    With sh1
        For Each c In .Range("A2", .Cells(Rows.Count, 1).End(xlUp))
            Set fn = sh2.Range("B:B").Find(c.Value, , xlValues, xlWhole)
                If Not fn Is Nothing Then
                    adr = fn.Address
                    Do
                        If c.Offset(, 2).Value = fn.Offset(, -1).Value Then
                            fn.Offset(, 4).Resize(, 3).Copy c.Offset(, 5)
                            Exit Do
                        End If
                        Set fn = sh2.Range("B:B").FindNext(fn)
                    Loop While adr <> fn.Address
                End If
        Next
    End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,908
Messages
6,175,307
Members
452,633
Latest member
DougMo

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