返回列表 上一主題 發帖

偵測"1.xls"檔是否呈打開狀態?

偵測"1.xls"檔是否呈打開狀態?

請教各位大大:
        個人在用VBA打開另外Excel檔如"1.xls"檔,若逢"1.xls"檔本來就已呈打開狀態時,就不太恰當。
請教各位先進若要偵測"1.xls"檔是否呈打開狀態?應如何運用VBA語法?

建議一開始就將,路徑與檔名設定成不同變數。方便之後利用。
另外
'取得路徑與檔名(不包括副檔名)
'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

TOP

建議一開始就將,路徑與檔名設定成不同變數。方便之後利用。
另外
'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

TOP

回復 15# Hsieh
那有什麼湊熱鬧的,論壇是越熱鬧越好

TOP

回復 18# Hsieh

謝謝 完全了解

TOP

回復 16# yangjie


    這是不一樣的
因為filename被宣告為string所以filename="FALSE"才是正確
若為filename=FALSE這是變成邏輯值判斷
filename必須是布林值
學海無涯_不恥下問

TOP

回復 15# Hsieh
水啦
謝了

TOP

回復 14# GBKEE
謝了
if Ucase(filename)="FALSE" then
與
if filename=False then
可有相異

TOP

回復 14# GBKEE

湊熱鬧,FullName去除路徑只剩檔名
    xlfileName = Dir(xlFullName)
學海無涯_不恥下問

TOP

回復 13# yangjie
  1. Option Explicit
  2. Sub 選擇檔名匯入()
  3.     Dim Path1, Str1 As String
  4.     Dim wb As Workbook
  5.     Dim Filt As String
  6.     Dim FilterIndex As Integer
  7.     Dim Title As String
  8.     '---------------------------------
  9.     Dim xlfileName As String, xlFullName As String
  10.     '修改型態 As String  xlfileName:沒有路徑
  11.     Dim MyPath
  12.     MyPath = CurDir    '紀錄原有的目錄或檔案夾。
  13.     Path1 = Application.ActiveWorkbook.Path
  14.    ' Path1 = "C:\WINDOWS\SYSTEM"
  15.     ChDrive Split(Path1, ":")(0)    'ChDrive 陳述式 改變目前的磁碟機。
  16.     ChDir Path1                     'ChDir   陳述式 改變目前的目錄或檔案夾。
  17.     '請注意 ChDir 陳述式會改變現有目錄位置,但不會改變磁碟機位置,
  18.     '例如,如果現在的磁碟機是 C,陳述式將現有目錄切換到磁碟機 D,但是 C 仍然是現有的磁碟機位置:
  19.     Filt = "Excel Files (*.xls),*.xls"
  20.     FilterIndex = 5
  21.     Title = "Select a File for Import"
  22.     xlFullName = Application.GetOpenFilename _
  23.         (FileFilter:=Filt, _
  24.          FilterIndex:=FilterIndex, _
  25.          Title:=Title)
  26.    
  27.     If UCase(xlFullName) = "FALSE" Then
  28.         MsgBox "No file was selected."
  29.         Exit Sub
  30.     End If
  31.     '''''''''''''''''''''''''
  32.     ChDrive Split(MyPath, ":")(0)    '改變為原有的磁碟機。
  33.     ChDir MyPath                     '改變為原有的目錄或檔案夾。
  34.     ''''''''''''''''''''''''''
  35.    
  36.     xlfileName = Split(xlFullName, "\")(UBound(Split(xlFullName, "\")))
  37.     '陣列(上限元素數) 取的檔案名稱沒有路徑
  38.     If IsOpen(xlfileName) <> False Then
  39.         Workbooks(xlfileName).Activate
  40.     Else
  41.         Set wb = Workbooks.Open(xlFullName, True, False)
  42.     End If
  43.     f_bookname2 = ActiveWorkbook.Name
  44.     Windows(f_bookname2).Activate
  45.     Sheets(1).Activate
  46. End Sub
  47. Function IsOpen(Fs As String) As Boolean
  48.     IsOpen = False
  49.     For Each W In Windows
  50.          If W.Caption = Fs Then IsOpen = True: Exit For
  51.    Next
  52. End Function
複製代碼

TOP

        靜思自在 : 口說一句好話,如口出蓮花;口說一句壞話如口吐毒蛇。
返回列表 上一主題