[color=darkblue]Sub[/color] Macro1()
[color=darkblue]Dim[/color] strFileName [color=darkblue]As[/color] [color=darkblue]String[/color]
[color=darkblue]Dim[/color] strPath [color=darkblue]As[/color] [color=darkblue]String[/color]
[color=darkblue]Dim[/color] strFile [color=darkblue]As[/color] [color=darkblue]String[/color]
[color=darkblue]Dim[/color] strExt [color=darkblue]As[/color] [color=darkblue]String[/color]
[color=darkblue]Dim[/color] i [color=darkblue]As[/color] [color=darkblue]Long[/color]
strPath = "C:\Test\"
strFile = "MyFile"
strExt = ".xls"
strFileName = strPath & strFile & strExt
[color=darkblue]Do[/color] [color=darkblue]Until[/color] [color=darkblue]Not[/color] FileInUse(strFileName) [color=green]'Test if file is in use[/color]
i = i - 1
strFileName = strPath & strFile & "(" & i & ")" & strExt
[color=darkblue]Loop[/color]
[color=darkblue]If[/color] Len(Dir(strFileName)) [color=darkblue]Then[/color] Kill strFileName
MsgBox strFileName, , "Save file useing this file name"
[color=green]'ActiveWorkbook.SaveAs Filename:=strFileName[/color]
[color=darkblue]End[/color] [color=darkblue]Sub[/color]
[color=darkblue]Public[/color] [color=darkblue]Function[/color] FileInUse(sFileName) [color=darkblue]As[/color] [color=darkblue]Boolean[/color]
[color=green]'http://stackoverflow.com/questions/9373082/detect-whether-excel-workbook-is-already-open-using-vba[/color]
[color=darkblue]On[/color] [color=darkblue]Error[/color] [color=darkblue]Resume[/color] [color=darkblue]Next[/color]
[color=darkblue]Open[/color] sFileName [color=darkblue]For[/color] [color=darkblue]Binary[/color] [color=darkblue]Access[/color] [color=darkblue]Read[/color] [color=darkblue]Lock[/color] [color=darkblue]Read[/color] [color=darkblue]As[/color] #1
[color=darkblue]Close[/color] #1
FileInUse = IIf(Err.Number > 0, [color=darkblue]True[/color], [color=darkblue]False[/color])
[color=darkblue]On[/color] [color=darkblue]Error[/color] [color=darkblue]GoTo[/color] 0
[color=darkblue]End[/color] [color=darkblue]Function[/color]