Excel vba to transfer value to another sheet based on date and number

ExcelNewbie2020

Active Member
Joined
Dec 3, 2020
Messages
333
Office Version
  1. 365
Platform
  1. Windows
i have a task to update the attendance daily (sheet 1). i was thinking of preparing a summary on sheet 2 then will auto-update it to sheet 1 with a button.

would it be possible to transfer the value from sheet 2 to sheet 1 based on the date and emp. #?. while those not included in the update will just carry over based on previous value..



testing.xlsx
ABCDEFGHIJKLMNOPQRSTUV
1SHEET 1SHEET 2
2
3NAMEEMP.#22-02-2323-02-2324-02-2325-02-2326-02-2327-02-2328-02-2301-03-2302-03-23EMP.#DATE24-02-2325-02-2326-02-2327-02-2328-02-2301-03-2302-03-23
4NAME1101FFFFFFFFF10224-02-23T888888
5NAME2102VVT88888810724-02-23T888888
6NAME3103FFFFFFFFF11124-02-23T888888
7NAME410488888888811424-02-23T888888
8NAME5105888888888
9NAME6106888888888
10NAME7107FFT888888
11NAME8108FFFFFFFFF
12NAME9109FFFFFFFFF
13NAME10110FFFFFFFFF
14NAME11111SST888888
15NAME12112VVVVVVVVV
16NAME13113EEEEEEEEE
17NAME14114FFT888888
18NAME15115888888888
19
20
21
22
Sheet28
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
Maybe something like:
VBA Code:
Sub test()
    Dim fRng As Range, fVar As Variant, f As Long
    Dim tRng As Range, tVar As Variant, t As Long
    Dim x As Long, c As Long, tmp As Date, z As Long
    
    Set fRng = Sheet2.UsedRange
    With fRng
        Set fRng = .Offset(1).Resize(.Rows.Count - 1)
    End With
    fVar = fRng.Value
    
    Set tRng = Sheet1.UsedRange
    tVar = tRng.Value
    z = 3
    
    tmp = Sheet2.Range("B2")
    For x = 3 To UBound(tVar, 2)
        If tVar(1, x) = tmp Then
            c = x: Exit For
        End If
    Next x
    
    For t = 2 To UBound(tVar)
        For f = 1 To UBound(fVar)
            If tVar(t, 2) = fVar(f, 1) Then
                For x = c To c + 6
                    tVar(t, x) = fVar(f, z): z = z + 1
                Next x
                z = 3
                Exit For
            Else
                For x = c To c + 6
                    tVar(t, x) = tVar(t, x - 1)
                Next x
            End If
        Next f
    Next t
    tRng = tVar
End Sub
 
Upvote 0
Or maybe just a range loop:
VBA Code:
Sub test()
    Dim dt As Date
    Dim rCell As Range
    Dim c As Long
    Dim fRng As Range
   
    dt = Sheet2.Range("B2")
    c = Sheet1.Range("A1:XFD1").Find(dt).Column - 2
   
    For Each rCell In Sheet1.Range("B2:B" & Sheet1.Range("B" & Rows.Count).End(xlUp).Row).Cells
        Set fRng = Sheet2.Range("A:A").Find(rCell.Value)
        If Not fRng Is Nothing Then
            rCell.Offset(, c).Resize(, 7).Value = fRng.Offset(, 2).Resize(, 7).Value
        Else
            rCell.Offset(, c).Resize(, 7).Value = rCell.Offset(, c - 1).Value
        End If
        Set fRng = Nothing
    Next rCell
End Sub
 
