回復 4#Hsieh
請教於板主先進
Dim Filt As String
Dim FilterIndex As Integer
Dim fileName As Variant
Dim Title As String
Filt = "Excel Files (*.xls),*.xls"
FilterIndex = 5
Title = "Select a File for Import"
fileName = Application.GetOpenFilename _
(FileFilter:=Filt, _
FilterIndex:=FilterIndex, _
Title:=Title)
If fileName = False Then
MsgBox "No file was selected."
Exit Sub
End If
我只要得到fileName 的檔名(string)應如何下語法?(不含path)
謝謝作者: yangjie 時間: 2012-9-1 04:49
回復 4#Hsieh
Hseih大大 謝謝您
Function IsOpen(Fs As String) As Boolean
IsOpen = False
For Each w In Windows
If w.Caption = Fs Then IsOpen = True: Exit For
Next
End Function
回復 4#Hsieh
謝謝Hsieh版主協助function Isopen()的指導
自個兒稍微有點突破
Sub 選擇檔名匯入()
Dim Path1, Str1 As String
Dim wb As Workbook
Path1 = Application.ActiveWorkbook.Path
Dim Filt As String
Dim FilterIndex As Integer
Dim fileName As Variant
Dim Title As String
Filt = "Excel Files (*.xls),*.xls"
FilterIndex = 5
Title = "Select a File for Import"
fileName = Application.GetOpenFilename _
(FileFilter:=Filt, _
FilterIndex:=FilterIndex, _
Title:=Title)
If fileName = False Then
MsgBox "No file was selected."
Exit Sub
End If
Dim fso3
Set fso3 = CreateObject("Scripting.FileSystemObject")
GetAn3 = fso3.GetbaseName(fileName)
If IsOpen(GetAn3 & ".xls") <> False Then
Workbooks(GetAn3 & ".xls").Activate
Else
Set wb = Workbooks.Open(fileName, True, False)
End If
f_bookname2 = ActiveWorkbook.Name
Windows(f_bookname2).Activate
Sheets(1).Activate
end sub
Function IsOpen(Fs As String) As Boolean
IsOpen = False
For Each w In Windows
If w.Caption = Fs Then IsOpen = True: Exit For
Next
End Function
再請教大大
1. 如何讓Application.GetOpenFilename 所開之視窗為path1?
2.可有更好的寫法?
謝謝作者: GBKEE 時間: 2012-9-1 17:25
建議一開始就將,路徑與檔名設定成不同變數。方便之後利用。
另外
'The following function returns the filename without the extension from the file's full path:
Function FileNameNoExt(strPath As String) As String
Dim strTemp As String
strTemp = Mid$(strPath, InStrRev(strPath, "\") + 1)
FileNameNoExt = Left$(strTemp, InStrRev(strTemp, ".") - 1)
End Function
'The following function returns the filename with the extension from the file's full path:
Function FileNameWithExt(strPath As String) As String
FileNameWithExt = Mid$(strPath, InStrRev(strPath, "\") + 1)
End Function
'取得路徑
'the following function will get the path only (i.e. the folder) from the file's ful path:
Function FilePath(strPath As String) As String
FilePath = Left$(strPath, InStrRev(strPath, "\"))
End Function作者: bugsfamily 時間: 2012-9-24 14:30
建議一開始就將,路徑與檔名設定成不同變數。方便之後利用。
另外
'取得路徑與檔名(不包括副檔名)
'The following function returns the filename without the extension from the file's full path:
Function FileNameNoExt(strPath As String) As String
Dim strTemp As String
strTemp = Mid$(strPath, InStrRev(strPath, "\") + 1)
FileNameNoExt = Left$(strTemp, InStrRev(strTemp, ".") - 1)
End Function
'取得完整檔名
'The following function returns the filename with the extension from the file's full path:
Function FileNameWithExt(strPath As String) As String
FileNameWithExt = Mid$(strPath, InStrRev(strPath, "\") + 1)
End Function
'取得路徑
'the following function will get the path only (i.e. the folder) from the file's ful path:
Function FilePath(strPath As String) As String
FilePath = Left$(strPath, InStrRev(strPath, "\"))
End Function