返回列表 上一主題 發帖

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

回復 4# Hsieh

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

TOP

回復 9# yangjie

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

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

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

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

回復 14# GBKEE

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

TOP

回復 14# GBKEE
謝了
if Ucase(filename)="FALSE" then

if filename=False then
可有相異

TOP

回復 15# Hsieh
水啦
謝了

TOP

回復 16# yangjie


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

TOP

回復 18# Hsieh

謝謝 完全了解

TOP

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

TOP

        靜思自在 : 修行要繫緣修心,藉事練心,隨處養心。
返回列表 上一主題