Public Function updateNameManager(name As Range)
Dim s As New cStringClass
Dim a As Range, cell As Range
On Error GoTo handleIt
For Each a In name
s.addAlphaNumeric a.Value
Set cell = a.Offset(0, 10)
Debug.Print s.TheString & " " & cell.Address
ThisWorkbook.Worksheets("Reagents").Names.Add NameLocal:=s.TheString, RefersTo:=cell <---- 1004 error
s.Clear
Next a
handleIt:
Debug.Print "update... " & Err.Number & " " & Err.Description
End Function
我在定义范围作为名称时遇到了问题。基本上这个函数接收一个范围说A2:A10。我将从每个范围创建名称,并将单元格设置为该名称。
当代码运行时,错误行被注释掉,它将列出每个单元格的内容。
即使我硬编码ReferensTo:=“= Reagents!$ K $ 2”它仍然出错。我想我需要以另一种方式引用工作表,但无法解决这个问题 1004应用程序定义或对象定义的错误
先谢谢
答案 0 :(得分:0)
试试这个:
有关详细信息,请参阅评论。
Sub test()
Dim rngTest As Range
'/ Add with Workbook Scope. (This will fail due to incorrect naming)
Set rngTest = Sheet1.Range("a5")
Debug.Print "Name Added: " & addName(rngTest, "###Test_WB", True)
'/ Add with worksheet Scope
Set rngTest = Sheet1.Range("b5")
Debug.Print "Name Added: " & addName(rngTest, "Test_WKS", False)
End Sub
Public Function addName(rngRangeTobeAdded As Range, strName As String, Optional bWorkbookLevel As Boolean = True) As Boolean
Dim bResult As Boolean
Dim wbParent As Workbook
Dim wksParent As Worksheet
bResult = False
On Error GoTo errHandler
If Not rngRangeTobeAdded Is Nothing And strName <> vbNullString Then
'/ Set Parent containers
Set wksParent = rngRangeTobeAdded.Parent
Set wbParent = wksParent.Parent
If Not bWorkbookLevel Then
'/ Add Local
wksParent.Names.Add strName, rngRangeTobeAdded
bResult = True
Else
'/ Add Global
wbParent.Names.Add strName, rngRangeTobeAdded
bResult = True
End If
End If
errHandler:
If Err.Number <> 0 Then
bResult = False
End If
addName = bResult
End Function