Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vba 将第二个变量迭代链接到第一个变量迭代_Vba_Loops_Ms Access - Fatal编程技术网

Vba 将第二个变量迭代链接到第一个变量迭代

Vba 将第二个变量迭代链接到第一个变量迭代,vba,loops,ms-access,Vba,Loops,Ms Access,嗨,我需要一些帮助来完成我放在Access中的以下代码。我正在寻找修改关于mfile变量的代码的建议,以便在遍历目录中的所有Excel文件时与file变量匹配 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.

嗨,我需要一些帮助来完成我放在Access中的以下代码。我正在寻找修改关于mfile变量的代码的建议,以便在遍历目录中的所有Excel文件时与file变量匹配

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

我想你只是想把那条线移到你的圈里

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

这很简单。谢谢我想得太多了。