Justplainj
Board Regular
- Joined
- Apr 15, 2021
- Messages
- 50
- Office Version
- 365
- Platform
- Windows
Hi All
I have the following code and am not able to find the error.
Quite a simple code. It clears the main sheet, then opens workbooks in a saved location and then copies the data.
However, once it gets to the line "Set owb = Workbooks.Open(sPath & sFil)" which opens the workbook it gives me a runtime error.
It does open the workbook before giving the error, but due to the error it does not continue.
I used this code before and had not issues until now.
the location where the documents are saved is within the documents folder on my PC which is backed up to one drive, in case that information is usefull.
I am using the most up to date office 365 on Windows 11.
Thanks J
I have the following code and am not able to find the error.
Quite a simple code. It clears the main sheet, then opens workbooks in a saved location and then copies the data.
However, once it gets to the line "Set owb = Workbooks.Open(sPath & sFil)" which opens the workbook it gives me a runtime error.
It does open the workbook before giving the error, but due to the error it does not continue.
I used this code before and had not issues until now.
the location where the documents are saved is within the documents folder on my PC which is backed up to one drive, in case that information is usefull.
I am using the most up to date office 365 on Windows 11.
Thanks J
VBA Code:
Option Explicit
Sub OpenRunCode() 'Open files run Excel VBA macro
Dim sPath As String
Dim sFil As String
Dim owb As Workbook
Dim LastRow As Long
Dim X As Integer
Dim sht As String
Worksheets("CombinedLeaveTaken").Select
sPath = "redacted" 'Gets folder location reports are saved to
If Right(sPath, 1) <> "\" Then sPath = sPath & "\" 'Checks if the location ends with a backslash
sFil = Dir(sPath & "*.xl*") 'Captures all XL files in a directory.
Worksheets("CombinedLeaveTaken").Select
Range("A2:J2").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.EntireRow.Delete
Range("A1").Select
Do While sFil <> "" 'Loop through all files in Folder
Application.AskToUpdateLinks = False
Application.DisplayAlerts = False
Set owb = Workbooks.Open(sPath & sFil) 'opens the workbook. make sure it is not encrypted or protected with a password
Windows(sFil).Activate 'Activates workbook within folder specified
Cells.Select
Selection.UnMerge
'Adds column before column A
Columns("A:A").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Range("A8").Select
ActiveCell.FormulaR1C1 = "Company"
'specify company name
Range("A10").Select
ActiveCell.Formula = "=RIGHT($B$2,LEN($B$2)-10)"
Range("A10").Select
Selection.AutoFill Destination:=Range(Selection, Selection.End(xlDown))
Range("A10").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
'delete unneccesary rows
Rows("9:9").Select
Application.CutCopyMode = False
Selection.Delete Shift:=xlUp
'Select range to copy
Range("A9:J9").Select
'Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
'pastes information to the main workbook
ThisWorkbook.Activate
LastRow = Cells(Rows.Count, 1).End(xlUp).Row
Cells(LastRow, 1).Offset(1, 0).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Workbooks(sFil).Close SaveChanges:=False
Application.DisplayAlerts = True
Application.AskToUpdateLinks = True
sFil = Dir
Loop
'Removes Duplicates
Range("A2:H2").Select
ActiveSheet.Range(Selection, Selection.End(xlDown)).RemoveDuplicates Columns:=Array(1, 2, 3, 4, 5 _
, 6, 7, 8), Header:=xlYes
Range("A1").Select
ThisWorkbook.Save
End Sub