简体   繁体   English

如何使用VBA为Excel单元格分配名称?

[英]How to assign a name to an Excel cell using VBA?

I need to assign a unique name to a cell which calls a particular user defined function. 我需要为调用特定用户定义函数的单元格分配一个唯一的名称。

I tried 我试过了

Dim r As Range
set r = Application.Caller

r.Name = "Unique"

The following code sets cell A1 to have the name 'MyUniqueName': 以下代码将单元格A1设置为名称“ MyUniqueName”:

Private Sub NameCell()

Dim rng As Range
Set rng = Range("A1")
rng.Name = "MyUniqueName"

End Sub

Does that help? 有帮助吗?

EDIT 编辑

I am not sure how to achieve what you need in a simple way, elegant way. 我不确定如何以简单,优雅的方式实现您的需求。 I did manage this hack - see if this helps but you'd most likely want to augment my solution. 我确实管理了这种黑客行为-看看是否有帮助,但是您很可能想扩大我的解决方案。

Suppose I have the following user defined function in VBA that I reference in a worksheet: 假设我在工作表中引用了以下VBA中的用户定义函数:

Public Function MyCustomCalc(Input1 As Integer, Input2 As Integer, Input3 As Integer) As Integer

MyCustomCalc = (Input1 + Input2) - Input3

End Function

Each time I call this function I want the cell that called that function to be assigned a name. 每次我调用此函数时,我都希望为调用该函数的单元分配一个名称。 To achieve this, if you go to 'ThisWorkbook' in your VBA project and select the 'SheetChange' event then you can add the following: 为此,如果您在VBA项目中转到“ ThisWorkbook”并选择“ SheetChange”事件,则可以添加以下内容:

Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
If Left$(Target.Formula, 13) = "=MyCustomCalc" Then
    Target.Name = "MyUniqueName"
End If
End Sub

In short, this code checks to see if the calling range is using the user defined function and then assigns the range a name (MyUniqueName) in this instance. 简而言之,这段代码检查调用范围是否正在使用用户定义的函数,然后在此实例中为该范围分配一个名称(MyUniqueName)。

As I say, the above isn't great but it may give you a start. 就像我说的那样,上面的方法不是很好,但是它可能会让您有所收获。 I couldn't find a way to embed code into the user defined function and set the range name directly eg using Application.Caller.Address or Application.Caller.Cells(1,1) etc. I am certain there is a way but I'm afraid I am a shade rusty on VBA... 我找不到将代码嵌入用户定义的函数中并直接设置范围名称的方法,例如使用Application.Caller.AddressApplication.Caller.Cells(1,1)等。我敢肯定有一种方法,但我恐怕我对VBA感到有些生疏...

I used this sub to work its way across the top row of a worksheet and if there is a value in the top row it sets that value as the name of that cell. 我使用了此子项来跨工作表的第一行,如果第一行中有一个值,则将该值设置为该单元格的名称。 It is VBA based so somewhat crude and simple, but it does the job!! 它是基于VBA的,所以有些粗糙和简单,但是可以完成工作!!

Private Sub SortForContactsOutlookImport()

    Dim ThisCell As Object
    Dim NextCell As Object
    Dim RangeName As String

    Set ThisCell = ActiveCell
    Set NextCell = ThisCell.Offset(0, 1)

    Do
        If ThisCell.Value <> "" Then
            RangeName = ThisCell.Value
            ActiveWorkbook.Names.Add Name:=RangeName, RefersTo:=ThisCell
            Set ThisCell = NextCell
            Set NextCell = ThisCell.Offset(0, 1)
        End If

    Loop Until ThisCell.Value = "Web Page"

End Sub

I use this sub, without formal error handling: 我使用此子程序,没有进行正式的错误处理:

Sub NameAdd()

    Dim rng As Range
    Dim nameString, rangeString, sheetString As String

    On Error Resume Next

    rangeString = "A5:B8"
    nameString = "My_Name"
    sheetString = "Sheet1"

    Set rng = Worksheets(sheetString).Range(rangeString)

    ThisWorkbook.Names.Add name:=nameString, RefersTo:=rng

End Sub

To Delete a Name: 删除名称:

Sub NameDelete()
    Dim nm As name

    For Each nm In ActiveWorkbook.Names
        If nm.name = "My_Name" Then nm.Delete
   Next

End Sub

您可以继续使用Range("A1") = "Unique"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM