Sub TestVersion1()
'
' Test version of import loop v2
' Import of files from directory, but includes conversion to ANSI step. This macro requires conversion to ANSI to be performed and be present in the directory.
' Now includes sort of directory by sample number
'
Dim FolderName As String
Dim LR As Long
Dim wrkMyWorkBook As Workbook
Dim lngRow As Long: lngRow = 1
Dim lngColumn As Long: lngColumn = 2
Dim compoundsCopied As Boolean: compoundsCopied = False
i = 1
' Open dialog box to select folder and saves directory as folder name
With Application.FileDialog(msoFileDialogFolderPicker)
.AllowMultiSelect = False
If .Show = -1 Then
FolderName = .SelectedItems(1)
End If
End With
LookInTheFolder = FolderName
Set FileSystemObject = CreateObject("Scripting.FileSystemObject")
For Each SearchFolders In FileSystemObject.GetFolder(LookInTheFolder).SubFolders
Cells(i, 1) = SearchFolders
i = i + 1
Next SearchFolders
' Find and delete STDBY and Conversion directories from list in sheet1
Cells.Find(What:="STDBY", After:=ActiveCell, LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, _
SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False).Activate
Selection.Delete Shift:=xlUp
Cells.Find(What:="Conv", After:=ActiveCell, LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, _
SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False).Activate
Selection.Delete Shift:=xlUp
'Text to columns of directory
Sheets("Sheet1").Range("A1").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.TextToColumns Destination:=Range("A1"), DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=False, _
Semicolon:=False, Comma:=False, Space:=False, Other:=True, OtherChar _
:="\", FieldInfo:=Array(Array(1, 1), Array(2, 1), Array(3, 1), Array(4, 1), Array(5, _
1), Array(6, 1), Array(7, 1)), TrailingMinusNumbers:=True
' Adds formula to last column + 1 and auto fills down based on number of rows
Range("A1").End(xlToRight).Offset(, 1).Select
ActiveCell.FormulaR1C1 = "=VALUE(MID(RC[-1],7,FIND(""."",RC[-1])-6))"
LR = Cells.Find("*", , , , 1, 2).Row
If Selection(1).Row < LR Then
Selection.AutoFill Destination:=Selection.Resize(LR - Selection.Row + 1)
End If
' Gets last row number and last column number
LastRow = ThisWorkbook.Sheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row
LastCol = ThisWorkbook.Sheets("Sheet1").Cells(1, Columns.Count).End(xlToLeft).Column
' Create name for all sort area
Set SortA = ActiveSheet.Range(Cells(1, 1), Cells(LastRow, LastCol))
' Sort cells by last column
Range("A1").Select
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add Key:=Range(Worksheets("Sheet1").Cells(1, LastCol), Worksheets("Sheet1").Cells(LastRow, LastCol)), _
SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Sheet1").Sort
.SetRange SortA
.Header = xlGuess
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
' Selects and deletes sort row before formula concatenate step
Range("A1").Select
Selection.End(xlToRight).Select
Range(Selection, Selection.End(xlDown)).ClearContents
'Reverses text to columns
Const StartRow As Long = 1
Delimiter = "\"
LastRow = Cells(Rows.Count, "A").End(xlUp).Row
For X = StartRow To LastRow
LastCol = Cells(X, Columns.Count).End(xlToLeft).Column
If LastCol = 3 Then
Cells(X, "B").Value = Cells(X, "C").Value
Else
Cells(X, "A").Value = Join(Application.Index(Range(Cells(X, "A"), Cells(X, LastCol)).Value, 1, 0), Delimiter)
End If
Next
'Deletes left over columns from reverse of text to columns
Columns("B:M").Delete Shift:=xlToLeft
' Loops, opens first directory in list (sheet) and selects file name Report01.csv it opens
' this file and finds Ethylene and copies and paste it data -3 cells over into new sheet. Then closes Report01
Do Until Sheets("Sheet1").Range("A" & lngRow).Value = vbNullString
If Dir(Sheets("Sheet1").Range("A" & lngRow).Value & "\" & "REPORT01.CSV") <> "" Then
Set wrkMyWorkBook = Workbooks.Open(Filename:=Sheets("Sheet1").Range("A" & lngRow).Value & "\" & "REPORT01.CSV")
lngRow = lngRow + 1
If compoundsCopied = False Then
Windows("REPORT01.CSV").Activate
Cells.Find(What:="Ethylene", After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Windows("FID_V1.xls").Activate
Sheets("Sheet2").Select
Cells(2, 1).Select
ActiveSheet.Paste
compoundsCopied = True
End If
Windows("REPORT01.CSV").Activate
Cells.Find(What:="Ethylene", After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate
ActiveCell.Offset(, -3).Select
Range(Selection, Selection.End(xlDown)).Copy
Windows("FID_V1.xls").Activate
Sheets("Sheet2").Cells(2, lngColumn).Select
lngColumn = lngColumn + 1
ActiveSheet.Paste
wrkMyWorkBook.Close SaveChanges:=False
Else
Sheets("Sheet1").Cells(lngRow, 1).Delete
End If
Loop
' Text to columns of directory to get folder name slhas sample name
Sheets("Sheet1").Select
Range("A1").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.TextToColumns Destination:=Range("A1"), DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=False, _
Semicolon:=False, Comma:=False, Space:=False, Other:=True, OtherChar _
:="\", FieldInfo:=Array(Array(1, 1), Array(2, 1), Array(3, 1), Array(4, 1), Array(5, _
1), Array(6, 1), Array(7, 1)), TrailingMinusNumbers:=True
' Transpose samples names on to sheet 2
Range("A1").End(xlToRight).Select
Range(Selection, Selection.End(xlDown)).Copy
Sheets("Sheet2").Range("B1,B20").Select
Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:=False, Transpose:=True
' Format sheet 2 data for import to HPLC final sheet
Range("A1").Select
Cells.Find(What:="Ethylene", After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate
ActiveCell.Offset(, 1).Select
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlToRight)).Select
Selection.Replace What:="-", Replacement:="0", LookAt:=xlPart, SearchOrder:=xlByRows, _
MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
Application.CutCopyMode = False
Selection.NumberFormat = "0.00"
Range("B1").Select
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlToRight)).Select
End Sub