Hi I have now managed to write my macro but the whole thing stops responding, just a guess but i presume it is because it is asking to check too much data? I have about 3000 rows (but have even tried it on 10). Does anyone know how I can cut this down or mend it to work? Another query on it I have gone to the heading 'sample187' but ideally wanted it to go to 'sample400'?
Thanks
Sub Newmacro()
Dim cn As Object
Dim rs As Object
Dim strFile As String
Dim strCon As String
Dim strSQL As String
Dim strWhere As String
Dim i As Integer
strFile = ActiveWorkbook.FullName
strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & strFile _
& ";Extended Properties=""Excel 8.0;HDR=Yes;IMEX=1"";"
Set cn = CreateObject("ADODB.Connection")
Set rs = CreateObject("ADODB.Recordset")
cn.Open strCon
strSQL = "SELECT a.[A], " _
& "(SELECT Max([Serial]) FROM [Data$] b WHERE b.[A]=a.A) As Serial, " _
& "(SELECT Max([Sample1]) FROM [Data$] b WHERE b.[A]=a.A ) As Sample1, " & "(SELECT Max([Sample2]) FROM [Data$] b WHERE b.[A]=a.A ) As Sample2, " & "(SELECT Max([Sample3]) FROM [Data$] b WHERE b.[A]=a.A ) As Sample3, " & "(SELECT Max([Sample4]) FROM [Data$] b WHERE b.[A]=a.A ) As Sample4, " & "(SELECT Max([Sample5]) FROM [Data$] b WHERE b.[A]=a.A ) As Sample5, " & "(SELECT Max([Sample6]) FROM [Data$] b WHERE b.[A]=a.A ) As Sample6, " & "(SELECT Max([Sample7]) FROM [Data$] b WHERE b.[A]=a.A ) As Sample7, " & "(SELECT Max([Sample8]) FROM [Data$] b WHERE b.[A]=a.A ) As Sample8, " & "(SELECT Max([Sample9]) FROM [Data$] b WHERE b.[A]=a.A ) As Sample9, " & "(SELECT Max([Sample10]) FROM [Data$] b WHERE b.[A]=a.A ) As Sample10, " & "(SELECT Max([Sample11]) FROM [Data$] b WHERE b.[A]=a.A ) As Sample11, " & "(SELECT Max([Sample12]) FROM [Data$] b WHERE b.[A]=a.A ) As Sample12, " & "(SELECT Max([Sample13]) FROM [Data$] b WHERE b.[A]=a.A ) As Sample13, " _
& "(SELECT Max([Sample14]) FROM [Data$] b WHERE b.[A]=a.A ) As Sample14, " & "(SELECT Max([Sample15]) FROM [Data$] b WHERE b.[A]=a.A ) As Sample15, " & "(SELECT Max([Sample16]) FROM [Data$] b WHERE b.[A]=a.A ) As Sample16, " & "(SELECT Max([Sample17]) FROM [Data$] b WHERE b.[A]=a.A ) As Sample17, " & "(SELECT Max([Sample18]) FROM [Data$] b WHERE b.[A]=a.A ) As Sample18, " & "(SELECT Max([Sample19]) FROM [Data$] b WHERE b.[A]=a.A ) As Sample19, " & "(SELECT Max([Sample20]) FROM [Data$] b WHERE b.[A]=a.A ) As Sample20, " & "(SELECT Max([Sample21]) FROM [Data$] b WHERE b.[A]=a.A ) As Sample21, " & "(SELECT Max([Sample22]) FROM [Data$] b WHERE b.[A]=a.A ) As Sample22, " & "(SELECT Max([Sample23]) FROM [Data$] b WHERE b.[A]=a.A ) As Sample23, " & "(SELECT Max([Sample24]) FROM [Data$] b WHERE b.[A]=a.A ) As Sample24, " & "(SELECT Max([Sample25]) FROM [Data$] b WHERE b.[A]=a.A ) As Sample25, " & "(SELECT Max([Sample26]) FROM [Data$] b WHERE b.[A]=a.A ) As Sample26, " _
& "(SELECT Max([Sample27]) FROM [Data$] b WHERE b.[A]=a.A ) As Sample27, " & "(SELECT Max([Sample28]) FROM [Data$] b WHERE b.[A]=a.A ) As Sample28, " & "(SELECT Max([Sample29]) FROM [Data$] b WHERE b.[A]=a.A ) As Sample29, " & "(SELECT Max([Sample30]) FROM [Data$] b WHERE b.[A]=a.A ) As Sample30, " & "(SELECT Max([Sample31]) FROM [Data$] b WHERE b.[A]=a.A ) As Sample31, " & "(SELECT Max([Sample32]) FROM [Data$] b WHERE b.[A]=a.A ) As Sample32, " & "(SELECT Max([Sample33]) FROM [Data$] b WHERE b.[A]=a.A ) As Sample33, " & "(SELECT Max([Sample34]) FROM [Data$] b WHERE b.[A]=a.A ) As Sample34, " & "(SELECT Max([Sample35]) FROM [Data$] b WHERE b.[A]=a.A ) As Sample35, " & "(SELECT Max([Sample36]) FROM [Data$] b WHERE b.[A]=a.A ) As Sample36, " & "(SELECT Max([Sample37]) FROM [Data$] b WHERE b.[A]=a.A ) As Sample37, " & "(SELECT Max([Sample38]) FROM [Data$] b WHERE b.[A]=a.A ) As Sample38, " & "(SELECT Max([Sample39]) FROM [Data$] b WHERE b.[A]=a.A ) As Sample39, " _
& "(SELECT Max([Sample40]) FROM [Data$] b WHERE b.[A]=a.A ) As Sample40, " & "(SELECT Max([Sample41]) FROM [Data$] b WHERE b.[A]=a.A ) As Sample41, " & "(SELECT Max([Sample42]) FROM [Data$] b WHERE b.[A]=a.A ) As Sample42, " & "(SELECT Max([Sample43]) FROM [Data$] b WHERE b.[A]=a.A ) As Sample43, " & "(SELECT Max([Sample44]) FROM [Data$] b WHERE b.[A]=a.A ) As Sample44, " & "(SELECT Max([Sample45]) FROM [Data$] b WHERE b.[A]=a.A ) As Sample45, " & "(SELECT Max([Sample46]) FROM [Data$] b WHERE b.[A]=a.A ) As Sample46, " & "(SELECT Max([Sample47]) FROM [Data$] b WHERE b.[A]=a.A ) As Sample47, " & "(SELECT Max([Sample48]) FROM [Data$] b WHERE b.[A]=a.A ) As Sample48, " & "(SELECT Max([Sample49]) FROM [Data$] b WHERE b.[A]=a.A ) As Sample49, " & "(SELECT Max([Sample50]) FROM [Data$] b WHERE b.[A]=a.A ) As Sample50, " & "(SELECT Max([Sample51]) FROM [Data$] b WHERE b.[A]=a.A ) As Sample51, " & "(SELECT Max([Sample52]) FROM [Data$] b WHERE b.[A]=a.A ) As Sample52, " _
& "(SELECT Max([Sample53]) FROM [Data$] b WHERE b.[A]=a.A ) As Sample53, " & "(SELECT Max([Sample54]) FROM [Data$] b WHERE b.[A]=a.A ) As Sample54, " & "(SELECT Max([Sample55]) FROM [Data$] b WHERE b.[A]=a.A ) As Sample55, " & "(SELECT Max([Sample56]) FROM [Data$] b WHERE b.[A]=a.A ) As Sample56, " & "(SELECT Max([Sample57]) FROM [Data$] b WHERE b.[A]=a.A ) As Sample57, " & "(SELECT Max([Sample58]) FROM [Data$] b WHERE b.[A]=a.A ) As Sample58, " & "(SELECT Max([Sample59]) FROM [Data$] b WHERE b.[A]=a.A ) As Sample59, " & "(SELECT Max([Sample60]) FROM [Data$] b WHERE b.[A]=a.A ) As Sample60, " & "(SELECT Max([Sample61]) FROM [Data$] b WHERE b.[A]=a.A ) As Sample61, " & "(SELECT Max([Sample62]) FROM [Data$] b WHERE b.[A]=a.A ) As Sample62, " & "(SELECT Max([Sample63]) FROM [Data$] b WHERE b.[A]=a.A ) As Sample63, " & "(SELECT Max([Sample64]) FROM [Data$] b WHERE b.[A]=a.A ) As Sample64, " & "(SELECT Max([Sample65]) FROM [Data$] b WHERE b.[A]=a.A ) As Sample65, " _
& "(SELECT Max([Sample66]) FROM [Data$] b WHERE b.[A]=a.A ) As Sample66, " & "(SELECT Max([Sample67]) FROM [Data$] b WHERE b.[A]=a.A ) As Sample67, " & "(SELECT Max([Sample68]) FROM [Data$] b WHERE b.[A]=a.A ) As Sample68, " & "(SELECT Max([Sample69]) FROM [Data$] b WHERE b.[A]=a.A ) As Sample69, " & "(SELECT Max([Sample70]) FROM [Data$] b WHERE b.[A]=a.A ) As Sample70, " & "(SELECT Max([Sample71]) FROM [Data$] b WHERE b.[A]=a.A ) As Sample71, " & "(SELECT Max([Sample72]) FROM [Data$] b WHERE b.[A]=a.A ) As Sample72, " & "(SELECT Max([Sample73]) FROM [Data$] b WHERE b.[A]=a.A ) As Sample73, " & "(SELECT Max([Sample74]) FROM [Data$] b WHERE b.[A]=a.A ) As Sample74, " & "(SELECT Max([Sample75]) FROM [Data$] b WHERE b.[A]=a.A ) As Sample75, " & "(SELECT Max([Sample76]) FROM [Data$] b WHERE b.[A]=a.A ) As Sample76, " & "(SELECT Max([Sample77]) FROM [Data$] b WHERE b.[A]=a.A ) As Sample77, " & "(SELECT Max([Sample78]) FROM [Data$] b WHERE b.[A]=a.A ) As Sample78, " _
& "(SELECT Max([Sample79]) FROM [Data$] b WHERE b.[A]=a.A ) As Sample79, " & "(SELECT Max([Sample80]) FROM [Data$] b WHERE b.[A]=a.A ) As Sample80, " & "(SELECT Max([Sample81]) FROM [Data$] b WHERE b.[A]=a.A ) As Sample81, " & "(SELECT Max([Sample82]) FROM [Data$] b WHERE b.[A]=a.A ) As Sample82, " & "(SELECT Max([Sample83]) FROM [Data$] b WHERE b.[A]=a.A ) As Sample83, " & "(SELECT Max([Sample84]) FROM [Data$] b WHERE b.[A]=a.A ) As Sample84, " & "(SELECT Max([Sample85]) FROM [Data$] b WHERE b.[A]=a.A ) As Sample85, " & "(SELECT Max([Sample86]) FROM [Data$] b WHERE b.[A]=a.A ) As Sample86, " & "(SELECT Max([Sample87]) FROM [Data$] b WHERE b.[A]=a.A ) As Sample87, " & "(SELECT Max([Sample88]) FROM [Data$] b WHERE b.[A]=a.A ) As Sample88, " & "(SELECT Max([Sample89]) FROM [Data$] b WHERE b.[A]=a.A ) As Sample89, " & "(SELECT Max([Sample90]) FROM [Data$] b WHERE b.[A]=a.A ) As Sample90, " & "(SELECT Max([Sample91]) FROM [Data$] b WHERE b.[A]=a.A ) As Sample91, " _
& "(SELECT Max([Sample92]) FROM [Data$] b WHERE b.[A]=a.A ) As Sample92, " & "(SELECT Max([Sample93]) FROM [Data$] b WHERE b.[A]=a.A ) As Sample93, " & "(SELECT Max([Sample94]) FROM [Data$] b WHERE b.[A]=a.A ) As Sample94, " & "(SELECT Max([Sample95]) FROM [Data$] b WHERE b.[A]=a.A ) As Sample95, " & "(SELECT Max([Sample96]) FROM [Data$] b WHERE b.[A]=a.A ) As Sample96, " & "(SELECT Max([Sample97]) FROM [Data$] b WHERE b.[A]=a.A ) As Sample97, " & "(SELECT Max([Sample98]) FROM [Data$] b WHERE b.[A]=a.A ) As Sample98, " & "(SELECT Max([Sample99]) FROM [Data$] b WHERE b.[A]=a.A ) As Sample99, " & "(SELECT Max([Sample100]) FROM [Data$] b WHERE b.[A]=a.A ) As Sample100, " & "(SELECT Max([Sample101]) FROM [Data$] b WHERE b.[A]=a.A ) As Sample101, " & "(SELECT Max([Sample102]) FROM [Data$] b WHERE b.[A]=a.A ) As Sample102, " & "(SELECT Max([Sample103]) FROM [Data$] b WHERE b.[A]=a.A ) As Sample103, " _
& "(SELECT Max([Sample104]) FROM [Data$] b WHERE b.[A]=a.A ) As Sample104, " & "(SELECT Max([Sample105]) FROM [Data$] b WHERE b.[A]=a.A ) As Sample105, " & "(SELECT Max([Sample106]) FROM [Data$] b WHERE b.[A]=a.A ) As Sample106, " & "(SELECT Max([Sample107]) FROM [Data$] b WHERE b.[A]=a.A ) As Sample107, " & "(SELECT Max([Sample108]) FROM [Data$] b WHERE b.[A]=a.A ) As Sample108, " & "(SELECT Max([Sample109]) FROM [Data$] b WHERE b.[A]=a.A ) As Sample109, " & "(SELECT Max([Sample110]) FROM [Data$] b WHERE b.[A]=a.A ) As Sample110, " & "(SELECT Max([Sample111]) FROM [Data$] b WHERE b.[A]=a.A ) As Sample111, " & "(SELECT Max([Sample112]) FROM [Data$] b WHERE b.[A]=a.A ) As Sample112, " & "(SELECT Max([Sample113]) FROM [Data$] b WHERE b.[A]=a.A ) As Sample113, " & "(SELECT Max([Sample114]) FROM [Data$] b WHERE b.[A]=a.A ) As Sample114, " & "(SELECT Max([Sample115]) FROM [Data$] b WHERE b.[A]=a.A ) As Sample115, " _
& "(SELECT Max([Sample116]) FROM [Data$] b WHERE b.[A]=a.A ) As Sample116, " & "(SELECT Max([Sample117]) FROM [Data$] b WHERE b.[A]=a.A ) As Sample117, " & "(SELECT Max([Sample118]) FROM [Data$] b WHERE b.[A]=a.A ) As Sample118, " & "(SELECT Max([Sample119]) FROM [Data$] b WHERE b.[A]=a.A ) As Sample119, " & "(SELECT Max([Sample120]) FROM [Data$] b WHERE b.[A]=a.A ) As Sample120, " & "(SELECT Max([Sample121]) FROM [Data$] b WHERE b.[A]=a.A ) As Sample121, " & "(SELECT Max([Sample122]) FROM [Data$] b WHERE b.[A]=a.A ) As Sample122, " & "(SELECT Max([Sample123]) FROM [Data$] b WHERE b.[A]=a.A ) As Sample123, " & "(SELECT Max([Sample124]) FROM [Data$] b WHERE b.[A]=a.A ) As Sample124, " & "(SELECT Max([Sample125]) FROM [Data$] b WHERE b.[A]=a.A ) As Sample125, " & "(SELECT Max([Sample126]) FROM [Data$] b WHERE b.[A]=a.A ) As Sample126, " & "(SELECT Max([Sample127]) FROM [Data$] b WHERE b.[A]=a.A ) As Sample127, " _
& "(SELECT Max([Sample128]) FROM [Data$] b WHERE b.[A]=a.A ) As Sample128, " & "(SELECT Max([Sample129]) FROM [Data$] b WHERE b.[A]=a.A ) As Sample129, " & "(SELECT Max([Sample130]) FROM [Data$] b WHERE b.[A]=a.A ) As Sample130, " & "(SELECT Max([Sample131]) FROM [Data$] b WHERE b.[A]=a.A ) As Sample131, " & "(SELECT Max([Sample132]) FROM [Data$] b WHERE b.[A]=a.A ) As Sample132, " & "(SELECT Max([Sample133]) FROM [Data$] b WHERE b.[A]=a.A ) As Sample133, " & "(SELECT Max([Sample134]) FROM [Data$] b WHERE b.[A]=a.A ) As Sample134, " & "(SELECT Max([Sample135]) FROM [Data$] b WHERE b.[A]=a.A ) As Sample135, " & "(SELECT Max([Sample136]) FROM [Data$] b WHERE b.[A]=a.A ) As Sample136, " & "(SELECT Max([Sample137]) FROM [Data$] b WHERE b.[A]=a.A ) As Sample137, " & "(SELECT Max([Sample138]) FROM [Data$] b WHERE b.[A]=a.A ) As Sample138, " & "(SELECT Max([Sample139]) FROM [Data$] b WHERE b.[A]=a.A ) As Sample139, " _
& "(SELECT Max([Sample140]) FROM [Data$] b WHERE b.[A]=a.A ) As Sample140, " & "(SELECT Max([Sample141]) FROM [Data$] b WHERE b.[A]=a.A ) As Sample141, " & "(SELECT Max([Sample142]) FROM [Data$] b WHERE b.[A]=a.A ) As Sample142, " & "(SELECT Max([Sample143]) FROM [Data$] b WHERE b.[A]=a.A ) As Sample143, " & "(SELECT Max([Sample144]) FROM [Data$] b WHERE b.[A]=a.A ) As Sample144, " & "(SELECT Max([Sample145]) FROM [Data$] b WHERE b.[A]=a.A ) As Sample145, " & "(SELECT Max([Sample146]) FROM [Data$] b WHERE b.[A]=a.A ) As Sample146, " & "(SELECT Max([Sample147]) FROM [Data$] b WHERE b.[A]=a.A ) As Sample147, " & "(SELECT Max([Sample148]) FROM [Data$] b WHERE b.[A]=a.A ) As Sample148, " & "(SELECT Max([Sample149]) FROM [Data$] b WHERE b.[A]=a.A ) As Sample149, " & "(SELECT Max([Sample150]) FROM [Data$] b WHERE b.[A]=a.A ) As Sample150, " & "(SELECT Max([Sample151]) FROM [Data$] b WHERE b.[A]=a.A ) As Sample151, " _
& "(SELECT Max([Sample152]) FROM [Data$] b WHERE b.[A]=a.A ) As Sample152, " & "(SELECT Max([Sample153]) FROM [Data$] b WHERE b.[A]=a.A ) As Sample153, " & "(SELECT Max([Sample154]) FROM [Data$] b WHERE b.[A]=a.A ) As Sample154, " & "(SELECT Max([Sample155]) FROM [Data$] b WHERE b.[A]=a.A ) As Sample155, " & "(SELECT Max([Sample156]) FROM [Data$] b WHERE b.[A]=a.A ) As Sample156, " & "(SELECT Max([Sample157]) FROM [Data$] b WHERE b.[A]=a.A ) As Sample157, " & "(SELECT Max([Sample158]) FROM [Data$] b WHERE b.[A]=a.A ) As Sample158, " & "(SELECT Max([Sample159]) FROM [Data$] b WHERE b.[A]=a.A ) As Sample159, " & "(SELECT Max([Sample160]) FROM [Data$] b WHERE b.[A]=a.A ) As Sample160, " & "(SELECT Max([Sample161]) FROM [Data$] b WHERE b.[A]=a.A ) As Sample161, " & "(SELECT Max([Sample162]) FROM [Data$] b WHERE b.[A]=a.A ) As Sample162, " & "(SELECT Max([Sample163]) FROM [Data$] b WHERE b.[A]=a.A ) As Sample163, " _
& "(SELECT Max([Sample164]) FROM [Data$] b WHERE b.[A]=a.A ) As Sample164, " & "(SELECT Max([Sample165]) FROM [Data$] b WHERE b.[A]=a.A ) As Sample165, " & "(SELECT Max([Sample166]) FROM [Data$] b WHERE b.[A]=a.A ) As Sample166, " & "(SELECT Max([Sample167]) FROM [Data$] b WHERE b.[A]=a.A ) As Sample167, " & "(SELECT Max([Sample168]) FROM [Data$] b WHERE b.[A]=a.A ) As Sample168, " & "(SELECT Max([Sample169]) FROM [Data$] b WHERE b.[A]=a.A ) As Sample169, " & "(SELECT Max([Sample170]) FROM [Data$] b WHERE b.[A]=a.A ) As Sample170, " & "(SELECT Max([Sample171]) FROM [Data$] b WHERE b.[A]=a.A ) As Sample171, " & "(SELECT Max([Sample172]) FROM [Data$] b WHERE b.[A]=a.A ) As Sample172, " & "(SELECT Max([Sample173]) FROM [Data$] b WHERE b.[A]=a.A ) As Sample173, " & "(SELECT Max([Sample174]) FROM [Data$] b WHERE b.[A]=a.A ) As Sample174, " & "(SELECT Max([Sample175]) FROM [Data$] b WHERE b.[A]=a.A ) As Sample175, " _
& "(SELECT Max([Sample176]) FROM [Data$] b WHERE b.[A]=a.A ) As Sample176, " & "(SELECT Max([Sample177]) FROM [Data$] b WHERE b.[A]=a.A ) As Sample177, " & "(SELECT Max([Sample178]) FROM [Data$] b WHERE b.[A]=a.A ) As Sample178, " & "(SELECT Max([Sample179]) FROM [Data$] b WHERE b.[A]=a.A ) As Sample179, " & "(SELECT Max([Sample180]) FROM [Data$] b WHERE b.[A]=a.A ) As Sample180, " & "(SELECT Max([Sample181]) FROM [Data$] b WHERE b.[A]=a.A ) As Sample181, " & "(SELECT Max([Sample182]) FROM [Data$] b WHERE b.[A]=a.A ) As Sample182, " & "(SELECT Max([Sample183]) FROM [Data$] b WHERE b.[A]=a.A ) As Sample183, " & "(SELECT Max([Sample184]) FROM [Data$] b WHERE b.[A]=a.A ) As Sample184, " & "(SELECT Max([Sample185]) FROM [Data$] b WHERE b.[A]=a.A ) As Sample185, " & "(SELECT Max([Sample186]) FROM [Data$] b WHERE b.[A]=a.A ) As Sample186, " & "(SELECT Max([Sample187]) FROM [Data$] b WHERE b.[A]=a.A ) As Sample187 " _
& "FROM [Data$] a " _
& "GROUP BY a.[A]"
rs.Open strSQL, cn, 3, 3
For i = 0 To rs.fields.Count - 1
Sheets("Duplicated").Cells(1, i + 2) = rs.fields(i).Name
Next
Worksheets("Duplicated").Cells(2, 2).CopyFromRecordset rs
Sheets("Duplicated").Select
End Sub