- 帖子
- 44
- 主題
- 7
- 精華
- 0
- 積分
- 51
- 點名
- 0
- 作業系統
- XP
- 軟體版本
- XP
- 閱讀權限
- 20
- 註冊時間
- 2012-11-18
- 最後登錄
- 2022-8-6

|
10#
發表於 2012-12-28 23:25
| 只看該作者
回復 9# gkld
再請教一個問題,我例用板大教的方式,跑出第一個1101台泥工作表
,程式碼如下:- Option Explicit
- Sub Ex()
- Dim Ex_Path As String, Ex_File As String, Ex_Date As String, Ex_Wb As Workbook
- Dim Rng As Range
- Ex_Path = "C:\Documents and Settings\gkld\桌面\my kp\資料庫\上市\" '******修改它********
- Ex_File = Dir(Ex_Path & "A112*ALL_1.csv")
- If Ex_File = "" Then
- MsgBox "沒有 A112*ALL_1.csv"
- Exit Sub
- End If
- Application.ScreenUpdating = False
- Range("a1:ag65536").Clear '消除每一行資料
- Do While Ex_File <> ""
- Ex_Date = Replace(Ex_File, "A112", "") '消除檔名中"A112"
- Ex_Date = Replace(Ex_Date, "ALL_1.csv", "") '消除檔名中"ALL_1.csv"
- Ex_Date = DateSerial(Mid(Ex_Date, 1, 4), Mid(Ex_Date, 5, 2), Mid(Ex_Date, 7, 2)) '帶入日期
- With ActiveSheet '作用中的工作表
- Set Ex_Wb = Workbooks.Open(Ex_Path & Ex_File) '開啟 A11220070102ALL_1.csv.....
- '************************************************
- .Cells(.Rows.Count, "A").End(xlUp).Offset(1) = Ex_Date '日期輸入
- If Ex_Wb.Sheets(1).Range("B3") <> "" Then
- .Cells(.Rows.Count, "B").End(xlUp).Offset(1) = Ex_Wb.Sheets(1).Range("b:b").Find("台泥", lookat:=xlWhole).Offset(, 7)
- .Cells(.Rows.Count, "e").End(xlUp).Offset(1) = Ex_Wb.Sheets(1).Range("b:b").Find("台泥", lookat:=xlWhole).Offset(, 4)
- .Cells(.Rows.Count, "f").End(xlUp).Offset(1) = Ex_Wb.Sheets(1).Range("b:b").Find("台泥", lookat:=xlWhole).Offset(, 5)
- .Cells(.Rows.Count, "g").End(xlUp).Offset(1) = Ex_Wb.Sheets(1).Range("b:b").Find("台泥", lookat:=xlWhole).Offset(, 6)
- .Cells(.Rows.Count, "i").End(xlUp).Offset(1) = Ex_Wb.Sheets(1).Range("b:b").Find("台泥", lookat:=xlWhole).Offset(, 1)
-
- Else
- .Cells(.Rows.Count, "B").End(xlUp).Offset(1) = "---" '**沒有資料
- .Cells(.Rows.Count, "e").End(xlUp).Offset(1) = "---" '**沒有資料
- .Cells(.Rows.Count, "f").End(xlUp).Offset(1) = "---" '**沒有資料
- .Cells(.Rows.Count, "g").End(xlUp).Offset(1) = "---" '**沒有資料
- .Cells(.Rows.Count, "i").End(xlUp).Offset(1) = "---" '**沒有資料
- '**** 修改作用中的工作表.Range("B1") 為查詢指數的類別 *********
- End If
- '************************************************
- Ex_Wb.Close False '關閉 A11220070102ALL_1.csv.....
- End With
- Ex_File = Dir '下一個"A112*ALL_1.csv"
- Loop
- Application.ScreenUpdating = True
- MsgBox "OK"
- End Sub
複製代碼 後來,我想在新增好幾個工作表,如1102亞泥;1103嘉泥;…等一直到1110東泥,共7個工作表
程式碼如下:- Option Explicit
- Sub Ex()
- Dim Ex_Path As String, Ex_File As String, Ex_Date As String, Ex_Wb As Workbook
- Dim Rng As Range
- Ex_Path = "C:\Documents and Settings\gkld\桌面\my kp\資料庫\上市\" '******修改它********
- Ex_File = Dir(Ex_Path & "A112*ALL_1.csv")
- If Ex_File = "" Then
- MsgBox "沒有 A112*ALL_1.csv"
- Exit Sub
- End If
- Application.ScreenUpdating = False
- Range("a1:ag65536").Clear '消除每一行資料
- For i = 1 To 7
- If i = 1 Then Name = "台泥"
- End If
- If i = 2 Then Name = "亞泥"
- End If
- If i = 3 Then Name = "嘉泥"
- End If
- If i = 4 Then Name = "環泥"
- End If
- If i = 5 Then Name = "幸福"
- End If
- If i = 6 Then Name = "信大"
- End If
- If i = 7 Then Name = "東泥"
- End If
- Do While Ex_File <> ""
- Ex_Date = Replace(Ex_File, "A112", "") '消除檔名中"A112"
- Ex_Date = Replace(Ex_Date, "ALL_1.csv", "") '消除檔名中"ALL_1.csv"
- Ex_Date = DateSerial(Mid(Ex_Date, 1, 4), Mid(Ex_Date, 5, 2), Mid(Ex_Date, 7, 2)) '帶入日期
- With ActiveSheet '作用中的工作表
- Set Ex_Wb = Workbooks.Open(Ex_Path & Ex_File) '開啟 A11220070102ALL_1.csv.....
- '************************************************
- .Cells(.Rows.Count, "A").End(xlUp).Offset(1) = Ex_Date '日期輸入
- If Ex_Wb.Sheets(1).Range("B3") <> "" Then
- Sheet(i).Cells(.Rows.Count, "B").End(xlUp).Offset(1) = Ex_Wb.Sheets(1).Range("b:b").Find(Name, lookat:=xlWhole).Offset(, 7)
- Sheet(i).Cells(.Rows.Count, "e").End(xlUp).Offset(1) = Ex_Wb.Sheets(1).Range("b:b").Find(Name, lookat:=xlWhole).Offset(, 4)
- Sheet(i).Cells(.Rows.Count, "f").End(xlUp).Offset(1) = Ex_Wb.Sheets(1).Range("b:b").Find(Name, lookat:=xlWhole).Offset(, 5)
- Sheet(i).Cells(.Rows.Count, "g").End(xlUp).Offset(1) = Ex_Wb.Sheets(1).Range("b:b").Find(Name, lookat:=xlWhole).Offset(, 6)
- Sheet(i).Cells(.Rows.Count, "i").End(xlUp).Offset(1) = Ex_Wb.Sheets(1).Range("b:b").Find(Name, lookat:=xlWhole).Offset(, 1)
-
- Else
- .Cells(.Rows.Count, "B").End(xlUp).Offset(1) = "---" '**沒有資料
- .Cells(.Rows.Count, "e").End(xlUp).Offset(1) = "---" '**沒有資料
- .Cells(.Rows.Count, "f").End(xlUp).Offset(1) = "---" '**沒有資料
- .Cells(.Rows.Count, "g").End(xlUp).Offset(1) = "---" '**沒有資料
- .Cells(.Rows.Count, "i").End(xlUp).Offset(1) = "---" '**沒有資料
- '**** 修改作用中的工作表.Range("B1") 為查詢指數的類別 *********
- End If
- '************************************************
- Ex_Wb.Close False '關閉 A11220070102ALL_1.csv.....
- End With
- Ex_File = Dir '下一個"A112*ALL_1.csv"
- Loop
- Application.ScreenUpdating = True
- MsgBox "OK"
- End Sub
複製代碼 用很笨拙的方式去寫,卻跑不出結果來,出現錯誤…,可以請板大撥空幫我看看嗎? |
|