- ©«¤l
- 79
- ¥DÃD
- 2
- ºëµØ
- 0
- ¿n¤À
- 193
- ÂI¦W
- 0
- §@·~¨t²Î
- Winwos 7 64 bits
- ³nÅ骩¥»
- Excel 2003/2007
- ¾\ŪÅv
- 20
- ©Ê§O
- ¨k
- ¨Ó¦Û
- TAIPEI
- µù¥U®É¶¡
- 2010-8-25
- ³Ì«áµn¿ý
- 2019-9-20
|
¤u¨ã¡÷³]©w¤Þ¥Î¶µ¥Ø¡÷Microsoft ActiveX Data Objects 2.8 Library
¥Î SQL¨Ó¸Ñ¨M¤éÂà¶g¡BÂà¤ë¡BÂà©u......ÂàÀÉ°ÝÃD- Sub ¤é½uÂà¶g½u()
- '«Ø¥ß¤é´Á»P¦~¶g¹ï·Ó¦r¨åÀÉ
- Dim d
- Set d = CreateObject("Scripting.Dictionary")
- Dim c As Range
- For Each c In Sheets("¤é»ù®æ").Range("A2:A" & Sheets("¤é»ù®æ").[A2].End(xlDown).Row)
-
- '±N¤é´ÁÂର¦~¶g¡A¨Ò¦p201215ªí¥Ü2012¦~²Ä15¶g
- yyyyww = Year(c.Value) & Format(DatePart("ww", c.Value), "00")
-
- 'Àˬd¦~¶g¬O§_¦b¦r¨åÀɤ¤¡AY¤£¦s¦b«h¥[¤J
- If Not d.Exists(yyyyww) Then
- d.Add yyyyww, c.Value
- End If
- Next
- '§R°£¡i©P»ù®æ2¡j¤u§@ªíº[¦sªº¸ê®Æ
- With Sheets("©P»ù®æ2")
- .[A1:E1].Value = Sheets("¤é»ù®æ").[A1:E1].Value
- .Activate
- .Rows("2:" & .[A2].End(xlDown).Row).ClearContents
- End With
-
- '«Ø¥ßADODB Connectionª«¥óÅܼÆ
- Dim cn As ADODB.Connection
- Set cn = New ADODB.Connection
-
- With cn
- .Provider = "MSDASQL"
- .ConnectionString = "Driver={Microsoft Excel Driver (*.xls)};" & _
- "DBQ=" & ThisWorkbook.FullName & ";"
- .Open
- End With
-
- 'SQL¦r¦ê
- mySQL = "Select ¦~¶g,FIRST(¶}½L»ù), MAX(³Ì°ª»ù), MIN(³Ì§C»ù), LAST(¦¬½L»ù) From ((SELECT (YEAR(¤é´Á)& FORMAT(DATEPART('ww',¤é´Á),'00')) AS ¦~¶g, ¶}½L»ù, ³Ì°ª»ù, ³Ì§C»ù, ¦¬½L»ù FROM [¤é»ù®æ$A:E] WHERE ¤é´Á IS NOT NULL) tmpTable) GROUP BY ¦~¶g"
-
- Set rs = cn.Execute(mySQL)
- With Sheets("©P»ù®æ2")
- .Activate
- .Range("A2").CopyFromRecordset rs
- End With
-
- '±N¦~¶gÂର¸Ó¶g²Ä¤@Ó¥æ©ö¤é´Á
- For Each c In Sheets("©P»ù®æ2").Range("A2:A" & Sheets("©P»ù®æ2").[A2].End(xlDown).Row)
- c.Value = d.Item(c.Value)
- Next
-
- 'Ãö³¬³s½u²M°£°O¾ÐÅé
- cn.Close
- Set cn = Nothing
-
- MsgBox "ÂàÀɧ¹¦¨!"
- End Sub
½Æ»s¥N½X |
|