Hello, I`d like to import an xls file from a specific folder into my current workbook. whenever I want to use my pathline, the code stops but it works when I use open.file....Also, the code imports the selected file in a separate workbook, but I want it in the workbook that I`m currently working on but in a separate sheet so leave sheet 1 as is and start the process in sheet 2. here is my code (you can ignore the second half since its not related to my problem - i think). TIA!!
Sub ImportCopySheetAndAddFormulas()
Dim selectedFile As Variant
Dim srcWorkbook As Workbook
Dim srcWorksheet As Worksheet
Dim copyWorksheet As Worksheet
Dim timestampCell As Range
Dim timestampRow As Long
' Allow the user to select the XLS file
selectedFile = Application.GetOpenFilename("Excel Files (*.xls), *.xls")
If selectedFile = "False" Then
MsgBox "You did not select a file.", vbExclamation
Exit Sub
End If
' Import data from the XLS file
Set srcWorkbook = Workbooks.Open(selectedFile)
Set srcWorksheet = srcWorkbook.Sheets(1)
' Create a new sheet and copy the data to it in the same workbook
Set copyWorksheet = srcWorkbook.Sheets.Add(After:=srcWorkbook.Sheets(srcWorkbook.Sheets.Count))
srcWorksheet.UsedRange.Copy copyWorksheet.Range("A1")
' Find the row with the word "timestamp"
On Error Resume Next
Set timestampCell = copyWorksheet.Cells.Find(What:="timestamp", LookIn:=xlValues, LookAt:=xlWhole, MatchCase:=False)
On Error GoTo 0
If timestampCell Is Nothing Then
MsgBox "The word 'timestamp' was not found in the copied sheet.", vbExclamation
Exit Sub
Else
timestampRow = timestampCell.Row
End If
' Delete rows above the timestamp row
If timestampRow > 1 Then
copyWorksheet.Rows("1:" & timestampRow - 1).Delete Shift:=xlUp
End If
' Delete all columns after the second column
copyWorksheet.Columns("C:O").Delete Shift:=xlToRight
' Add the specified formulas
copyWorksheet.Range("E1").Value = "Time"
copyWorksheet.Range("F1").Value = "Average"
copyWorksheet.Range("I1").Value = "Time"
copyWorksheet.Range("J1").Value = "Average"
copyWorksheet.Range("L1").Value = "10 min avg"
copyWorksheet.Range("E2").Formula = "=AVERAGE(INDEX(A:A,1+60*(ROW()-ROW($E$2))):INDEX(A:A,60*(ROW()-ROW($E$2)+1)))"
copyWorksheet.Range("F2").Formula = "=AVERAGE(INDEX(B:B,1+60*(ROW()-ROW($F$2))):INDEX(B:B,60*(ROW()-ROW($F$2)+1)))"
copyWorksheet.Range("I2").Formula = "=OFFSET($E$2,(ROW(E1)-1)*10,0)"
copyWorksheet.Range("J2").Formula = "=OFFSET($F$2,(ROW(F1)-1)*10,0)"
copyWorksheet.Range("L3").Formula = "=AVERAGE(INDEX(F:F,1+10*(ROW()-ROW($L$3))):INDEX(F:F,10*(ROW()-ROW($L$3)+1)))"
' Format columns E and I to display time as HH:mm:ss AM/PM
copyWorksheet.Columns("E:E").NumberFormat = "HH:mm AM/PM"
copyWorksheet.Columns("I:I").NumberFormat = "HH:mm AM/PM"
'Format columns F, J, L to 2 decimal places"
copyWorksheet.Columns("F:F").NumberFormat = "0.00"
copyWorksheet.Columns("J:J").NumberFormat = "0.00"
copyWorksheet.Columns("L:L").NumberFormat = "0.00"
'Adjust column A to autofill
copyWorksheet.Columns("A").AutoFit
MsgBox "Data imported from the selected XLS file. A copy is created in the same workbook with rows above 'timestamp' removed and the specified formulas added.", vbInformation
End Sub