So what I'm ultimately trying to do if take information from one workbook and export it to like 40 other workbooks. It seems to work but it's very slow. I have two issues... One is, is there any way you think you can improve the speed of the looping through files in the given folder? The second issue is trying to use an if statement for if cell F2 in workbook2 matches anywhere in column A of workbook1 then (do code), else go to next loop. I'm at a loss. Any help would be appreciated. It's a hot mess right, as I've tried using several different ways of doing things.
'Optimize Macro Speed
Application.ScreenUpdating = False
Application.AskToUpdateLinks = False
Application.DisplayAlerts = False
Application.EnableEvents = False
Application.Calculation = xlCalculationManual
Application.CalculateBeforeSave = False
'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)
'Loop through each Excel file in folder
Do While myFile <> ""
'Set variable equal to opened workbook
Set sht = ActiveWorkbook.ActiveSheet
Set wks = ActiveSheet
Set wb = Workbooks.Open(Filename:=MyPath & myFile)
'Ensure Workbook has opened before moving on to next line of code
DoEvents
myLookupValue = ActiveSheet.Range("F2")
Set rngToSearch = wks.Range("A:A")
Set rngCurrent = rngToSearch.Find(F2)
If rngCurrent Is Nothing Then
Else
rngCurrent.Offset(0, 1).Value = "Found"
MsgBox ("Was found")
End If
With ActiveSheet
LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
.Range("A" & LastRow + _
IIf(LastRow = 1 And .Range("A1") = "", 0, 1)).Value _
= Format(Now() - 1, "MM/DD/YYYY")
End With
sht.Activate
myFirstColumn = 1
myLastColumn = 8
colVol = 3
colHours = 4
colPH = 5
colMis = 6
myFirstRow = 1
myLastRow = 305
With ActiveSheet
Set myTableArray = .Range(.Cells(myFirstRow, myFirstColumn), .Cells(myLastRow, myLastColumn))
End With
VVol = WorksheetFunction.VLookup(myLookupValue, myTableArray, colVol, False)
VHours = WorksheetFunction.VLookup(myLookupValue, myTableArray, colHours, False)
VPH = WorksheetFunction.VLookup(myLookupValue, myTableArray, colPH, False)
VMis = WorksheetFunction.VLookup(myLookupValue, myTableArray, colMis, False)
Dim nameChecker As String
wb.Activate
'Volumn
With ActiveSheet
LastRow = .Cells(.Rows.Count, "B").End(xlUp).Row
.Range("B" & LastRow + _
IIf(LastRow = 1 And .Range("B1") = "", 0, 1)).Value _
= VVol
End With
'Hours
With ActiveSheet
LastRow = .Cells(.Rows.Count, "C").End(xlUp).Row
.Range("C" & LastRow + _
IIf(LastRow = 1 And .Range("C1") = "", 0, 1)).Value _
= VHours
End With
'PH
With ActiveSheet
LastRow = .Cells(.Rows.Count, "D").End(xlUp).Row
.Range("D" & LastRow + _
IIf(LastRow = 1 And .Range("D1") = "", 0, 1)).Value _
= VPH
End With
'Mis
With ActiveSheet
LastRow = .Cells(.Rows.Count, "E").End(xlUp).Row
.Range("E" & LastRow + _
IIf(LastRow = 1 And .Range("E1") = "", 0, 1)).Value _
= VMis
End With
ActiveWorkbook.Save
ActiveWorkbook.Close
'Next
'Ensure Workbook has closed before moving on to next line of code
DoEvents
myFile = Dir
Loop
'Message Box when tasks are completed
MsgBox "Task Complete!"
ResetSettings:
'Reset Macro Optimization Settings
Application.EnableEvents = True
Application.AskToUpdateLinks = True
Application.DisplayAlerts = True
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
Application.CalculateBeforeSave = True
End Sub
Bookmarks