Upvote 0
Solution
Or maybe just a range loop:
VBA Code:
Sub test()
    Dim dt As Date
    Dim rCell As Range
    Dim c As Long
    Dim fRng As Range
  
    dt = Sheet2.Range("B2")
    c = Sheet1.Range("A1:XFD1").Find(dt).Column - 2
  
    For Each rCell In Sheet1.Range("B2:B" & Sheet1.Range("B" & Rows.Count).End(xlUp).Row).Cells
        Set fRng = Sheet2.Range("A:A").Find(rCell.Value)
        If Not fRng Is Nothing Then
            rCell.Offset(, c).Resize(, 7).Value = fRng.Offset(, 2).Resize(, 7).Value
        Else
            rCell.Offset(, c).Resize(, 7).Value = rCell.Offset(, c - 1).Value
        End If
        Set fRng = Nothing
    Next rCell
End Sub
thanks man, Both work in the sample file, but this one is easier to understand when i update the script with my actual file.. I really appreciate your effort sir..
 
Upvote 0
Or maybe just a range loop:
VBA Code:
Sub test()
    Dim dt As Date
    Dim rCell As Range
    Dim c As Long
    Dim fRng As Range
  
    dt = Sheet2.Range("B2")
    c = Sheet1.Range("A1:XFD1").Find(dt).Column - 2
  
    For Each rCell In Sheet1.Range("B2:B" & Sheet1.Range("B" & Rows.Count).End(xlUp).Row).Cells
        Set fRng = Sheet2.Range("A:A").Find(rCell.Value)
        If Not fRng Is Nothing Then
            rCell.Offset(, c).Resize(, 7).Value = fRng.Offset(, 2).Resize(, 7).Value
        Else
            rCell.Offset(, c).Resize(, 7).Value = rCell.Offset(, c - 1).Value
        End If
        Set fRng = Nothing
    Next rCell
End Sub
sir, the code is working fine..however, I encounter issues on my data.. i mentioned before that if the EMP# does not exist in sheet 2 the script will just copy the values from the previous day of the sheet 1.. I found out that this will affect the previously updated EMP#..

1677650542140.png


Sir, would it be possible to add another line in the script that it should ignore the copy of previous value if the previous value is "T". thank you very much..
 
Upvote 0
Like this?
VBA Code:
Sub test()
    Dim dt As Date
    Dim rCell As Range
    Dim c As Long
    Dim fRng As Range
  
    dt = Sheet2.Range("B2")
    c = Sheet1.Range("A1:XFD1").Find(dt).Column - 2
  
    For Each rCell In Sheet1.Range("B2:B" & Sheet1.Range("B" & Rows.Count).End(xlUp).Row).Cells
        Set fRng = Sheet2.Range("A:A").Find(rCell.Value)
        If Not fRng Is Nothing Then
            rCell.Offset(, c).Resize(, 7).Value = fRng.Offset(, 2).Resize(, 7).Value
        Else
            If rCell.Offset(, c - 1).Value <> "T" Then
                rCell.Offset(, c).Resize(, 7).Value = rCell.Offset(, c - 1).Value
            End If
        End If
        Set fRng = Nothing
    Next rCell
End Sub
 
Upvote 1
Like this?
VBA Code:
Sub test()
    Dim dt As Date
    Dim rCell As Range
    Dim c As Long
    Dim fRng As Range
 
    dt = Sheet2.Range("B2")
    c = Sheet1.Range("A1:XFD1").Find(dt).Column - 2
 
    For Each rCell In Sheet1.Range("B2:B" & Sheet1.Range("B" & Rows.Count).End(xlUp).Row).Cells
        Set fRng = Sheet2.Range("A:A").Find(rCell.Value)
        If Not fRng Is Nothing Then
            rCell.Offset(, c).Resize(, 7).Value = fRng.Offset(, 2).Resize(, 7).Value
        Else
            If rCell.Offset(, c - 1).Value <> "T" Then
                rCell.Offset(, c).Resize(, 7).Value = rCell.Offset(, c - 1).Value
            End If
        End If
        Set fRng = Nothing
    Next rCell
End Sub
it works like a charm.. thank you sir
 
Upvote 0

Forum statistics

Threads
1,223,227
Messages
6,170,848
Members
452,361
Latest member
d3ad3y3

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