返回列表 上一主題 發帖

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

回復 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.可有更好的寫法?
謝謝

TOP

回復 9# yangjie

Function IsOpen() 我測試是可以執行

但檔案名稱有分大小寫,例如 Book1與BOOK1就會判斷為不同
WINDOW系統的檔案應該是沒有分大小寫(雖然顯是有大小寫)
{...} 表示需要用 CTRL+SHIFT+ENTER 三鍵輸入公式

TOP

回復 4# Hsieh

原來用.Caption就可以決解,太感謝你了
{...} 表示需要用 CTRL+SHIFT+ENTER 三鍵輸入公式

TOP

回復 4# Hsieh
對不起 將大名拼錯了 應為Hsieh大大

TOP

回復 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

    其中 fs 與 上述filename 型態不符 我就卡住了 怎麼辦?

TOP

回復 7# yangjie 主要

    主要下一列要Open
Set wb = Workbooks.Open(fileName, True, False)
   但常在已打開之下做重複打開
不支應如何處理

TOP

本帖最後由 yangjie 於 2012-9-1 04:33 編輯

回復 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)
謝謝

TOP

回復 5# 小俠客

千萬別這麼說,討論區沒有真正對與錯
勇於發表才能獲得更多
學海無涯_不恥下問

TOP

回復 4# Hsieh


   
對不起,胡亂回答錯的答案,十分抱歉:dizzy:

TOP

本帖最後由 Hsieh 於 2012-8-31 22:32 編輯

回復 3# ML089


    這個問題牽涉到開啟檔案是否是在同一個Application
如果已啟動一個全新的Excel然後開啟其他檔案
那麼同一個Application所開啟的檔案都能找得到
但是假如開啟了一些舊檔,然後按下程式集裡的Excel程式
在這個新的檔案內則無法搜尋到之前開啟的檔案名稱
必須是在已經開啟的程式中
以檔案/開新檔案才能夠搜尋所有已開啟檔案
FilesExists.rar (7.6 KB)
play.gif
學海無涯_不恥下問

TOP

        靜思自在 : 手心向下是助人,手心向上是求人;助人快樂,求人痛苦。
返回列表 上一主題