The following code opens a .csv file, finds “Trimmed Mean” in col B, uses the row of “Trimmed Mean” as the starting point to find the next “NC” value in column B and copies value one cell to the right of “NC” to the Workbook from which the code is executed (Sheet 1 col A).
The problem is that the code runs but the value is not copied to the sheet1. It’s probably just a minor thing but I can’t figure out what that is. Thanks for your help.
Const delim = vbTab 'for TAB delimited text files
Sub ImportMultipleTextFiles()
Dim wb As Workbook
Dim sFile As Variant
Dim LastRow As Long
Dim rngCell As Range
Dim varMyItem As String
varMyItem = "NC"
sFile = Application.GetOpenFilename("Text Files (*.csv),*.csv", , "Please select text file...")
Set wb = Workbooks.Open(Filename:=sFile)
Application.ScreenUpdating = False
wb.Sheets(1).Select
LastRow = ActiveSheet.Range("B" & Rows.Count).End(xlUp).Row
Debug.Print "LastRow = " & LastRow
Set aCell = ActiveSheet.Range("B1:B" & LastRow).Find(What:="Trimmed Mean", LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
Debug.Print "Trimmed Mean can be found in Row # " & aCell.Row
'wb.Sheets(1).Select
For Each rngCell In ActiveSheet.Range("B" & aCell.Row & ":B" & LastRow)
' Debug.Print ActiveSheet.Range("B" & aCell.Row & ":B" & LastRow)
If InStr(rngCell, "NC") > 0 Then
Debug.Print rngCell.Row
'
rngCell.Offset(0, 1).Copy Destination:=ThisWorkbook.ActiveSheet.Range("A" & ActiveSheet.Range("A" & Rows.Count).End(xlUp).Row + 1)
Exit For
End If
Next rngCell
wb.Close SaveChanges:=False
Set wb = Nothing
Application.ScreenUpdating = True
End Sub