【问题标题】:Linking Second Variable Iteration to First将第二个变量迭代链接到第一个
【发布时间】:2017-07-31 14:58:11
【问题描述】:

您好,我需要一些帮助来完成我放置在 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

【问题讨论】:

    标签: vba loops ms-access


    【解决方案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
    

    【讨论】:

    • 这很简单。谢谢。我想多了。
    猜你喜欢
    • 2019-04-26
    • 1970-01-01
    • 2016-07-11
    • 2012-08-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-11
    相关资源
    最近更新 更多