saving a file (with a difference)

jaypatel

Active Member
Joined
Nov 25, 2002
Messages
389
Hi

have a file (spreadsheet) on our intranet, which is accessible to everyone within our organisation. The spreadsheet is a timesheet.

The problem i have is, i would like part of my script to say:
1, email the payroll dept (this is sorted)
2, but then do a file save as, within a particular directory. But, firstly to do a check if any files exist in the specified directory, if not, then do file save as timesheet1.xls, but essentially do a count of the number of files that exist in that directory, so the second file to be uploaded will be timesheet2.xls, and the 30th file would be timesheet30.xls. This is where i am struggling......(Shall we say that the new path is c:\payroll

I have worked out the Ch Dir macro part, but cannot work out if part 2 is possible.

many thanks in advance

Jay
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
You could use this code:

Code:
Sub saveas_Wigi()
    
    Const sDirectory As String = "C:\payroll\"
    
    ActiveWorkbook.SaveAs sDirectory & "timesheet" & Format(CountFiles(sDirectory, "timesheet*.xls") + 1, "00") & ".xls"

End Sub

Function CountFiles(Directory As String, sMask As String) As Long

    Dim lTemp As Long
    Dim MyName As String

    MyName = Dir(Directory & sMask, vbNormal)

    Do While MyName <> ""

        If MyName Like sMask Then lTemp = lTemp + 1
        MyName = Dir
        
    Loop

    CountFiles = lTemp

End Function
 
Upvote 0

Forum statistics

Threads
1,207,096
Messages
6,076,555
Members
446,213
Latest member
bettigb

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