2017-04-15 35 views
-1

想要檢查是否有與該文件同名的工作表。想要檢查是否有與該文件同名的工作表

目前,我有filnam作爲數組打開文件,但希望它遍歷代碼,看看是否有一個工作表具有相同的名稱。

我已經使用拆分來刪除路徑名和擴展名,但不能得到它來檢查。

我對代碼的混亂表示歉意。一直試圖讓它排序,然後我整理它。有更多的代碼,但這不是必需的,因爲我希望它運行該代碼,如果沒有匹配。

請幫助我們嗎?

Sub sort_it_out() 

Dim wb1 As Workbook 
Dim wb2 As Workbook 
Dim Sheet As Worksheet 
Dim filnam As Variant 
On Error GoTo errorhandler 

Set wb1 = ActiveWorkbook 

ChDir Application.ActiveWorkbook.path 

    'get files 
    filnam = Application.GetOpenFilename(FileFilter:="2D Table Formats (*.htm;*.xlsm;*.html),*.htm;*.xlsm;*.html", Title:="Select 2D Table", MultiSelect:=True) 

    'set the array 
    If IsArray(filnam) Then 'if at least one file is selected, this will be an Array 

    'define j as the array 
    For j = LBound(filnam) To UBound(filnam) 

    'remove path and extension 
    Dim s As String, a() As String, p As String 
    s = filnam(j) 
    a() = Split(s, "\") 
    p = Split(a(UBound(a)), ".")(0) 

    MsgBox "p " & p 

    'check if worksheet exists 
    For Each ws_check In ThisWorkbook.Worksheets() 

     If ws_check.Name = p Then 
      MsgBox "Its there" 
      Exit Sub 
      Else 
     End If 
    Next ws_check 

    'continue code from here 

然後運行代碼...但由於某種原因它沒有循環數組。一次只有一個文件。你能幫忙嗎?

回答

0

我已經全力以赴了。

這將現已開放文件的位置,縮短它們的路徑,只是文件名減去擴展,那麼WS檢查檢查對文件名的牀單,然後彎針跳到下一個。

謝謝alwaysdata幫助我。

Sub sort_it_out() 

Dim filnam As Variant 

'open file locations 
filnam = Application.GetOpenFilename(FileFilter:="2D Table Formats (*.htm;*.xlsm;*.html),*.htm;*.xlsm;*.html", Title:="Select 2D Table", MultiSelect:=True) 

'if at least one file is selected, this will be an Array 
    If IsArray(filnam) Then 

    For j = LBound(filnam) To UBound(filnam) 

'remove pathway and extension from entire filename and path. ie C:\open.txt becomes open. 

    Dim s As String, a() As String, p As String 
    s = filnam(j) 
    a() = Split(s, "\") 
    p = Split(a(UBound(a)), ".")(0) 


'check if worksheet exists against p ... ie if theres a worksheet called open it will goto the next option if not it will continue through code 
    For Each ws_check In ThisWorkbook.Worksheets() 

     If ws_check.Name = p Then 
      MsgBox p & " has already been transfered across. ", vbExclamation 'lets the user know this is already there. 

      GoTo looper 
      Else 
     End If 
    Next ws_check 

'do something here with the code if not found. IE MSGBOX " NOT FOUND " 

'jump to this point if there is a match. 
looper: 
Next 
    Else 
    Exit Sub 

End If 

End Sub 
1

這是有點難以遵循你的代碼,但這是否做你想做的事情?

我想你在p變量中存儲的文件名,所以我下面的代碼會檢查每個工作表在工作簿,看看他們是否有相同的名稱作爲變量p。

Public Sub CompareWorksheetNamesToFiles() 

    Dim file_name As String 
    file_name = ActiveWorkbook.Name 

    Dim ws_check As Worksheet 
    For Each ws_check In ThisWorkbook.Worksheets() 

     If ws_check.Name = p Then 
      Debug.Print ("Do Something") 
     End If 

    Next ws_check 

End Sub 
+0

謝謝。當我進入時我會檢查它。filnam是什麼打開文件到一個數組中。 Filnam(j)調用數組,然後我不得不用p縮短它以刪除所有附加路徑信息。這會檢查每個打開的文件或只是一個文件? –

+0

我想我需要定義p作爲來自filnam的數組,如果這很有意義 –

+0

以及你的p似乎是在for循環內,所以我想你正在瀏覽數組中的每個文件並用文件名填充p。但我可能是錯的。 – AlwaysData

相關問題