Excel:在文件名太长后使用VBA和名称表导入文件

Excel:在文件名太长后使用VBA和名称表导入文件,excel,vba,Excel,Vba,我已经修改了我在这里找到的一个代码,它将文本文件拉入并将数据粘贴到新的工作表中。该文件应将图纸命名为文本文件的名称,但我的文本文件名称太大。excel表格似乎可以有31个字符长。如何调整此代码以使用文本文件名的前31个字符命名工作表 我还希望代码提示我选择文件夹目标。我试过几件事,但还没弄明白 Sub-ImportManyTXTs_test() 作为字符串的Dim strFile 将ws设置为工作表 strFile=Dir(“I:\path\*.lev”) 执行While strFile vb

我已经修改了我在这里找到的一个代码,它将文本文件拉入并将数据粘贴到新的工作表中。该文件应将图纸命名为文本文件的名称,但我的文本文件名称太大。excel表格似乎可以有31个字符长。如何调整此代码以使用文本文件名的前31个字符命名工作表

我还希望代码提示我选择文件夹目标。我试过几件事,但还没弄明白

Sub-ImportManyTXTs_test()
作为字符串的Dim strFile
将ws设置为工作表
strFile=Dir(“I:\path\*.lev”)
执行While strFile vbNullString
设置ws=Sheets.Add
使用ws.QueryTables.Add(连接:=_
“TEXT;”和“I:\path\”&strFile,目标:=范围($A$1))
.Name=strFile
.FieldNames=True
.rowNumber=False
.FillAdjacentFormulas=False
.PreserveFormatting=True
.refreshinfoleopen=False
.RefreshStyle=xlInsertDeleteCells
.SavePassword=False
.SaveData=True
.AdjustColumnWidth=True
.RefreshPeriod=0
.TextFilePromptOnRefresh=False
.TextFilePlatform=437
.TextFileStartRow=1
.TextFileParseType=xlFixedWidth
.TextFileTextQualifier=xlTextQualifierDoubleQuote
.textfileconsutivedelimiter=False
.TextFileTabDelimiter=False
.TextFileSemicolonDelimiter=False
.textfilecommadelimitor=False
.TextFileSpaceDelimiter=False
.TextFileColumnDataTypes=数组(xlYMDFormat,1,1)
.TextFileFixedColumnWidths=数组(22,13,13)
.TextFileTrailingMinusNumbers=True
.Refresh BackgroundQuery:=False
以
strFile=Dir
环

结束子文件
.Name=strFile更改为

If Len(strFile) < 31 Then
   .Name = strFile
Else
   .Name = Mid(strFile, 1, 31)
End If
如果Len(strFile)<31则
.Name=strFile
其他的
.Name=Mid(strFile,1,31)
如果结束

.Name=strFile
更改为

If Len(strFile) < 31 Then
   .Name = strFile
Else
   .Name = Mid(strFile, 1, 31)
End If
如果Len(strFile)<31则
.Name=strFile
其他的
.Name=Mid(strFile,1,31)
如果结束

.Name=strFile
更改为

If Len(strFile) < 31 Then
   .Name = strFile
Else
   .Name = Mid(strFile, 1, 31)
End If
如果Len(strFile)<31则
.Name=strFile
其他的
.Name=Mid(strFile,1,31)
如果结束

.Name=strFile
更改为

If Len(strFile) < 31 Then
   .Name = strFile
Else
   .Name = Mid(strFile, 1, 31)
End If
如果Len(strFile)<31则
.Name=strFile
其他的
.Name=Mid(strFile,1,31)
如果结束
使用
LEFT()
函数仅获取文件名的前31个字符,如下所示:

