PDA

View Full Version : Code doesnt work as add-in



impius
10-21-2008, 08:07 AM
Hello all,

I have the code below, it works as a command button, but when I create it as an add-in, it does not do its job. It does not error, just doesnt delete the named ranges. Do you have to write code differently in an add-in?
Thanks in advance!



If Deleterange = True Then
MsgBox "Deleterange"
Dim oneName As Name, promptStr As String
For Each oneName In ThisWorkbook.Names
With oneName
If IsError(Evaluate(.RefersTo)) Then
promptStr = .Name & " evaluates to error" & vbCrLf & "Refers To: " & .RefersTo & vbCrLf & "Delete Name?"
If MsgBox(promptStr, vbYesNo) = vbYes Then
oneName.Delete
End If
End If
End With
Next oneName

End If

Bob Phillips
10-21-2008, 08:09 AM
Shouldn't it be referring to Activeworkbook? Thisworkbook will be the addin.

impius
10-21-2008, 08:13 AM
I tried it, unfortunately, same result...nothing. Seems like the code is correct, I don't understand!

:help

Bob Phillips
10-21-2008, 08:35 AM
I have just tried a simple test, not an addin, but with the code in another workbook, and it worked fine.

How do you invoke the procedure, and how does DeleteRange get set?

impius
10-21-2008, 08:49 AM
It is from a check box in a userform.

Yes...It works for me as well, if I put it in a command button. But as an add-in, it does not work, it runs through the code though.

impius
10-21-2008, 08:51 AM
Actuallyt it is not working in a button for me. I checked the ranges with the button hioghlighted and it doesnt show any when you do that.

impius
10-21-2008, 08:58 AM
Finally got this to work as an add-in...Thanks for the help...


If Deleterange = True Then
MsgBox "Deleterange"
'DEl Range Code
Dim nm As Name

On Error Resume Next
For Each nm In ActiveWorkbook.Names
nm.Delete
Next
On Error GoTo 0
'End DEl Range Code
End If

Bob Phillips
10-21-2008, 09:19 AM
But that names processed are very different. The first code deleted bad names, this deletes any, inclduing system names. The logic is the same though.