- ©«¤l
- 90
- ¥DÃD
- 16
- ºëµØ
- 0
- ¿n¤À
- 114
- ÂI¦W
- 0
- §@·~¨t²Î
- XP
- ³nÅ骩¥»
- sp2
- ¾\ŪÅv
- 20
- ©Ê§O
- ¨k
- µù¥U®É¶¡
- 2011-7-9
- ³Ì«áµn¿ý
- 2018-10-7
|
¥»©«³Ì«á¥Ñ white5168 ©ó 2012-6-23 22:57 ½s¿è
¦bSheet1ªºµ{¦¡½X- Private Sub ¤j½L¦¨¥æ¸ê°T_Click()
- Dim Year As String
- Dim Mon As String
-
- Year = Format(Range("C1"), "0000") 'קï¦r¦ê®æ¦¡
- Mon = Format(Range("C2"), "00") 'קï¦r¦ê®æ¦¡
- Call Run(Year, Mon) '©I¥sModule1¤¤ªº¨ç¼Æ
- End Sub
½Æ»s¥N½X ¦bModule1ªºµ{¦¡½X- Sub Run(Year As String, Month As String)
- Dim sheetName As String
- sheetName = "Temp"
-
- If CheckSheetExist(sheetName) <> True Then '½T©wTemp¤u§@ªí¬O§_¦s¦b,Y¤£¦s¦b«h©I¥sAddTempSheet«Ø¥ßTemp¤u§@ªí
- Call AddTempSheet(sheetName)
- End If
-
- Call ClearTempTablesData(sheetName) 'ÁקKTemp¤u§@ªí¦s¦b®É¸ê®Æ®æ¦¡¥¼²M°£,¦Ó²M°£
- Call GetPrice(sheetName, Year, Month) '±qTWSE¨ú±o¤j½L¾ú¥v¸ê®Æ
- Call ClearsheetTablesData("Sheet1") '²M°£ì¥»¦bSheet1¤u§@ªíªº¸ê®Æ
- Call SetCellWidthSize(sheetName) '³]©wTWSE¨ú±oªº¸ê®Æ©Ò³y¦¨ªº®æ¦¡,±N¦¹®æ¦¡½Õ¾ã¬°excel¹w³]ªºÀx¦s®æ®æ¦¡
- Call CopyDatatoSheet(sheetName) '±NTemp¤u§@ªí¸ê®Æ«þ¨©¦ÜSheet1¤u§@ªí
- Call DeleteTempSheet(sheetName) '§R°£Temp¤u§@ªí
- Sheets("Sheet1").Select '±Nfocus³]©w¨ìSheet1¤u§@ªí
- End Sub
- Function CheckSheetExist(sheetName As String) As Boolean
- Dim i As Integer
- CheckSheetExist = False
- For i = 1 To Worksheets.Count '¨ú±o¥Ø«e¤u§@ªíªº¼Æ¶q
- If sheetName = Worksheets(i).Name Then '§PÂ_«ü©wªº¤u§@ªí¦WºÙ¬O§_¦s¦b,¦s¦b«h¦^¶Ç§ä¨ì¤u§@ªíªº°T®§
- CheckSheetExist = True '±N§ä¨ìªº°T®§³]©w¦Ü¦^¶ÇÈ
- End If
- Next
- End Function
- Sub AddTempSheet(sheetName As String)
- ActiveWorkbook.Worksheets.Add After:=Worksheets(Worksheets.Count) '«Ø¥ß«ü©w¤u§@¨ì²{¦s¤u§@ªíªº¹ï«á±
- Worksheets(Worksheets.Count).Select '¿ï¾Ü«Ø¥ß¤u§@ªí
- ActiveSheet.Name = sheetName 'קï¤u§@ªí¦WºÙ
- End Sub
- Sub GetPrice(sheetName As String, Year As String, Month As String)
-
- Sheets(sheetName).UsedRange.Select '¿ï¨ú«ü©w¤u§@ªíA1:H50ªºÀx¦s®æ½d³ò
- Selection.Clear '²M°£©Ò¿ï¨úÀx¦s®æ®æ¦¡
- Selection.ClearContents '²M°£©Ò¿ï¨úªº¸ê®Æ
- Sheets(sheetName).Range("A1").Select '¿ï¨úTemp¤u§@ªíA1Àx¦s®æ,ÁקK¨Ï¥ÎQueryTable«á,¦]¬°¸ê®ÆÀ½À£·|³y¦¨Àx¦s®æ¥k²¾,¾ÉPfoucs¤£¦bA1Àx¦s®æ¤W¦Óµo¥Í¿ù»~°T®§
-
- '¥H¤U´N¤£¦h¤¶²Ð,Excel¬ÛÃö¤º®e
- With ActiveSheet.QueryTables.Add(Connection:= _
- "TEXT;http://www.twse.com.tw/ch/trading/exchange/FMTQIK/FMTQIK2.php?STK_NO=&myear=" & Year & "&mmon=" & Month & "&type=csv", _
- Destination:=Range("A1"))
- .Name = "¤j½L¾ú¥v¸ê®Æ"
- .FieldNames = True
- .RowNumbers = False
- .FillAdjacentFormulas = False
- .PreserveFormatting = True
- .RefreshOnFileOpen = False
- .RefreshStyle = xlInsertDeleteCells
- .SavePassword = False
- .SaveData = True
- .AdjustColumnWidth = True
- .RefreshPeriod = 0
- .TextFilePromptOnRefresh = False
- .TextFilePlatform = 950
- .TextFileStartRow = 1
- .TextFileParseType = xlDelimited
- .TextFileTextQualifier = xlTextQualifierDoubleQuote
- .TextFileConsecutiveDelimiter = False
- .TextFileTabDelimiter = False
- .TextFileSemicolonDelimiter = False
- .TextFileCommaDelimiter = True
- .TextFileSpaceDelimiter = False
- .TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1)
- .TextFileTrailingMinusNumbers = True
- .Refresh BackgroundQuery:=False 'Y¨S¦³ Sheets(sheetName).Range("A1").Select,¦b¦¹¦æ·|µo¥Í¿ù»~
- If Err.Number <> 0 Then Err.Clear: MsgBox "¸ê®Æ¬d¸ß¥¢±Ñ" '³Q§K¸ê®Æ§ì¨ú¤£¦¨¥\,¦ÓÅã¥Ü°T®§
- End With
- End Sub
- Sub SetCellWidthSize(sheetName As String)
- Dim n As Integer
- Worksheets(sheetName).Select
- n = ActiveSheet.Range("A1").End(xlDown).Row '¨ú±o¿ï¨ú¦³¦s¦b¸ê®ÆªºÀx¦s®æ¦C¼Æ
- ActiveSheet.Range("A1:F" & n).UseStandardWidth = True '³]©w«ü©w¤u§@ªíªºÀx¦s®æ¼e«×¬°¹w³]È
- End Sub
- Sub CopyDatatoSheet(sheetName As String)
- Dim n As Integer
-
- Worksheets(sheetName).Select '¿ï¨ú«ü©w¦WºÙ¤u§@ªí
- n = ActiveSheet.Range("A3").End(xlDown).Row - 1 '¨ú±o¿ï¨ú¦³¦s¦b¸ê®ÆªºÀx¦s®æ¦C¼Æ
- ActiveSheet.Range("A3:F" & n).Copy '½Æ»s¿ï¨úªºÀx¦s®æ¸ê®Æ
-
- Worksheets("Sheet1").Select '¿ï¨úSheet1¤u§@ªí
- Range("A5").Select '¿ï¨úA5Àx¦s®æ
- ActiveSheet.Paste '¶K¤W¸ê®Æ
- End Sub
- Sub ClearsheetTablesData(sheetName As String)
- Dim n As Integer
- Dim qyt As QueryTable
-
- Worksheets(sheetName).Select
- If ActiveSheet.Range("A5") <> "" Then '§PÂ_¥Ø«eªº¬¡¶Ã¯¬O§_¦³¸ê®Æ¦s¦b, ³o¦æ¥i¥H¦A¼gªº§óÂÔ·V,Åwªï¦U¦ì¦Û¦æקï
- n = ActiveSheet.Range("A5").End(xlDown).Row '¿ï¨ú¥Ø«e¬¡¶Ã¯±qA4¦ì¸m¨ì³Ì«á¤@¦æªº½d³ò
- For Each qyt In ActiveSheet.QueryTables '¿ï¨ú¥ÎQueryTables§ì¨úªº¨C¤@¦æ
- qyt.Delete '±N¨Ï¥ÎQueryTables¤èªk©Ò²£¥Íªº¦æ¶i¦æ§R°£,ÁקKQueryTables¥Î¤[¤F,³y¦¨¨t²Î°ï¿n¤@°ïQueryTablesªº©U§£,¦p¦¹¨t²Î¤~³¡·|ÅܺC
- Next
- ActiveSheet.Range("A5:G" & n).Clear '²M°£©Ò¿ï¨úÀx¦s®æ®æ¦¡
- ActiveSheet.Range("A5:G" & n).ClearContents '²M°£©Ò¿ï¨úªº¸ê®Æ
- Else
- ActiveSheet.Range("A5:G40").Clear '²M°£©Ò¿ï¨úÀx¦s®æ®æ¦¡
- ActiveSheet.Range("A5:G40").ClearContents '²M°£©Ò¿ï¨úªº¸ê®Æ
- End If
- End Sub
- Sub DeleteTempSheet(sheetName As String)
- Worksheets(sheetName).Select
- Application.DisplayAlerts = False 'Ãö³¬Äµ§iµøµ¡
- Worksheets(sheetName).Delete '§R°£§@¥Î¤¤ªº¤u§@ªí
- Application.DisplayAlerts = True '«ì´_ĵ§iµøµ¡
- End Sub
- Sub ClearTempTablesData(sheetName As String)
- Dim n As Integer
- Dim qyt As QueryTable
-
- Worksheets(sheetName).Select '¿ï¨ú«ü©w¦WºÙ¤u§@ªí
- If ActiveSheet.Range("A1") <> "" Then
- n = ActiveSheet.Range("A1").End(xlDown).Row '¿ï¨ú¥Ø«e¬¡¶Ã¯±qA1¦ì¸m¨ì³Ì«á¤@¦æªº½d³ò
- For Each qyt In Worksheets(sheetName).QueryTables '¿ï¨ú¥ÎQueryTables§ì¨úªº¨C¤@¦æ
- qyt.Delete '±N¨Ï¥ÎQueryTables¤èªk©Ò²£¥Íªº¦æ¶i¦æ§R°£,ÁקKQueryTables¥Î¤[¤F,³y¦¨¨t²Î°ï¿n¤@°ïQueryTablesªº©U§£,¦p¦¹¨t²Î¤~³¡·|ÅܺC
- Next
- ActiveSheet.Range("A1:F" & n).Clear '²M°£©Ò¿ï¨úÀx¦s®æ®æ¦¡
- ActiveSheet.Range("A1:F" & n).ClearContents '²M°£©Ò¿ï¨úªº¸ê®Æ
- Else
- ActiveSheet.UsedRange.Clear '²M°£©Ò¿ï¨úÀx¦s®æ®æ¦¡
- ActiveSheet.UsedRange.ClearContents '²M°£©Ò¿ï¨úªº¸ê®Æ
- End If
- End Sub
½Æ»s¥N½X |
|