Why saving in xlsx is not working, but in xls is? - vba

I made a code to help me save some files quickly in a folder optimasing an online exemple. When i save the file in xls format, everything looks normal, but when i do it in xlsx and try to open the saved file, an erro appears telling me that the format is corrupted.
All files where in xls in beginning
Sub LoopAllExcelFilesInFolder()
'PURPOSE: To loop through all Excel files in a user specified folder and perform a set task on them
'SOURCE: www.TheSpreadsheetGuru.com
Dim wb As Workbook
Dim myPath As String
Dim myFile As String
Dim myExtension As String
Dim FldrPicker As FileDialog
'Optimize Macro Speed
Application.ScreenUpdating = False
Application.EnableEvents = False
Application.Calculation = xlCalculationManual
'security biass
If Worksheets("atualizador").Range("H6") <> "x" Or Worksheets("atualizador").Range("H7") <> "x" Then
Exit Sub
End If
'start folder
myPath = "C:\Users\anna.costa\Downloads\Dados\"
'Target File Extension (must include wildcard "*")
myExtension = "*.xls"
'Target Path with Ending Extention
myFile = Dir(myPath & myExtension)
'Loop through each Excel file in folder
Do While myFile <> ""
'Set variable equal to opened workbook
Set wb = Workbooks.Open(Filename:=myPath & myFile)
'copy Worksheet's and rename
If Right(myFile, 5) <> ")" Then
Select Case Left(myFile, 1)
Case "V"
wb.SaveAs ("C:\Users\anna.costa\Desktop\Dados_FIPE\ANBIMA\VNA\" & setnameVNA(myFile) & ".xlsx")
wb.Close SaveChanges:=False
Case "m"
wb.SaveCopyAs ("C:\Users\anna.costa\Desktop\Dados_FIPE\ANBIMA\TÍTULO_PÚBLICO\" & setnameTP(myFile) & ".xls")
wb.Close SaveChanges:=False
Case "C"
wb.SaveCopyAs ("C:\Users\anna.costa\Desktop\Dados_FIPE\ANBIMA\ETTJ\" & setnameETTJ(myFile) & ".xlsx")
wb.Close SaveChanges:=False
End Select
End If
'Get next file name
myFile = Dir
Loop
'Message Box when tasks are completed
MsgBox "Task Complete!"
ResetSettings:
'Reset Macro Optimization Settings
Application.EnableEvents = True
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
End Sub

You are trying to open an xls file and save it as an xlsx file, without any conversion. To properly convert the file to xlsx you need to include the right FileFormat:
wb.SaveAs "C:\Users\anna.costa\Desktop\Dados_FIPE\ANBIMA\ETTJ\" & setnameETTJ(myFile) & ".xlsx", _
FileFormat:=xlOpenXMLWorkbook, AccessMode:=xlExclusive, ConflictResolution:=Excel.XlSaveConflictResolution.xlLocalSessionChanges
wb.Close SaveChanges:=False

I faced a similar situation, what I did was this
Set WBDesiredToConvert = ThisWorkbook
WBDesiredToConvert.SaveAs ThisWorkbook.Path & "\" & "MacroEnabled", 52

Related

VBA EXP file transformed to Excel file not allowing code to work

I hope you can help. The issue I am facing is this I have two pieces of code the first piece takes EXP files stored in a folder and transforms them into Excel files "xls" this piece of code is labelled CODE Piece 1 below.
This piece of code seems to work fine.
The second piece of code labelled CODE Piece 2 below. Allows a user to navigate to a folder select this folder then the code loops through all the files in this folder and deliminates the Excel files with | and then it should add new rows and columns. The issue is that the files deliminate no problem but the adding of rows and columns does not happen.
The odd thing is if I run this macro on workbooks that have not been through CODE Piece 1, Everything works fine the row and columns add.
But even if take out the deliminating part of the code and just just tell the macro to added the row and the columns and i run it on the files that have been transformed by CODE piece 1 again nothing. I don't get any errors, the macro runs but nothing happens.
If I even turn on the screen updating applications and I can see the macro doing its thing, adding the row and columns and saving the document but when I went to open the file back up i got the an error pop up (see error 1 below) once i clicked 'Yes' on the pop up the file would open but no added row or columns
I am at a loss as to how to solve this issue. I believe my coding is good but Ia m not getting the result I want. Can someone please advise.
As always any and all help is welcome.
CODE Piece 1
Public Sub Loop_Rename_Files_in_Folder()
Dim folder As String
Dim filename As String
folder = "C:\Users\CONNELLP\Desktop\Claire Macro\Reject Macro\Move Exceled\" 'MODIFY THIS LINE - FOLDER CONTAINING FILES TO BE RENAMED
If Right(folder, 1) <> "\" Then folder = folder & "\"
filename = Dir(folder & "*.exp")
Do While filename <> vbNullString
Name folder & filename As folder & Left(filename, InStrRev(filename, ".")) & "xls"
filename = Dir
Loop
End Sub
CODE Piece 2
Sub LoopAllExcelFilesInFolder()
'PURPOSE: To loop through all Excel files in a user specified folder and perform a set task on them
'SOURCE: www.TheSpreadsheetGuru.com
Dim wb As Workbook
Dim myPath As String
Dim myFile As String
Dim myExtension As String
Dim FldrPicker As FileDialog
Dim rng As Range
'Optimize Macro Speed
Application.ScreenUpdating = False
Application.EnableEvents = False
Application.Calculation = xlCalculationManual
'Retrieve Target Folder Path From User
Set FldrPicker = Application.FileDialog(msoFileDialogFolderPicker)
With FldrPicker
.Title = "Select A Target Folder"
.AllowMultiSelect = False
If .Show <> -1 Then GoTo NextCode
myPath = .SelectedItems(1) & "\"
End With
'In Case of Cancel
NextCode:
myPath = myPath
If myPath = "" Then GoTo ResetSettings
'Target File Extension (must include wildcard "*")
myExtension = "*.xl??*"
'Target Path with Ending Extention
myFile = Dir(myPath & myExtension)
'Loop through each Excel file in folder
Do While myFile <> ""
'Set variable equal to opened workbook
Set wb = Workbooks.Open(filename:=myPath & myFile)
'Ensure Workbook has opened before moving on to next line of code
DoEvents
'Change First Worksheet's Background Fill Blue
With wb
Set rng = Range("A:A")
rng.TextToColumns Destination:=rng, DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, _
Semicolon:=False, Comma:=False, Space:=False, Other:=True, _
OtherChar:="|"
Range("A1").EntireRow.Insert
Range("A:E").EntireColumn.Insert
End With
'Save and Close Workbook
wb.Close SaveChanges:=True
'Ensure Workbook has closed before moving on to next line of code
DoEvents
'Get next file name
myFile = Dir
Loop
'Message Box when tasks are completed
MsgBox "Task Complete!"
ResetSettings:
'Reset Macro Optimization Settings
Application.EnableEvents = True
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
End Sub
Error 1
Formatted
STUCK AS TXT
CODE that saves as new Workbook but leaves the corrupted Originals as is
CODE Piece 3
Sub OpenFiles()
'UpdateByExtendoffice20160623
Dim xStrPath As String
Dim xFileDialog As FileDialog
Dim xFile As String
Dim wb As Workbook
Set wb = ActiveWorkbook
'Optimize Macro Speed
Application.ScreenUpdating = False
Application.EnableEvents = False
Application.Calculation = xlCalculationManual
On Error Resume Next
Set xFileDialog = Application.FileDialog(msoFileDialogFolderPicker)
xFileDialog.AllowMultiSelect = False
xFileDialog.Title = "C:\Users\CONNELLP\Desktop\GAP Reports\"
If xFileDialog.Show = -1 Then
xStrPath = xFileDialog.SelectedItems(1)
End If
If xStrPath = "" Then Exit Sub
xFile = Dir(xStrPath & "\*.xl??")
Do While xFile <> ""
Workbooks.Open xStrPath & "\" & xFile
With wb
Set rng = Range("A:A")
rng.TextToColumns Destination:=rng, DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, _
Semicolon:=False, Comma:=False, Space:=False, Other:=True, _
OtherChar:="|"
Range("A1").EntireRow.Insert
Range("A:E").EntireColumn.Insert
Range("A1").Value = "Source File"
Range("B1").Value = "Reason"
Range("C1").Value = "Action"
Range("D1").Value = "Team"
Range("E1").Value = "Status"
ActiveWorkbook.SaveAs FileFormat:=xlExcel8, Password:="", WriteResPassword:="", _
ReadOnlyRecommended:=False, CreateBackup:=False
ActiveWorkbook.Close
End With
xFile = Dir
Loop
'Reset Macro Optimization Settings
Application.EnableEvents = True
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
End Sub
You should save the Workbooks in CODE Piece 1 as .xlsx instead of .xls (Win2003 Format). Also its enough to say myExtension = "*.xl*" for Excel Files. Your problem is when opening workbooks, that you dont address then worksheet correctly. Also what does DoEvents do? Its probably not necessary.
Code Piece 2 should look like this:
Sub LoopAllExcelFilesInFolder()
'PURPOSE: To loop through all Excel files in a user specified folder and perform a set task on them
'SOURCE: www.TheSpreadsheetGuru.com
Dim wb As Workbook
Dim myPath As String: myPath = ""
Dim myFile As String
Dim myExtension As String
Dim FldrPicker As FileDialog
Dim rng As Range
Dim sht As Worksheet
'Optimize Macro Speed
Application.ScreenUpdating = False
Application.EnableEvents = False
Application.Calculation = xlCalculationManual
'Retrieve Target Folder Path From User
Set FldrPicker = Application.FileDialog(msoFileDialogFolderPicker)
With FldrPicker
.Title = "Select A Target Folder"
.AllowMultiSelect = False
If .Show <> -1 Then GoTo NextCode
myPath = .SelectedItems(1) & "\"
End With
'In Case of Cancel
NextCode:
myPath = myPath
If myPath = "" Then GoTo ResetSettings
'Target File Extension (must include wildcard "*")
myExtension = "*.xl*"
'Target Path with Ending Extention
myFile = Dir(myPath & myExtension)
'Loop through each Excel file in folder
Do While myFile <> ""
'Set variable equal to opened workbook
Set wb = Workbooks.Open(myPath & myFile)
Set sht = wb.Worksheets(1) 'First Sheet (adjust if needed)
'Ensure Workbook has opened before moving on to next line of code
DoEvents
'Change First Worksheet's Background Fill Blue
Set rng = sht.Range("A:A")
rng.TextToColumns Destination:=rng, DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, _
Semicolon:=False, Comma:=False, Space:=False, Other:=True, _
OtherChar:="|"
With sht
.Range("A1").EntireRow.Insert
.Range("A:E").EntireColumn.Insert
End With
'Save and Close Workbook
wb.Close SaveChanges:=True
'Ensure Workbook has closed before moving on to next line of code
DoEvents
'Get next file name
myFile = Dir
Loop
'Message Box when tasks are completed
MsgBox "Task Complete!"
ResetSettings:
'Reset Macro Optimization Settings
Application.EnableEvents = True
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
End Sub

How to copy specific cells and paste to a new workbook

Sub Button3_Click()
'PURPOSE: To loop through all Excel files in a user specified folder and perform a set task on them
'SOURCE: www.TheSpreadsheetGuru.com
Dim wb As Workbook
Dim myPath As String
Dim myFile As String
Dim myExtension As String
Dim FldrPicker As FileDialog
'Optimize Macro Speed
Application.ScreenUpdating = False
Application.EnableEvents = False
Application.Calculation = xlCalculationManual
'Retrieve Target Folder Path From User
Set FldrPicker = Application.FileDialog(msoFileDialogFolderPicker)
With FldrPicker
.Title = "Select A Target Folder"
.AllowMultiSelect = False
If .Show <> -1 Then GoTo NextCode
myPath = .SelectedItems(1) & "\"
End With
'In Case of Cancel
NextCode:
myPath = myPath
If myPath = "" Then GoTo ResetSettings
'Target File Extension (must include wildcard "*")
myExtension = "*.xlsx"
'Target Path with Ending Extention
myFile = Dir(myPath & myExtension)
Set newWb = Workbooks.Add
With newWb
.SaveAs Filename:=myPath & Left(myFile, 5) & "_import.xlsx"
End With
'Loop through each Excel file in folder
i = 2
Do While myFile <> ""
'Set variable equal to opened workbook
Set wb = Workbooks.Open(Filename:=myPath & myFile)
Set newWb = Workbooks.Open(Filename:=myPath & Left(myFile, 5) & "_import.xlsx")
'Change First Worksheet's Background Fill Blue
wb.Sheets("Textual elements").Range("J11").Copy _
Destination:=newWb.Sheets("Sheet1").Cells(i, 1)
wb.Worksheets("Textual elements").Range("J31").Copy _
Destination:=newWb.Worksheets("Sheet1").Cells(i, 2)
i = i + 1
'Save and Close Workbook
newWb.Close SaveChanges:=True
'Get next file name
myFile = Dir()
Loop
'Message Box when tasks are completed
MsgBox "Task Complete!"
ResetSettings:
'Reset Macro Optimization Settings
Application.EnableEvents = True
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
End Sub
So I took the code from below site tried to edit according to my needs, but I get error italicized row in debugging mode.
The intention is to open a new workbook named as selected the folder and copy the cells to specific cells.
http://www.thespreadsheetguru.com/the-code-vault/2014/4/23/loop-through-all-excel-files-in-a-given-folder
The Range-object does not have a Paste method, so it cannot identify the Paste statement. Hence the error. You can use PasteSpecial though.
Try this:
replace:
wb.Worksheets("Textual elements").Range("J11").Copy
newWb.Worksheets("Sheet1").Range(Cells(i, 1)).Paste
wb.Worksheets("Textual elements").Range("J11").Copy
newWb.Worksheets("Sheet1").Range(Cells(i, 2)).Paste
with:
wb.Worksheets("Textual elements").Range("J11").Copy
newWb.Worksheets("Sheet1").Range(Cells(i, 1),Cells(i,2)).PasteSpecial
or without PasteSpecial:
wb.Worksheets("Textual elements").Range("J11").Copy newWb.Worksheets("Sheet1").Range(Cells(i, 1),Cells(i,2))
or even, if you must use Paste ;-):
wb.Worksheets("Textual elements").Range("J11").Copy
newWb.Worksheets("Sheet1").Range(Cells(i, 1),Cells(i,2)).select
ActiveSheet.Paste
I am still using Excel2002 so it doesn't play nice with xlsx files all the time. That said, you may not need the line
set newWb = Workbooks.Open(Filename:=myPath & Left(myFile, 5) & "_import.xlsx")
as I believe the workbook should open when it is added (possibly different in new versions).
When it comes to copying the range from one workbook to another
wb.Worksheets("Textual elements").Range("J11").Copy Destination:=newWb.Worksheets("Sheet1").Range(Cells(i, 1))
I am not sure if you have the Do While myFile <> "" loops set properly. You are ensuring myFile <> "" then doing something but not changing myFile's value and checking again that myFile <> ""
Also if you run this macro, it will overwrite the specified cells each time it is run so you will only have the most recent data - just incase that's not what you're trying to do.
EDIT
I still don't think you need to open the newwb when you have just opened it - might be best to step through the code using F8 & F9 to test that.
With regards the copying, I was right that you should use copy : destination, but hadn't noticed you were trying to range a cell. It should be either Range() OR cell(). Try this:
wb.Sheets("Textual elements").Range("J11").Copy _
Destination:=newWb.Sheets("Sheet1").Cells(i, 1)
wb.Worksheets("Textual elements").Range("J31").Copy _
Destination:=newWb.Worksheets("Sheet1").Cells(i, 2)
The space underscore ' _' at the end of copy is just to take the codee to a new line for ease of reading (so it doesn't go off the end of the page)
At the end of your code you are closing wb and saving changes even though you have not made any changes. I would change this to newwb and close wb without saving changes.

Open multiple excel files with user form

Write a code in vba that, Calling user form of one excel file to all the other 10 excel files without having any reference in those 10 excel files.
It is displaying the output in current excel file but not in the destination files and shows the error as Userform is already shown and showing form modally is not possible
Private Sub Workbook_OnClick()
Dim mypath As String
Dim file As Workbook
Dim wb As Workbook
Dim pat As String
Application.ScreenUpdating = False
ChDrive "C:"
ChDir "C:\Users\Administrator\Desktop\John"
'john is a folder on the desktop
mypath = Range("B1").Value
'mypath has the same value as chDir
file = Dir(mypath & "\" & "*.xlsx")
Set wb = Application.Workbooks.Open(file)
If (wb.Click) Then
Application.Visible = False
userform1.Show
End If
End Sub
chDir is mentioned because the default directory shown with the dir() function was C:\Users\Administrator\Documents\ but the folder saved in desktop and that is C:\Users\Administrators\Desktop\John
Sir, It is displaying the run time error - 91 that is "Object variable or with block variable is not set" and highlighting the line "file = Dir(mypath & "\" & "*.xlsx")"
Private Sub Workbook_OnClick()
Dim mypath As String
Dim file As String
Dim wb As Workbook
Dim pat As String
Application.ScreenUpdating = False
ChDrive "C:"
ChDir "C:\Users\Administrator\Desktop\John"
'john is a folder on the desktop
mypath = Range("B1").Value
'mypath has the same value as chDir
file = Dir(mypath & "\" & "*.xlsx")
Do While file <> ""
Set wb = Application.Workbooks.Open(file)
If Not IsEmpty(wb) Then
Application.Visible = False
userform1.Show
End If
wb.Close
file = Dir()
Loop
End Sub

Loop coding aborts unexpectedly

I am using a code to loop through all files in a user specified folder and perform a task.
The codes begins executing and then unexpectedly aborts. The first attempt aborted after about 40 files. The second attempt went as far as 177 files. Upon aborting the results to that point are appearing and are accurate.
Does anyone have any idea as to why it may be aborting and/or a different solution. the destination folder has about 7000 files needing data extracted. See existing code below.
Sub LoopAllExcelFilesInFolder()
'PURPOSE: To loop through all Excel files in a user specified folder and perform a set task on them
Dim wb As Workbook
Dim myPath As String
Dim myFile As String
Dim myExtension As String
Dim Folder As String
Dim MacroFile As String
Dim RowCTR As Integer
MacroFile = "Transportation Contact List.xlsm"
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
'Retrieve Target Folder Path From User
Set FldrPicker = Application.FileDialog(msoFileDialogFolderPicker)
With FldrPicker
.Title = "Select A Target Folder"
.AllowMultiSelect = False
If .Show <> -1 Then GoTo NextCode
myPath = .SelectedItems(1) & "\"
End With
'In Case of Cancel
NextCode:
myPath = myPath
If myPath = "" Then GoTo ResetSettings
'Target File Extension (must include wildcard "*")
myExtension = "*.xls"
'Target Path with Ending Extention
myFile = Dir(myPath & myExtension)
RowCTR = 2
'Loop through each Excel file in folder
Do While myFile <> ""
'Set variable equal to opened workbook
Set wb = Workbooks.Open(Filename:=myPath & myFile)
'Windows("\\ATLP3FILE5\shared\AITransport\AITFILES_mig-103009\AITUW\LDM\CIF").Activate
'CUT AND PASTE SECTION
Workbooks(myFile).Activate
Worksheets("CIF").Range("F5").Copy
Workbooks(MacroFile).Worksheets("Sheet1").Range("A" & RowCTR).PasteSpecial (xlPasteValues)
Workbooks(myFile).Activate
Worksheets("CIF").Range("h10").Copy
Workbooks(MacroFile).Worksheets("Sheet1").Range("B" & RowCTR).PasteSpecial (xlPasteValues)
Workbooks(myFile).Activate
Worksheets("CIF").Range("h12").Copy
Workbooks(MacroFile).Worksheets("Sheet1").Range("C" & RowCTR).PasteSpecial (xlPasteValues)
Workbooks(myFile).Activate
Worksheets("CIF").Range("D13").Copy
Workbooks(MacroFile).Worksheets("Sheet1").Range("D" & RowCTR).PasteSpecial (xlPasteValues)
Workbooks(myFile).Activate
Worksheets("CIF").Range("s64").Copy
Workbooks(MacroFile).Worksheets("Sheet1").Range("E" & RowCTR).PasteSpecial (xlPasteValues)
Workbooks(myFile).Activate
Worksheets("CIF").Range("Y5").Copy
Workbooks(MacroFile).Worksheets("Sheet1").Range("F" & RowCTR).PasteSpecial (xlPasteValues)
Workbooks(myFile).Activate
Worksheets("CIF").Range("X10").Copy
Workbooks(MacroFile).Worksheets("Sheet1").Range("G" & RowCTR).PasteSpecial (xlPasteValues)
Workbooks(myFile).Activate
Worksheets("CIF").Range("AB11").Copy
Workbooks(MacroFile).Worksheets("Sheet1").Range("H" & RowCTR).PasteSpecial (xlPasteValues)
Workbooks(myFile).Activate
Worksheets("CIF").Range("W9").Copy
Workbooks(MacroFile).Worksheets("Sheet1").Range("I" & RowCTR).PasteSpecial (xlPasteValues)
Workbooks(myFile).Activate
'Save and Close Workbook
wb.Close SaveChanges:=False
'Get next file name
myFile = Dir
RowCTR = RowCTR + 1
Loop
'Message Box when tasks are completed
MsgBox "Task Complete!"
ResetSettings:
'Reset Macro Optimization Settings
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
End Sub
I've taken your code and tightened it up by removing all of the workbook.activate commands. Likewise, I've used direct value transfer in place of the clipboard's copy & paste special, values.
Sub LoopAllExcelFilesInFolder()
'PURPOSE: To loop through all Excel files in a user specified folder and perform a set task on them
Dim wb As Workbook, wsMFS1 As Worksheet
Dim myPath As String
Dim myFile As String
Dim myExtension As String
Dim Folder As String
Dim MacroFile As String
Dim RowCTR As Integer
MacroFile = "Transportation Contact List.xlsm"
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
'Retrieve Target Folder Path From User
Set FldrPicker = Application.FileDialog(msoFileDialogFolderPicker)
With FldrPicker
.Title = "Select A Target Folder"
.AllowMultiSelect = False
If .Show <> -1 Then GoTo NextCode
myPath = .SelectedItems(1) & "\"
End With
'In Case of Cancel
NextCode:
myPath = myPath
If myPath = "" Then GoTo ResetSettings
'Target File Extension (must include wildcard "*")
myExtension = "*.xls"
'Target Path with Ending Extention
myFile = Dir(myPath & myExtension)
RowCTR = 2
Set wbMF = Workbooks(MacroFile).Worksheets("Sheet1")
'Loop through each Excel file in folder
Do While CBool(Len(myFile))
'Set variable equal to opened workbook
Set wb = Workbooks.Open(Filename:=myPath & myFile)
With wb.Worksheets("CIF")
'Windows("\\ATLP3FILE5\shared\AITransport\AITFILES_mig-103009\AITUW\LDM\CIF").Activate
'CUT AND PASTE SECTION
wsMFS1.Range("A" & RowCTR) = .Range("F5").Value
wsMFS1.Range("B" & RowCTR) = .Range("H10").Value
wsMFS1.Range("C" & RowCTR) = .Range("H12").Value
wsMFS1.Range("D" & RowCTR) = .Range("D13").Value
wsMFS1.Range("E" & RowCTR) = .Range("S64").Value
wsMFS1.Range("F" & RowCTR) = .Range("Y5").Value
wsMFS1.Range("G" & RowCTR) = .Range("X10").Value
wsMFS1.Range("H" & RowCTR) = .Range("AB11").Value
wsMFS1.Range("I" & RowCTR) = .Range("W9").Value
End With
'Save and Close Workbook
wb.Close SaveChanges:=False
Set wb = Nothing
'Get next file name
myFile = Dir
RowCTR = RowCTR + 1
Loop
Set wbMF = Nothing
'Message Box when tasks are completed
MsgBox "Task Complete!"
ResetSettings:
'Reset Macro Optimization Settings
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
End Sub
I don't know if that will save enough resource to finish off your long task but it should make some improvements.

PasteSpecial Method of Range Run Error

Sub LoopOtherRevenue()
Dim MyFile As String
Dim FilePath As String
FilePath = "C:\Users\jdubbaneh002\Desktop\Racetrac Other\"
MyFile = Dir(FilePath)
Do While Len(MyFile) > 0
If MyFile = "Book1.xlsm" Then
Exit Sub
End If
ActiveSheet.Range("A1:B14").Copy
Workbooks.Open (FilePath & MyFile)
ActiveWorkbook.Worksheets("A2) Monthly P&L (Source)").Activate
Range("B746:C759").PasteSpecial xlPasteValues
Application.CutCopyMode = False
ActiveWorkbook.Close
MyFile = Dir
Loop
End Sub
Get a paste special error on line Range("B746:C759").PasteSpecial xlPasteValues
The values are being pasted into a combo box. that is where the error is coming from.
For me it seems like the file path is missing a "\"
FilePath = "C:\Users\jdubbaneh002\Desktop\Racetrac Other"
...
MyFile = Dir(FilePath)
...
If MyFile = "Book1.xlsm" Then
...
Workbooks.Open (FilePath & MyFile)
Correct:
Workbooks.Open (FilePath & "\" & MyFile)
Did you try debugging? Where does it throw the error?
I can see all sorts of issues because you're using ActiveWorkbook after opening the 2nd workbook. Is ActiveWorkbook still pointing at the one where the code is, or is it actually pointing at the one you just opened?
Create & set an as Workbook variable as assign the one the code is in to one, and the workbook you're opening to the other. That will eliminate all confusion.
Try this:
Sub LoopOtherRevenue()
Dim rgCopy as Range
Dim MyFile As String
Dim FilePath As String
Dim wb as Workbook
FilePath = "destination folder\"
MyFile = Dir(FilePath)
Set rgCopy = ActiveSheet.Range("A1:B14")
Do While Len(MyFile) > 0
If MyFile = "Book1.xlsm" Then
Exit Sub
End If
set wb Workbooks.Open(FilePath & "\" & MyFile)
rgCopy.Copy Destination:=wb.Worksheets("A2) Monthly P&L (Source)").Range("B746")
wb.Close
MyFile = Dir
Loop
End Sub