Horizontal Lookup

SamPaulRoger

New Member
Joined
Jul 28, 2018
Messages
12
Hi,

I have a sheet which contains a data output of all my orders under each date and in another sheet I have a list of all the unique orders. I am trying to do a count of any duplicates that are greater than or equal to today's date. For example if today is 7/27, I want to find all duplicates that start from 7/27 to the end of the data set (Nothing before).

7/247/257/267/277/287/297/307/318/18/2
GEE100ACD107ABC102ABC100DEF501ABC100
DBC100GEE101ABC102DEF501ABC102
ASD104GED104ABC103DEF502ABC103

<tbody>
</tbody>


On sheet2 my list of Pre-Populated ID's and the script output

IDDuplicates
ABC1002
ABC101
DEF5012
DEF5021

<tbody>
</tbody>
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
Try this, copied down.

Excel Workbook
ABCDEFGHIJ
124 Jul25 Jul26 Jul27 Jul28 Jul29 Jul30 Jul31 Jul1 Aug2 Aug
2GEE100ACD107ABC102ABC100DEF501ABC100
3DBC100GEE101ABC102DEF501ABC102
4ASD104GED104ABC103DEF502ABC103
5
6
7
8IDDuplicates
9ABC1002
10ABC1010
11DEF5012
12DEF5021
Count
 
Upvote 0
That worked really well, Is there a way this can be done through VBA with an evaluate based on last row/column? The data set I use changes on a day by day basis and can be as many as 200 columns, but never more than 1000 rows... Another reason I would like this to be in VBA is because this gets distributed to multiple people who have a habit of deleting formulas and then complain their file is broken...
 
Upvote 0
Is there a way this can be done through VBA ...
Good idea to state that up-front if you are looking for a particular method. ;)

Assuming that ..
- original data is on 'Sheet1' starting at cell A1
- 'Sheet2' table also starts at cell A1 with headings in row 1
.. try this on a copy of your workbook.

Code:
Sub CountAfterDate()
  Dim a As Variant, b As Variant
  Dim d As Object
  Dim lr As Long, fc As Long, lc As Long, i As Long, j As Long, cols As Long, ub2 As Long
  
  With Sheets("Sheet1")
    lc = .Cells(1, .Columns.Count).End(xlToLeft).Column
    For i = 1 To lc
      If .Cells(1, i).Value = Date Then
        fc = i
        Exit For
      End If
    Next i
    If fc <> 0 Then
      Set d = CreateObject("Scripting.Dictionary")
      cols = lc - fc + 1
      lr = .Columns(fc).Resize(, cols).Find(What:="*", LookIn:=xlFormulas, SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
      a = .Columns(fc).Resize(, cols).Resize(lr).Value
      ub2 = UBound(a, 2)
      For i = 2 To UBound(a)
        For j = 1 To ub2
          d(a(i, j)) = d(a(i, j)) + 1
        Next j
      Next i
      With Sheets("Sheet2")
        b = .Range("A2", .Range("A" & Rows.Count).End(xlUp)).Resize(, 2).Value
        For i = 1 To UBound(a)
          b(i, 2) = d(b(i, 1))
        Next i
        .Range("A2:B2").Resize(UBound(b)).Value = b
      End With
    End If
  End With
End Sub
 
Upvote 0
VBA code
Code:
Sub Macro1()
Dim LR As Long, LC As Long, TA As Long, TB As Long, DTno As Long, X As Long
Dim Sh1 As Worksheet, Sh2 As Worksheet
Dim celVal
Dim Frng As Range


Set Sh1 = Sheets("Sheet1")
Set Sh2 = Sheets("Sheet2")
Sh1.Activate
Range("A1").Select
LR = Range("A" & Rows.Count).End(xlUp).Row
LC = Range("A1").End(xlToRight).Column
DTno = Date
X = Range("A1:ZZ1").Find(What:=DTno, After:=ActiveCell, LookIn:=xlFormulas, _
        LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
        MatchCase:=False, SearchFormat:=False).Column


Sh2.Range("A1:B1").Value = Array("ID#", "Duplicates")


For TB = X To LC
    For TA = 2 To LR
    celVal = Sh1.Cells(TA, TB).Value
    
    If celVal <> "" Then
    Set Frng = Sh2.Range("A:A").Find(What:=celVal, After:=Sh2.Range("A1"), LookIn:=xlFormulas, _
        LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
        MatchCase:=False, SearchFormat:=False)
    
    If Not Frng Is Nothing Then
    Frng.Offset(0, 1) = Frng.Offset(0, 1) + 1
    Else
    Sh2.Range("A" & Rows.Count).End(xlUp).Offset(1, 0) = celVal
    Sh2.Range("A" & Rows.Count).End(xlUp).Offset(0, 1) = 1
    End If
    End If
    
    Next TA


Next TB
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,518
Messages
6,125,293
Members
449,218
Latest member
Excel Master

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