我有一个电子表格,其数据组织如下:
用户将根据需要插入多个或多个文本文件,因此每次用户导入新文本文件时,数据都会放在上一个导入下面,因此每次都会出现文件路径,深度,A0,A180等导入文本文件。 我希望能够在“阅读日期”栏下的每个文件中获得相应的日期和时间(2003-11-03 17-52-04)。但是,我不确定如何解决这个问题。
任何提示/帮助将不胜感激!
以下是导入数据的代码:
Sub Import_Textfiles()
Dim fName As String, LastRow As Long
LastRow = Range("A" & Rows.Count).End(xlUp).Row + 2
fName = Application.GetOpenFilename("Text Files (*.txt), *.txt")
If fName = "False" Then Exit Sub
With ActiveSheet.QueryTables.Add(Connection:="TEXT;" & fName, _
Destination:=Range("A" & LastRow))
.Name = "2001-02-27 14-48-00"
.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(1, 1, 1, 1, 1, 1, 1)
.TextFileFixedColumnWidths = Array(14, 14, 8, 16, 12, 14)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
Range("W16").Select
ActiveWindow.SmallScroll Down:=0
Dim strShortName As String
Dim strInitialDir As String
'Adding Updating Location to Excel Sheet:
Dim sourceCol As Integer, rowCount As Integer, currentRow As Integer
Dim currentRowValue As String
sourceCol = 1 'column A has a value of 1
rowCount = Cells(Rows.Count, sourceCol).End(xlUp).Row
strShortName = fName
'for every row, find the first blank cell and select it
For currentRow = 1 To rowCount
currentRowValue = Cells(currentRow, sourceCol).Value
If IsEmpty(currentRowValue) Or currentRowValue = "" Then
Cells(currentRow, sourceCol).Select
Cells(currentRow, sourceCol) = ("Updating Location: " & strShortName)
End If
Next
End Sub
答案 0 :(得分:0)
您可以尝试在With
区块的末尾添加此内容:
.Refresh BackgroundQuery:=False
.ResultRange.Columns(.ResultRange.Columns.Count + 1) = Now
End With
答案 1 :(得分:0)
我按照以下代码做了我需要它做的事情:
Sub Import_Textfiles() Dim fName As String,LastRow As Long
LastRow = Range(“A”& Rows.Count).End(xlUp).Row + 2
fName = Application.GetOpenFilename(“Text Files(* .txt),* .txt”)
如果fName =“False”则退出Sub
With ActiveSheet.QueryTables.Add(Connection:="TEXT;" & fName, _
Destination:=Range("A" & LastRow))
.Name = "2001-02-27 14-48-00"
.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(1, 1, 1, 1, 1, 1, 1)
.TextFileFixedColumnWidths = Array(14, 14, 8, 16, 12, 14)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
Range("W16").Select
ActiveWindow.SmallScroll Down:=0
Dim strShortName As String
Dim strInitialDir As String
'Adding Updating Location to Excel Sheet:
Dim sourceCol As Integer, rowCount As Integer, currentRow As Integer
Dim currentRowValue As String
Dim fileDate1 As String
Dim fileDate2 As String
sourceCol = 1 'column A has a value of 1
rowCount = Cells(Rows.Count, sourceCol).End(xlUp).Row
strShortName = fName
fileDate1 = Mid(fName, InStrRev(fName, "\") + 1)
fileDate2 = Left(fileDate1, 19)
'for every row, find the first blank cell and select it
For currentRow = 1 To rowCount
currentRowValue = Cells(currentRow, sourceCol).Value
If IsEmpty(currentRowValue) Or currentRowValue = "" Then
Cells(currentRow, sourceCol).Select
Cells(currentRow, sourceCol) = ("Updating Location: " & strShortName)
Cells((currentRow + 1), (sourceCol + 7)).Select
Cells((currentRow + 1), (sourceCol + 7)) = "Reading Date"
Cells((currentRow + 1), (sourceCol + 7)).Select
Cells((currentRow + 2), (sourceCol + 7)) = fileDate2
End If
Next
End Sub
感谢其他建议!!