Sub ImportManyTXTs_test()
Dim strFile As String
Dim ws As Worksheet
strFile = Dir("I:\path\*.lev")
Do While strFile <> vbNullString
Set ws = Sheets.Add
With ws.QueryTables.Add(Connection:= _
    "TEXT;" & "I:\path\" & strFile, Destination:=Range("$A$1"))
    .Name = LEFT(strFile,31)
    .FieldNames = True
    .RowNumbers = False
    .FillAdjacentFormulas = False
    .PreserveFormatting = True
    .RefreshOnFileOpen = False
    .RefreshStyle = xlInsertDeleteCells
    .SavePassword = False
    .SaveData = True
    .AdjustColumnWidth = True
    .RefreshPeriod = 0
    .TextFilePromptOnRefresh = False
    .TextFilePlatform = 437
    .TextFileStartRow = 1
    .TextFileParseType = xlFixedWidth
    .TextFileTextQualifier = xlTextQualifierDoubleQuote
    .TextFileConsecutiveDelimiter = False
    .TextFileTabDelimiter = False
    .TextFileSemicolonDelimiter = False
    .TextFileCommaDelimiter = False
    .TextFileSpaceDelimiter = False
    .TextFileColumnDataTypes = Array(xlYMDFormat, 1, 1)
    .TextFileFixedColumnWidths = Array(22, 13, 13)
    .TextFileTrailingMinusNumbers = True
    .Refresh BackgroundQuery:=False
End With
strFile = Dir
Loop
End Sub
Sub-ImportManyTXTs_test()
作为字符串的Dim strFile
将ws设置为工作表
strFile=Dir(“I:\path\*.lev”)
执行While strFile vbNullString
设置ws=Sheets.Add
使用ws.QueryTables.Add(连接:=_
“TEXT;”和“I:\path\”&strFile,目标:=范围($A$1))
.Name=左(strFile,31)
.FieldNames=True
.rowNumber=False
.FillAdjacentFormulas=False
.PreserveFormatting=True
.refreshinfoleopen=False
.RefreshStyle=xlInsertDeleteCells
.SavePassword=False
.SaveData=True
.AdjustColumnWidth=True
.RefreshPeriod=0
.TextFilePromptOnRefresh=False
.TextFilePlatform=437
.TextFileStartRow=1
.TextFileParseType=xlFixedWidth
.TextFileTextQualifier=xlTextQualifierDoubleQuote
.textfileconsutivedelimiter=False
.TextFileTabDelimiter=False
.TextFileSemicolonDelimiter=False
.textfilecommadelimitor=False
.TextFileSpaceDelimiter=False
.TextFileColumnDataTypes=数组(xlYMDFormat,1,1)
.TextFileFixedColumnWidths=数组(22,13,13)
.TextFileTrailingMinusNumbers=True
.Refresh BackgroundQuery:=False
以
strFile=Dir
环
端接头
使用
LEFT()
函数仅获取文件名的前31个字符,如下所示:

Sub ImportManyTXTs_test()
Dim strFile As String
Dim ws As Worksheet
strFile = Dir("I:\path\*.lev")
Do While strFile <> vbNullString
Set ws = Sheets.Add
With ws.QueryTables.Add(Connection:= _
    "TEXT;" & "I:\path\" & strFile, Destination:=Range("$A$1"))
    .Name = LEFT(strFile,31)
    .FieldNames = True
    .RowNumbers = False
    .FillAdjacentFormulas = False
    .PreserveFormatting = True
    .RefreshOnFileOpen = False
    .RefreshStyle = xlInsertDeleteCells
    .SavePassword = False
    .SaveData = True
    .AdjustColumnWidth = True
    .RefreshPeriod = 0
    .TextFilePromptOnRefresh = False
    .TextFilePlatform = 437
    .TextFileStartRow = 1
    .TextFileParseType = xlFixedWidth
    .TextFileTextQualifier = xlTextQualifierDoubleQuote
    .TextFileConsecutiveDelimiter = False
    .TextFileTabDelimiter = False
    .TextFileSemicolonDelimiter = False
    .TextFileCommaDelimiter = False
    .TextFileSpaceDelimiter = False
    .TextFileColumnDataTypes = Array(xlYMDFormat, 1, 1)
    .TextFileFixedColumnWidths = Array(22, 13, 13)
    .TextFileTrailingMinusNumbers = True
    .Refresh BackgroundQuery:=False
End With
strFile = Dir
Loop
End Sub
Sub-ImportManyTXTs_test()
作为字符串的Dim strFile
将ws设置为工作表
strFile=Dir(“I:\path\*.lev”)
执行While strFile vbNullString
设置ws=Sheets.Add
使用ws.QueryTables.Add(连接:=_
“TEXT;”和“I:\path\”&strFile,目标:=范围($A$1))
.Name=左(strFile,31)
.FieldNames=True
.rowNumber=False
.FillAdjacentFormulas=False
.PreserveFormatting=True
.refreshinfoleopen=False
.RefreshStyle=xlInsertDeleteCells
.SavePassword=False
.SaveData=True
.AdjustColumnWidth=True
.RefreshPeriod=0
.TextFilePromptOnRefresh=False
.TextFilePlatform=437
.TextFileStartRow=1
.TextFileParseType=xlFixedWidth
.TextFileTextQualifier=xlTextQualifierDoubleQuote
.textfileconsutivedelimiter=False
.TextFileTabDelimiter=False
.TextFileSemicolonDelimiter=False
.textfilecommadelimitor=False
.TextFileSpaceDelimiter=False
.TextFileColumnDataTypes=数组(xlYMDFormat,1,1)
.TextFileFixedColumnWidths=数组(22,13,13)
.TextFileTrailingMinusNumbers=True
.Refresh BackgroundQuery:=False
以
strFile=Dir
环
端接头
使用
LEFT()
函数仅获取文件名的前31个字符,如下所示:

Sub ImportManyTXTs_test()
Dim strFile As String
Dim ws As Worksheet
strFile = Dir("I:\path\*.lev")
Do While strFile <> vbNullString
Set ws = Sheets.Add
With ws.QueryTables.Add(Connection:= _
    "TEXT;" & "I:\path\" & strFile, Destination:=Range("$A$1"))
    .Name = LEFT(strFile,31)
    .FieldNames = True
    .RowNumbers = False
    .FillAdjacentFormulas = False
    .PreserveFormatting = True
    .RefreshOnFileOpen = False
    .RefreshStyle = xlInsertDeleteCells
    .SavePassword = False
    .SaveData = True
    .AdjustColumnWidth = True
    .RefreshPeriod = 0
    .TextFilePromptOnRefresh = False
    .TextFilePlatform = 437
    .TextFileStartRow = 1
    .TextFileParseType = xlFixedWidth
    .TextFileTextQualifier = xlTextQualifierDoubleQuote
    .TextFileConsecutiveDelimiter = False
    .TextFileTabDelimiter = False
    .TextFileSemicolonDelimiter = False
    .TextFileCommaDelimiter = False
    .TextFileSpaceDelimiter = False
    .TextFileColumnDataTypes = Array(xlYMDFormat, 1, 1)
    .TextFileFixedColumnWidths = Array(22, 13, 13)
    .TextFileTrailingMinusNumbers = True
    .Refresh BackgroundQuery:=False
End With
strFile = Dir
Loop
End Sub
Sub-ImportManyTXTs_test()
作为字符串的Dim strFile
将ws设置为工作表
strFile=Dir(“I:\path\*.lev”)
执行While strFile vbNullString
设置ws=Sheets.Add
使用ws.QueryTables.Add(连接:=_
“TEXT;”和“I:\path\”&strFile,目标:=范围($A$1))
.Name=左(strFile,31)
.FieldNames=True
.rowNumber=False
.FillAdjacentFormulas=False
.PreserveFormatting=True
.refreshinfoleopen=False
.RefreshStyle=xlInsertDeleteCells
.SavePassword=False
.SaveData=True
.AdjustColumnWidth=True
.RefreshPeriod=0
.TextFilePromptOnRefresh=False
.TextFilePlatform=437
.TextFileStartRow=1
.TextFileParseType=xlFixedWidth
.TextFileTextQualifier=xlTextQualifierDoubleQuote
.textfileconsutivedelimiter=False
.TextFileTabDelimiter=False
.TextFileSemicolonDelimiter=False
.textfilecommadelimitor=False
.TextFileSpaceDelimiter=False
.TextFileColumnDataTypes=数组(xlYMDFormat,1,1