2017-07-31 23 views
0

こんにちは私はAccessに配置した次のコードを完成させるためにいくつかの助けが必要です。私はディレクトリ内のすべてのExcelファイルを反復する方法でファイル変数に一致するように、mfile変数に関するコードを変更する際の提案を探しています。2番目の変数の反復を最初にリンクする

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

関連する問題