Hi Guys with the help of someone a lot more clued up than me on excel macros i got this macro that is supouse to check if a file exists and then based on the result stop and display a message "File Exists" or Copy, paste and clear some data before saving as a name partly based on a cell data. This macro is not completly finished and i am a bit stuck as the result i am getting all the time is the message box saying the "file exists" even when this is not the case. I am pulling my hair now because i have tried all sorts.
Could anyone help out please.
May thanks
Jsantos77
Could anyone help out please.
May thanks
Jsantos77
Code:
Public Function FileExists(sFullPath As String) As Boolean
If Not Dir(sFullPath, vbDirectory) = vbNullString Then FileExists = True
End Function
Public Sub TestFolderExistence()
If FileExists("MyFolder & NewFileName" & ".xls") Then
MsgBox "File exists!"
Else: MkDir ("MyFolder & NewFileName" & ".xls")
End If
End Sub
Public Sub TestFileExistenceF()
MyFolder = "C:\Users\Marta\Desktop\NPO\NPO_ "
NewFileName = Sheets("Hoja1").Range("A1").Value
Dim Sht As Variant
Dim Shts As Variant
Dim MyFolder As String
Dim NewFileName As String
If FileExists("MyFolder & NewFileName" & ".xls") Then
MsgBox "File exists!"
Else
Range("G6:G9").Copy
Range("A6").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("B6:G9").ClearContents
Range("B6").Select
Application.ScreenUpdating = True
ActiveWorkbook.SaveAs _
Filename:=MyFolder & NewFileName & ".xls", _
Password:="", _
WriteResPassword:="", _
ReadOnlyRecommended:=False, _
CreateBackup:=False
Application.ScreenUpdating = True
End If
End Sub