標題:
GetOpenFilename的語法問題
[打印本頁]
作者:
doom834
時間:
2011-11-16 13:31
標題:
GetOpenFilename的語法問題
小弟最近在使用GetOpenFilename時發生了一些問題一直無法排除,
以下是我截取了我程式碼的部分,
目前小弟遇到的問題就是我在判斷FO在開啟舊檔時若按下取消會出現錯誤,
若我在If UBound(FO) > File_qty Then前面再加入If FO = False Then來判斷時可以解決,
但若我實際有載入檔案時就會發生型態不符的問題,而無法繼續後面的程式,請各位大大幫我解惑一下,謝謝。
File_qty = Sheets(SN_1).Cells(2, 6)
FO = Application.GetOpenFilename("Excel File(*.CSV) (*.CSV),(*.CSV)", , , , True)
If UBound(FO) > File_qty Then
MsgBox ("載入檔案大於" & File_qty & "個已超出程式上限,請重新選擇檔案與確認載入數量是否小於或等於" & File_qty & "個,謝謝!!")
Sheets(SN_1).Select
Else
For I = 1 To UBound(FO)
Sheets(SN_2).Select
WB = ActiveWorkbook.Name
WS = ActiveSheet.Name
Workbooks.Open Filename:=FO(I), ReadOnly:=False, Notify:=False
FN = ActiveWorkbook.Name
SN = ActiveSheet.Name
Range("A1:O15").Copy
Windows(WB).Activate
Sheets(SN_2).Cells(1, 1).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
NN = [A65536].End(xlUp).Row
Windows(FN).Activate
ActiveWorkbook.Close SaveChanges:=False
Windows(WB).Activate
ID = Cells(2, 3)
T = Split(Trim(Cells(3, 3)), " ")
Range(Cells(7, 1), Cells(NN, 15)).Copy
Sheets("LED測試結果").Select
Cells((I - 1) * 20 + 8, 1).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Cells((I - 1) * 20 + 4, 2) = Mid(ID, 1, Len(ID) - 4)
Cells(I + 4, 20) = Mid(ID, 1, Len(ID) - 4)
Cells((I - 1) * 20 + 5, 2) = T(0)
Cells((I - 1) * 20 + 6, 2) = T(1) & " " & T(2)
Next I
Sheets("LED測試結果").Select
Application.Run ("Calculate_Mcd")
End If
作者:
GBKEE
時間:
2011-11-16 15:13
回復
1#
doom834
UBound
(FO)-> FO 必須是陣列 , GetOpenFilename 方法 只傳回選取檔案名稱 沒有傳回陣列
試試看
Sub Ex()
Dim File_qty As Integer, SN_1 As String, i As Integer
File_qty = Sheets(SN_1).Cells(2, 6)
With Application.FileDialog(msoFileDialogOpen)
.InitialFileName = "d:\" '指定開啟的路徑
.Filters.Clear
.Filters.Add "Excel File", "*.csv; *.txt; *.xls", 1
.Filters.Add "*.*", "*.*", 2
.FilterIndex = 1
.Show
If .SelectedItems.Count > File_qty Then
MsgBox ("載入檔案大於" & File_qty & "個已超出程式上限,請重新選擇檔案與確認載入數量是否小於或等於" & File_qty & "個,謝謝!!")
Sheets(SN_1).Select
Else
For i = 1 To .SelectedItems.Count
MsgBox .SelectedItems(1)
Next
End If
End With
End Sub
複製代碼
作者:
doom834
時間:
2011-11-16 17:38
GB大大真強,
我就卡在FO有讀取檔案時是陣列與按下取消時型態不符而卡住,
讓我學習到不同的寫法了~~感謝GB大大的指導!!!
歡迎光臨 麻辣家族討論版版 (http://forum.twbts.com/)