Advice on a potencial error when saving...

Helper monkey

Board Regular
Joined
Jun 23, 2002
Messages
63
Hi,

I've got a potencial dilema. I have a macro that saves a copy of a worksheet (if a check box is checked on exit). As i am leaving the macro in the hands of people who's IT skills is at best limited and have less VB experiance than me i can forsee a disaster on the horizon. I'm running excel 97 and if prompted to save using the recommomended file type i will loose all the userforms (and a lot of time and effort).

The macro saves a copy of the worksheet with the date and text from two textboxes as the title. Obviously if the file already exists there's going to be a clash. I do not want to replace the existing file but save it with a slighly different name (eg add a number - file name would be blabladate2.xls) or have a userform pop up asking the user to enter an alternate name. Could i attach this to an error line of code? i.e. if error then...

I could do with some advice on this problem and am running out of time to find a solution.

Thanks in advance!
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
You could check whether a similar named file exists in the particular directory and if so change the name.

e.g.

Lets assume your files are all held in C:

dim strFileName as String
dim i As Integer

i = 1
strFileName = "XYZ" & i
' check if file exists on C:
If (Dir$("c:" & strFilename) <> "") Then
i = i + 1
End if

ActiveWorkbook.SaveAs "C:" & strFileName



You might have to add a do while loop to keep adding numbers if this is used often and then file numbers keep growing.

Probably a better way but taht might get you started

Vodoo
 
Upvote 0
oops forgot to change the file name if it does exist

dim strFileName as String
dim i As Integer

i = 1
strFileName = "XYZ" & i
' check if file exists on C:\
If (Dir$("c:\" & strFilename) <> "") Then
i = i + 1
strFileName = "XYZ" & i
End if

ActiveWorkbook.SaveAs "C:\" & strFileName
 
Upvote 0
Hi,

I gave Vodoos suggestion a shot yesterday, using the code below, and the file is now saved with a "1" on the end. However, if the file already exists the "1" doesn't change to a "2" etc etc. What am i doing wrong?

Thanks in advance :confused:


Dim strFileName As String
Dim i As Integer

i = 1
strFileName = Range(" b1") & i
If (Dir$("C:\" & strFileName) <> "") Then
i = i + 1
strFileName = Range(" b1") & i
End If
ActiveWorkbook.SaveAs "C:\" & strFileName
 
Upvote 0
Try this Ive just added a do while loop instead of the If statement

Dim strFileName As String
Dim i As Integer

i = 1
strFileName = Range(" b1") & i
Do While (Dir$("C:\\" & strFileName) <> "")
i = i + 1
Loop
strFileName = Range(" b1") & i
End If
ActiveWorkbook.SaveAs "C:\\" & strFileName
 
Upvote 0
oops let me try that again.

Dim strFileName As String
Dim i As Integer

i = 1
strFileName = Range(" b1") & i
Do While (Dir$("C:\" & strFileName) <> "")
i = i + 1
strFileName = Range(" b1") & i
Loop
ActiveWorkbook.SaveAs "C:\" & strFileName

I have not actually tried this but the theory is it checks if the FileName1 exists, if so it checks FileName2, if so FileName3 right up until FileNameN, where N becomes the number of the new file.
 
Upvote 0
Your welcome HP, I think that will work now.

I believe it is 6:44pm in Oz but Im not 100% sure because actually I working in Japan.

Cheers

voodoo
 
Upvote 0

Forum statistics

Threads
1,214,651
Messages
6,120,744
Members
448,989
Latest member
mariah3

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