I'm new to VBA and I've practiced some code about delete Excel files in specified folder with condition (if cell A2 has no data then delete Excel file). And my code look like this:
Public Sub Deletefile()
Dim myfolder As String
Dim myfile As Variant
Dim i As Variant
myfolder = "C:\Users\Downloads\AttachmentFolder"
myfiles = Dir(myfolder & "*.xlsx")
For i = 1 To UBound(myfiles)
With Workbooks(i)
.Open
End With
If Workbooks(i).Range("A2").Count = 0 Then
Kill myfiles(i)
End If
Next
End Sub
I took the code above on the Internet and modified it but VBA just said "type mismatch". Please correct and explain where I'm wrong.