[VBA] set range find() -> finds wrong value

dunnobe

New Member
Joined
Jan 24, 2018
Messages
2
Hey all,

My first time posting on this forum. :)
I'm troubled by an annoying problem in my code.

My goal is to transpose some data from a vertical to a horizontal structure. A date is looked up in a datasheet and everytime it finds that date, it should copy a value next to it to my summary sheet. If it finds the date 2x, it should copy 2 cells etc.

Problem

The loop does something wrong when looking up dates. It tries to find a specific date, but copies the cells of a similar data.

Example of problem

It looks up the value 15/01/2016 from ws Summary in wsTime -> finds it -> copies a value (c(, 2).Copy) -> next find
It looks up the value 15/01/2016 from ws Summary in wsTime -> finds it -> copies a value (c(, 2).Copy) -> next find

-> should no longer find 15/01/2016 and go to next find (16/01/2016)
-> but problem occurs

It looks up the value 15/01/2016 from ws Summary in wsTime -> finds 15/11/2016 -> copies a value (c(, 2).Copy) -> next find
It looks up the value 15/01/2016 from ws Summary in wsTime -> finds 15/11/2016 -> copies a value (c(, 2).Copy) -> next find
It looks up the value 15/01/2016 from ws Summary in wsTime -> finds 15/11/2016 -> copies a value (c(, 2).Copy) -> next find
It looks up the value 15/01/2016 from ws Summary in wsTime -> finds 15/11/2016 -> copies a value (c(, 2).Copy) -> next find

->It no longer finds 15/01/2016 and goes to next find (16/01/2016)

It looks up the value 16/01/2016 from ws Summary in wsTime-> finds it...

Ironically it works fine when looking up and copying 15/11/2016... the problem only occurs if the data starts in January (01) or February (02) AND when the sheet also has data for November (11) and December (12).

Rich (BB code):
' Create the data for column C till last in wsSummary
' ---------------------------------------------------
    Dim rng1 As Range, rng2 As Range, rng3 As Range, r As Range, c As Range, d As Range
    Dim ff As String, gg As String
    Set rng1 = wsSummary.Cells(1).CurrentRegion
    Set rng2 = wsTime.Cells(1).CurrentRegion.Columns("f")
    Set rng3 = wsPlanification.Cells(2).CurrentRegion.Columns("g")
    rng1.Offset(1, 4).ClearContents
    For Each r In rng1.Columns(1).Cells
        If IsDate(r.Value) Then
            Set d = rng3.Find(r.Value, , xlFormulas)
            If Not d Is Nothing Then
                    d(, 8).Copy r.Offset(, 2)
                    d(, 3).Copy r.Offset(, 3)
            Else
                r(, 3).Resize(, 2) = 0
            End If
            Set c = rng2.Find(r.Value, , xlFormulas)
            If Not c Is Nothing Then
                ff = c.Address
                Do
                    r(, 5) = r(, 5) + 1
'
'                    c(, 2).Copy r.Offset(, r(, 5) * 5)
'                    c(, 4).Copy r.Offset(, r(, 5) * 5 + 1)
'                    c(, 6).Copy r.Offset(, r(, 5) * 5 + 2)
'                    c(, 7).Copy r.Offset(, r(, 5) * 5 + 3)
'                    c(, 13).Copy r.Offset(, r(, 5) * 5 + 4)
                   
                   Union(c(, 2), c(, 4), c(, 6), c(, 7), c(, 13)).Copy r.Offset(, r(, 5) * 5)
                    Set c = rng2.FindNext(c)
                Loop Until c.Address = ff
            Else
                r(, 5) = 0
            End If
        End If
    Next


PS: special thanks to those who helped me make this code in the past. I'm quite new to VBA and I'm quite clueless right now...

Thanks!!!
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
The topic can be closed. :)

Set c = rng2.Find(r.Value, , xlFormulas, xlwhole)lookat:=xlWhole made the code look for exact matches.

Best regards,

dunnobe
 
Upvote 0

Forum statistics

Threads
1,215,063
Messages
6,122,927
Members
449,094
Latest member
teemeren

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