我每天都使用Excel宏运行一个文本文件。但是,我想每天将此文件重命名为“ aa”。除非我正确输入此名称,否则宏将无法正常工作。
我在这里提到的是原始文件名。请注意,每天仅更改此文件日期。
文件名:20017122018.ASCII.TXT
给定名称每天都会更改,如下所述。
20017122018.ASCII.TXT
20118122018.ASCII.TXT
20219122018.ASCII.TXT
20320122018.ASCII.TXT
这是我录制的宏代码。我该如何纠正?我想用标准文件名替换aa.txt。
With ActiveSheet.QueryTables.Add(Connection:="TEXT;D:\aa.txt", Destination _
:=Range("$A$1"))
.Name = "aa"
.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 = True
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = False
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = Array(9, 2, 9, 1, 2, 9, 1, 1, 1, 1, 1, 1, 1, 1, 1, 9, 1, 9, 1, 9, 1, _
9)
.TextFileFixedColumnWidths = Array(4, 16, 7, 6, 16, 8, 4, 12, 3, 12, 3, 25, 13, 3, 4, 13, 1 _
, 1, 6, 7, 3)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
Cells.Select
Selection.AutoFilter
ActiveSheet.Range("$A$1:$O$828").AutoFilter Field:=6, Criteria1:="144"
Range("G54").Select
答案 0 :(得分:1)
此代码将仅显示所选文件夹中的*.ASCII.txt
个文件。
选择所需的文件,它将打开它。
这告诉它显示与Excel文件位于同一文件夹中的文件:
vFile = GetFile(ThisWorkbook.Path)
这告诉它显示默认文件位置中的文件:
vFile = GetFile()
然后使用wrkBk
引用文本文件。
这是整个过程:
Sub Test()
Dim vFile As Variant
Dim wrkBk As Workbook
vFile = GetFile()
If vFile <> "" Then
Set wrkBk = Workbooks.Open(vFile)
End If
End Sub
Function GetFile(Optional startFolder As Variant = -1) As Variant
Dim fle As FileDialog
Dim vItem As Variant
Set fle = Application.FileDialog(msoFileDialogFilePicker)
With fle
.Title = "Select a File"
.AllowMultiSelect = False
.Filters.Add "ASCII.TXT", "*.ASCII.txt", 1
If startFolder = -1 Then
.InitialFileName = Application.DefaultFilePath
Else
If Right(startFolder, 1) <> "\" Then
.InitialFileName = startFolder & "\"
Else
.InitialFileName = startFolder
End If
End If
If .Show <> -1 Then GoTo NextCode
vItem = .SelectedItems(1)
End With
NextCode:
GetFile = vItem
Set fle = Nothing
End Function