VBA Save as help

Vladgs

New Member
Joined
Nov 26, 2015
Messages
45
Hi everyone,
I need some help to achieve a task, I’ve tried getting code and adapting it but unfortunately couldn’t.

What I want to do is the following:

1 Check to see if file name already exists in a specific folder.

2 Filename is not the “Activewirkbook” but instead something like: Range("D2") & " " & Range("C3") & ".xlsx", FileFormat:=51 So I need to check if that file name exists.

3 If that file name exists add something like “V2”,”V3” etc

4 Hide all dialog boxes and warning from user.
I think this can be achieved by “Application.DisplayAlerts = False”

Looking forward to any help!

Regards,
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
Hi Mark,

No, the file name is composed by a concatenation between D2 and C3 and "xlsx", the file path is fixed, say "C\finance\Financial Operations\Special Access\PO model\".

Regards,
 
Upvote 0
Okay, then try like:

Rich (BB code):
Option Explicit
  
Public Sub Example()
Dim FSO     As Object ' Scripting.FileSystemObject
Dim fsoFile As Object ' Scripting.File
  
Dim sFixedPath  As String
Dim sFileName   As String
Dim n           As Long
  
  '// Alter to suit ///
  sFixedPath = ThisWorkbook.Path & "\"
  Set FSO = CreateObject("Scripting.FileSystemObject")
  
  '// Ensure folder exists  //
  If FSO.FolderExists(sFixedPath) Then
    
    Application.DisplayAlerts = False
    
    '// Leave the extension off of the filename //
    sFileName = Trim$(Sheet2.Range("D2").Value) & Trim$(Sheet2.Range("C3").Value)
    
    '// If no "version" is needed...      //
    If Not FSO.FileExists(sFixedPath & sFileName & ".xlsx") Then
      ThisWorkbook.SaveAs sFixedPath & sFileName, 51
    Else 'Else add "_ver" to the filename and...  //
      sFileName = sFileName & "_ver"
      n = 1
      '// ...loop until we don't find the proposed filename.  //
      Do While FSO.FileExists(sFixedPath & sFileName & Format(n, "000") & ".xlsx")
        n = n + 1
      Loop
      ThisWorkbook.SaveAs sFixedPath & sFileName & Format(n, "000"), 51
    End If
    
    '// Turn alerts back on as soon as possible //
    Application.DisplayAlerts = True
    
  End If
  
End Sub

Please note that I qualified the .Range with the worksheet's CodeName which is safer that using the name on the tab.

Hope that helps,

Mark
 
Upvote 0

Forum statistics

Threads
1,214,827
Messages
6,121,824
Members
449,050
Latest member
Bradel

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top