Vba Access中的DIR函数和命名表
我正在使用DIR函数将一组excel文件导入access。然后我传递DIR的属性,使access中的表名与excel文件相同。唯一的问题是,我的名字中也有xls,我如何才能阻止它 代码如下:Vba Access中的DIR函数和命名表,vba,ms-access,Vba,Ms Access,我正在使用DIR函数将一组excel文件导入access。然后我传递DIR的属性,使access中的表名与excel文件相同。唯一的问题是,我的名字中也有xls,我如何才能阻止它 代码如下: Sub Sample2() Const cstrFolder As String = "F:\TCB_HR_KPI\Data View\" Dim strFile As String Dim i As Long strFile = Dir(cstrFolder & "*.xls") If Len(
Sub Sample2()
Const cstrFolder As String = "F:\TCB_HR_KPI\Data View\"
Dim strFile As String
Dim i As Long
strFile = Dir(cstrFolder & "*.xls")
If Len(strFile) = 0 Then
MsgBox "No Files Found"
Else
Do While Len(strFile) > 0
Debug.Print cstrFolder & strFile
DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9, _
strFile, cstrFolder & strFile, True
i = i + 1
strFile = Dir()
Loop
MsgBox i & " Files are imported"
End If
End Sub
你想要这个吗
Sub Sample2()
'
Const cstrFolder As String = "F:\TCB_HR_KPI\Data View\"
'
Dim i As Long, lng As Long
'
Dim strExt As String, strFile As String, strTable As String
'
strExt = ".xls"
lng = Len(strExt)
strFile = Dir(cstrFolder & "*" & strExt)
'
If Len(strFile) = 0 Then
MsgBox "No Files Found"
Else
Do While Len(strFile) > 0
'
' Debug.Print cstrFolder & strFile
'
strTable = Left(strFile, Len(strFile) - lng)
'
DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9, _
strTable, cstrFolder & strFile, True
i = i + 1
strFile = Dir()
Loop
MsgBox i & " Files are imported"
End If
'
End Sub
因此,类似Sample1.xls的文件将被改进为表Sample1。根据需要使用辅助函数将其剥离
Function StripFileExt(FileName As String) As String
Dim Pos As Long
Pos = InStrRev(FileName, ".")
If (Pos > 0) And (Pos > InStrRev(FileName, "\")) Then
StripFileExt = Left$(FileName, Pos - 1)
Else
StripFileExt = FileName
End If
End Function
使用“”拆分,并将该数组的第一个元素作为表名
Split(strFile,“.”(0)
您可以将该结果存储在中间变量中。或者直接在TransferSpreadsheet
语句中使用该表达式
DoCmd.transfer电子表格导入,acSpreadsheetTypeExcel9_
拆分(strFile,“.”(0),cstrFolder和strFile,True
注意:基于,我假设工作簿文件名只包含一个点:REPORT1.xls到REPORT67.xls中的名称。但是,如果您这次处理的文件名可能包含多个点,我的第一个建议是不合适的
在这种情况下,您仍然可以使用包含Split()
的表达式,但该表达式不会那么简单
左(strFile,Len(strFile)-Len(拆分(strFile,“.”(1))-1)
请注意,这种方法除了.xls之外,还可以容纳任何其他Excel文件扩展名,因为“.”字符原则上可以是主文件名的一部分(例如,“Report 10.10.13.xls”-您的方法将返回“Report 10”而不是“Report 10.10.13”)。我之前的评论是在编辑之前写的。不能说我个人热衷于使用Split只是为了找到一个最终的delimeter,但每个人都有自己的。。。