[英]Extract text from within a cell and paste text in corresponding column using vba
I have a spreadsheet that has data organized like so: 我有一个电子表格,其数据组织如下:
The user will insert multiple or as many text files as they want therefore each time the user imports a new text file the data gets placed beneath the previous import so the filepath, depth, A0, A180, etc are all present each time a text file is imported. 用户将根据需要插入多个或任意数量的文本文件,因此,每次用户导入新的文本文件时,数据都将放置在上一个导入文件的下方,因此每次文本文件时都会显示文件路径,深度,A0,A180等是进口的。 I want to be able to get the corresponding date and time (2003-11-03 17-52-04) for each file under the column 'Reading Date'.
我希望能够为“读取日期”列下的每个文件获取相应的日期和时间(2003-11-03 17-52-04)。 However I'm not sure how to go about this.
但是我不确定该怎么做。
Any tips/help would be greatly appreciated! 任何提示/帮助将不胜感激!
Here is my code to import the data: 这是我导入数据的代码:
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
You can try adding this near the end of the With
block: 您可以尝试在
With
块的末尾添加以下内容:
.Refresh BackgroundQuery:=False
.ResultRange.Columns(.ResultRange.Columns.Count + 1) = Now
End With
I got it to do what I need it to do with the following code: 我用下面的代码来做我需要做的事情:
Sub Import_Textfiles() Dim fName As String, LastRow As Long Sub Import_Textfiles()昏暗fName作为字符串,LastRow作为长
LastRow = Range("A" & Rows.Count).End(xlUp).Row + 2 LastRow = Range(“ A”&Rows.Count).End(xlUp).Row + 2
fName = Application.GetOpenFilename("Text Files (*.txt), *.txt") fName = Application.GetOpenFilename(“文本文件(* .txt),*。txt”)
If fName = "False" Then Exit Sub 如果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 结束子
Thanks for the other suggestions!! 感谢其他建议!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.