Do while loop for certain value

Sonya810

Registered User.
Local time
Today, 13:29
Joined
Mar 8, 2018
Messages
22
I have a Do While myfile <> "" which looks through a folder for all excel files and then formats them. But what code do I include if I don't want each file formatted if it has a certain value in A1. I still want the code to loop through each file, but only format the files if the value isn't in A1.
 
inside the loop...

Code:
workbooks.open vFilename
if range("A1").value = "my value" then
   'do this
else
   'do that
endif
 
Where would I put that? Here is my code

Option Compare Database

Function ProcessFiles()


Dim wb As Workbook
Dim myExtension As String
Dim FldrPicker As FileDialog
Dim fileName As String


'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)





'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


Columns("A:A").Select
Selection.Delete Shift:=xlToLeft
Rows("1:5").Select
Selection.Delete Shift:=xlUp
Columns("B:B").Select
Selection.Delete Shift:=xlToLeft
Selection.Delete Shift:=xlToLeft
Columns("C:C").Select
Selection.Delete Shift:=xlToLeft
Selection.Delete Shift:=xlToLeft
Selection.Delete Shift:=xlToLeft
Columns("E:E").Select
Selection.Delete Shift:=xlToLeft
Columns("D:D").Select
Selection.Delete Shift:=xlToLeft
Range("E7").Select


'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
End If
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 Function
 
Code:
Set wb = Workbooks.Open(fileName:=mypath & myfile)
'workbooks.open vFilename

if range("A1").value = "my value" then
   activeworkbook.close false
else
   'put your code here
endif
 
I get an error "Sorry, we couldn't find False.xlsx. Is it possible it was moved, renamed or deleted?"
 
The code loop to the first file and if the first file is already formatted it stops. I want it to loop through all of the files in the folder. If the first file is already formatted then move to the next file
 

Users who are viewing this thread

Back
Top Bottom