2017-07-31 57 views
0

您好,我需要一些幫助來完成我放置在Access中的以下代碼。我正在尋找修改關於mfile變量的代碼的建議,以便在它遍歷目錄中的所有Excel文件時如何匹配文件變量。將第二個變量迭代鏈接到第一個

Public Function load_data() 
'mfile is modified filename with "xl" prefix to group all imported tables 
together 

Dim file, mfile As Variant  
file = Dir(CurrentProject.Path & "\") 
mfile = "xl_" & Left(file, Len(file) - 5) 

Do Until file = ""  
    Debug.Print mfile   
    If file Like "*.xlsx" Then    
     For Each tbl In CurrentDb.TableDefs    
      If mfile = tbl.Name Then     
       DoCmd.DeleteObject acTable, tbl.Name 
       Exit For     
      End If    
     Next tbl   
     DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel12, mfile, CurrentProject.Path & "\" & file, True   
    End If   
    file = Dir   
Loop 

End Function 

回答

1

我想你只是想在你的循環內移動那一行?

Public Function load_data() 

    'mfile is modified filename with "xl" prefix to group 
    ' all imported tables together 

    Dim file, mfile As Variant  
    file = Dir(CurrentProject.Path & "\") 

    Do Until file = "" 

     If file Like "*.xlsx" Then 

      mfile = "xl_" & Left(file, Len(file) - 5) '<< move inside loop 
      Debug.Print mfile   

      For Each tbl In CurrentDb.TableDefs    
       If mfile = tbl.Name Then     
        DoCmd.DeleteObject acTable, tbl.Name 
        Exit For     
       End If    
      Next tbl   
      DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel12, _ 
          mfile, CurrentProject.Path & "\" & file, True   
     End If 

     file = Dir   
    Loop 

End Function 
+0

很簡單。謝謝。我正在推翻它。 – BarryMahnly

相關問題