S_Alvernaz
New Member
- Joined
- Aug 2, 2017
- Messages
- 3
Hi Everyone! I am realatively new to VBA and have been piecing together this code from other posted codes. However, I am having trouble with an Application DialogBox
The Project: I am designing a macro for a packing company that allows the user to select a folder and enter a date. The macro will copy all the rows (from the files in the folder the selected using folder picker) matching the entered date to a master spread sheet called the QueenSheet.
The Problem: The macro is only copying and pasting from the first file in the folder, but still opening and closing the following files. When I was first playing around with the inputbox, I put it below "DoEvents" and I had to enter the date for every file in the folder. It would only copy and paste from the first file if I put the same date for all of them, but would copy and paste from all of the files if I entered different dates for each file in the loop.
Any help or tips would be greatly appreciated. Thank!
- Suzi
The Project: I am designing a macro for a packing company that allows the user to select a folder and enter a date. The macro will copy all the rows (from the files in the folder the selected using folder picker) matching the entered date to a master spread sheet called the QueenSheet.
The Problem: The macro is only copying and pasting from the first file in the folder, but still opening and closing the following files. When I was first playing around with the inputbox, I put it below "DoEvents" and I had to enter the date for every file in the folder. It would only copy and paste from the first file if I put the same date for all of them, but would copy and paste from all of the files if I entered different dates for each file in the loop.
Code:
Sub Link_Data_FromOpenFile_To_QueenSheet()
Dim myPath As String
Dim myFile As String
Dim myExtension As String
Dim FldrPicker As FileDialog
Dim LSearchRow
Dim myDate As String
x = 34
'Optimize Macro Speed but not necessary
Application.ScreenUpdating = False
Application.EnableEvents = False
Application.Calculation = xCalculationManual
'Empty previous sheet
Windows("QueenSheet.xlsm").Activate
Range("A2:W50").ClearContents
'Retrieve Target Folder Path From User
Set FldrPicker = Application.FileDialog(msoFileDialogFolderPicker)
With FldrPicker
.Title = "Select Open Lots 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
myExtension = "*.xls*"
'First event is to copy the specific row from each open lot sheet
myDate = Application.InputBox("Enter Date", "Select Data to view by Date")
'Target Path with Ending Extension
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)
DoEvents
Do While Cells(x, 1) <> ""
If Cells(x, 1) = myDate Then
Rows(x).Copy
'Second event is to paste into the QueenSheet and continue down
Windows("QueenSheet.xlsm").Activate
Range("A" & Rows.Count).End(xlUp).Offset(1).PasteSpecial (xlPasteValues)
Application.CutCopyMode = False
End If
x = x + 1
Loop
'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 "All Done! Hit Ok"
ResetSettings:
'Reset Macro Optimization Settings
Application.EnableEvents = True
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
End Sub
Any help or tips would be greatly appreciated. Thank!
- Suzi