以下是引用office2008在2008-9-6 12:09:00的发言: 第一题:网上的方法 一知半解,那位高手有心人能代码解说一下。谢谢! Sub yy15() With Worksheets("行列转换2") Set CNN = CreateObject("adodb.connection") CNN.Open "Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties='Excel 8.0;hdr=yes;';Data Source=" & ThisWorkbook.FullName .Range("e2:n10").ClearContents r = .Range("a65536").End(xlUp).Row SQL = " select f1 ,max(iif(mid(f2,2,2)=1,f2,'')) as t1," _ & " max(iif(mid(f2,2,2)=2,f2,'')) as t2," _ & " max(iif(mid(f2,2,2)=3,f2,'')) as t3," _ & " max(iif(mid(f2,2,2)=4,f2,'')) as t4," _ & " max(iif(mid(f2,2,2)=5,f2,'')) as t5," _ & " max(iif(mid(f2,2,2)=6,f2,'')) as t6," _ & " max(iif(mid(f2,2,2)=7,f2,'')) as t7" _ & " from (" _ & " select t.* , (select count(1) from [行列转换2$a1:b" & r & "] " _ & " where f1 = t.f1 and f2 < t.f2) + 1 from [行列转换2$a1:b" & r & "] t ) m" _ & " group by f1 " .Range("e2").CopyFromRecordset CNN.Execute(SQL) End With End Sub
晕,楼上的代码不就是简单的对号如座吗,跟下面的一样,觉得这种思路有点简单, Sql = " select f1 ,max(iif(mid(f2,2,2)=1,f2,'')) as t1," _ & " max(iif(mid(f2,2,2)=2,f2,'')) as t2," _ & " max(iif(mid(f2,2,2)=3,f2,'')) as t3," _ & " max(iif(mid(f2,2,2)=4,f2,'')) as t4," _ & " max(iif(mid(f2,2,2)=5,f2,'')) as t5," _ & " max(iif(mid(f2,2,2)=6,f2,'')) as t6," _ & " max(iif(mid(f2,2,2)=7,f2,'')) as t7" _ & " from " _ & " [行列转换2$a1:b" & r & "] " _ & " group by f1 " |