返回列表 上一主題 發帖

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

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

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

本帖最後由 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

回復 7# yangjie 主要

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

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

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

TOP

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

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

TOP

回復 15# Hsieh
水啦
謝了

TOP

回復 18# Hsieh

謝謝 完全了解

TOP

        靜思自在 : 【做人的開始】每一天都是故人的開始,每一個時刻都是自己的警惕。
返回列表 上一主題