For each row, find the date in other sheet and return some values

most

Board Regular
Joined
Feb 22, 2011
Messages
107
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
  2. Mobile
I need really need some pointers here, what is the best/easiest way to solve this? The code below doesn't work, so many different issues so I'm not sure there are any point for me in pointing them out. =)

For each date in this sheet...
ideh3c.png


...find the date in this sheet return time and type to the first sheet.
ra3kfn.png


Here they come anyway...
Issue#1 - When date is not found I get and Error13 Type mismatch
Issue#2 - For each day it only returns "In", "Out" seems to be overwritten

Code:
Sub MigrateX()
For Each c In Worksheets("Sheet1").Range("A2:A7").Cells
 For Each f In Worksheets("Sheet2").Range("A1:A6").Cells
   If DateValue(c.Value) = DateValue(f.Value) Then  'Find the right date
         If f.Offset(0, 2).Value = "Out" Then       'Find "out"
             c.Offset(0, 1).Value = "Out"           'Apply data
             c.Offset(0, 2).Value = f.Offset(0, 1).Value
             GoTo NextIteration
          Else
             'Nothing
         End If
         If f.Offset(0, 2).Value = "In" Then        'Find "in"
             c.Offset(0, 1).Value = "In"            'Apply data
             c.Offset(0, 2).Value = f.Offset(0, 1).Value
             GoTo NextIteration
          Else
            'Nothing
        End If
    Else
   End If
 Next f
NextIteration:
Next c
End Sub
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
Can you show result in the first sheet
 
Last edited:
Upvote 0
See next code
Code:
Option Explicit


Sub Treat()
Dim InDic   As Object
Set InDic = CreateObject("Scripting.Dictionary")


Dim OutDic   As Object
Set OutDic = CreateObject("Scripting.Dictionary")
Dim I As Integer
Dim T
    
    With Sheets("Sheet2")
        For I = 2 To .Cells(Rows.Count, 1).End(3).Row
            If (.Cells(I, 1) <> "") Then
                If (.Cells(I, 3) = "In") Then
                    InDic.Item(.Cells(I, 1).Value) = .Cells(I, 2).Value
                Else
                    OutDic.Item(.Cells(I, 1).Value) = .Cells(I, 2).Value
                End If
            End If
        Next I
    End With
    With Sheets("Sheet1")
        For I = 2 To .Cells(Rows.Count, 1).End(3).Row
            T = .Cells(I, 1)
            If (T <> "") Then
                If (InDic.exists(T)) Then
                    .Cells(I, 2) = "In"
                    .Cells(I, 3) = InDic.Item(T)
                    InDic.Remove (T)
                Else
                    If (OutDic.exists(T)) Then
                        .Cells(I, 2) = "Out"
                        .Cells(I, 3) = OutDic.Item(T)
                        OutDic.Remove (T)
                    End If
                End If
            End If
        Next I
    End With
End Sub
 
Upvote 0
It worked fine, thanks!
But the example data was created to simplify my issue, now I need to understand your code so I can develop it for my needs. I've been transforming the code to my live data, but it doesn't work, no data is written in sheet 1. I've been breaking down your code I think I understand most of it, except for this line.

Code:
InDic.Item(.Cells(I, 1).Value) = .Cells(I, 2).Value

Put the time into a dictionary item!? I don't understand why it's column 1, the date is stored there.

Here is a more lifelike mock-up
so47xt.png

15gv9rd.png
 
Upvote 0
Solved! Thanks again for the help!

Code:
Option Explicit
Sub Treat()
 Dim InDic   As Object
 Set InDic = CreateObject("Scripting.Dictionary")
 Dim OutDic   As Object
 Set OutDic = CreateObject("Scripting.Dictionary")
 Dim I As Integer
 Dim T
    
    With Sheets("Sheet2")
        For I = 2 To .Cells(Rows.Count, 4).End(3).Row     'do each row until the end, 4 is column
            If (.Cells(I, 4) <> "") Then                  'skip if date doesn't exist
                If (.Cells(I, 2) = "entered") Then
                    InDic.Item(.Cells(I, 4).Value) = .Cells(I, 5).Value 'put time into dict
                Else
                    OutDic.Item(.Cells(I, 4).Value) = .Cells(I, 5).Value
                End If
            End If
        Next I
    End With
    With Sheets("Sheet1")
        For I = 2 To 32
            T = .Cells(I, 2)
                If (InDic.exists(T)) Then
                    If (.Cells(I, 3) = "") Then .Cells(I, 3) = InDic.Item(T)   'Get time from dict, only write if empty
                    InDic.Remove (T)
                End If
                If (OutDic.exists(T)) Then
                    If (.Cells(I, 4) = "") Then .Cells(I, 4) = OutDic.Item(T)
                    OutDic.Remove (T)
                End If
        Next I
    End With
End Sub
 
Upvote 0
Good news, so you understood how it works.
Be free to contact me for any other support
PCL
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,325
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