View Single Post
  #5   Report Post  
Posted to microsoft.public.excel.programming
Kevin O'Neill[_2_] Kevin O'Neill[_2_] is offline
external usenet poster
 
Posts: 44
Default Error if 'name' already exists

Hey Tom, that would be very true. Being as new as I am to to VBA, that
didn't cross my mind (1st proj. ever...) I suppose the easiest way is
to just show you all the code. This worksheet change event is triggered
when an "add" button is hit, and creats 4 rows worth of info, 1 of the
cells in 4 rows is the 'name' to be added and checked.

Sub Worksheet_Change(ByVal Target As Excel.Range)

Dim myrow&, myCol&
myrow = Target.Row
myCol = Target.Column

If myrow 0 And myrow < 100 Then
If myCol 0 And myCol < 100 Then
Application.EnableEvents = False

'Load Case Name Range adder.

Dim bubba As Integer, bubba2 As Integer, i As Integer
bubba = Range("Bookmark").Offset(2, 0).Row
bubba2 = Range("B" & Rows.Count).End(xlUp).Row

For i = bubba To bubba2 Step 4
Dim myValue
myValue = Cells(i, 2).Value

'start your code
Dim nm As name
On Error Resume Next
Set nm = thisworkbooks.Names(Target.Value)
On Error GoTo 0
If Not nm Is Nothing Then
MsgBox "Name " & Target.Value & " already exists"
Exit Sub
End If
'end your code

ActiveWorkbook.Names.Add name:=myValue,
RefersTo:="='Loads'!$B$" & i
Next i

Application.EnableEvents = True
End If
End If


End Sub