How to find what an error number means [{Solved}]

I try this:

If Err.Number = 58 Then
Range("B11").Value & "l" & ".xls"

I get an error:
It highlights the first & sign
It says compile error Expected: expression

Michael
 
Upvote 0

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
Code:
If Err.Number = 58 Then
Range("B11").Value & "l" & ".xls"

Looks like you're trying to create a string, but you're not actually *doing* anything with it. It's basically a random statement, there. What do you want to do with that string?
 
Upvote 0
I am doing a SaveAs.

Code:
ActiveWorkbook.SaveAs Filename:= _
        Path & _
Range("B11").Value & ".xls", FileFormat:=xlNormal _
        , Password:="", WriteResPassword:="", ReadOnlyRecommended:=False, _
        CreateBackup:=False
    If Err.Number = 58 Then
    Path & Range("B11").Value & "l " & ".xls"
    new_file = ActiveWorkbook.Name

Michael
 
Upvote 0
That's all well and good, but you are still effectively doing absolutely nothing with that bit of code. Ok, see how you used it in the ActiveWorkbook.SaveAs statement? It is fulfilling an expression there by returning the value to use as the workbook name.

The way you are using it here:
Code:
If Err.Number = 58 Then
    Path & Range("B11").Value & "l " & ".xls"
You are not doing anything. You aren't even saving it to a variable.

You need to tell the macro what to do with that string, not just leave it out in limbo like that.
 
Upvote 0
Ok So I should just do:

ActiveWorkbook.SaveAs Filename:= _
Path & _
Range("B11").Value & ".xls", FileFormat:=xlNormal _
, Password:="", WriteResPassword:="", ReadOnlyRecommended:=False, _
CreateBackup:=False
If Err.Number = 58 Then
ActiveWorkbook.SaveAs Filename:= _
Path & _
Range("B11").Value & "l" & ".xls", FileFormat:=xlNormal _
, Password:="", WriteResPassword:="", ReadOnlyRecommended:=False, _
CreateBackup:=False
new_file = ActiveWorkbook.Name

Just rewrite the hole thing over right??

Michael
 
Upvote 0
How about something like this?

Code:
Sub test()
Dim strPath As String, fname As String, i As Integer
strPath = "\\Sidonna\c\Estimating\"
fname = Range("B11") & "*.xls"
With Application.FileSearch
    .NewSearch
    .LookIn = strPath
    .Filename = fname
    .Execute
    i = .FoundFiles.Count
End With
If i > 0 Then
    fname = Range("B11") & i + 1 & ".xls"
    x = MsgBox("Your file already exists, do you want to make another copy?", vbYesNo)
Else
    fname = Range("B11") & ".xls"
End If
If x = vbYes Or i = 0 Then ActiveWorkbook.SaveAs strPath & fname
End Sub
 
Upvote 0
You could do that, but you shouldn't need to. Just check if the file already exists like Smitty suggested, then you only need to try at saving it once.

Hotpepper's example should work, but a simple example I cobbled together before I saw he posted that I believe this should work--I'm simply going with Dir instead of FileExists or FileSearch. This is definately not the *best* code, though. Hotpepper's is much better and adds some failsafes that I didn't:

Code:
Dim Path As String, NewFileName As String, i As long

i = 1
Path = "C:\"

NewFileName = Path & Range("B11").Value & ".xls"

'check if the file already exists
Do
    'if the file is not found at path specified
    If Dir(NewFileName) = "" Then
        'exit the loop
        Exit Do
    Else 'if file already exists at path specified
        'add 1 to the file name
        ' (if filename with '1' on the end exists, will try with 2, etc.)
        NewFileName = Path & Range("B11").Value & i & ".xls"
    End If
    i = i + 1
Loop

'save the file
ActiveWorkbook.SaveAs NewFileName
 
Upvote 0
Thank You for your responses :pray: :pray:
I ended up using HOTPEPPER's code:
Code:
Dim strPath As String, fname As String, i As Integer 
strPath = "\\Sidonna\c\Estimating\" 
fname = Range("B11") & "*.xls" 
With Application.FileSearch 
    .NewSearch 
    .LookIn = strPath 
    .Filename = fname 
    .Execute 
    i = .FoundFiles.Count 
End With 
If i > 0 Then 
    fname = Range("B11") & i + 1 & ".xls" 
    x = MsgBox("Your file already exists, do you want to make another copy?", vbYesNo) 
Else 
    fname = Range("B11") & ".xls" 
End If 
If x = vbYes Or i = 0 Then ActiveWorkbook.SaveAs strPath & fname

HOw in the world do you folks make this seem so easy :oops: :oops: :wink:

I wish I could write code like you folks...........


Thank You again and Happy Holidays!
Michael
 
Upvote 0

Forum statistics

Threads
1,216,030
Messages
6,128,413
Members
449,449
Latest member
Quiet_Nectarine_

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