简体   繁体   English

Excel VBA-定义命名范围

[英]Excel VBA - define named ranges

For each cell with content in range C4:C53 of worksheet "Size Selection", I want to create a new named range, and the name of that range should be the same as the content of that cell. 对于工作表“大小选择”的范围C4:C53中内容的每个单元格,我想创建一个新的命名范围,并且该范围的名称应与该单元格的内容相同。

Each of the named ranges should be formula driven, and has a variable that depends on the row of the cell. 每个命名范围应由公式驱动,并具有取决于单元格行的变量。

I've tried coding this, but keep getting errors; 我已经尝试过对此进行编码,但是仍然会出错。 would you please take a look and help me resolve the issue? 您能否看一下并帮助我解决问题?

Private Sub Worksheet_Change(ByVal Target As Range)

    If Not Intersect(Target, Range("A1:F20000")) Is Nothing Then

    Dim i As Integer
    Dim range_name As String


    For i = 4 To 53

    If Sheets("Size Selection").Cells(i, 3) <> "" Then

    Range(range_name).Formula = "=OFFSET('Size Selection'!$F$" & i & ", 0, 0, 1, COUNT(IF('Size Selection'!$F$" & i & ":$AZ$" & i & "="", "", 1)))"

    Range(range_name).Name = Sheets("Size Selection").Cells(i, 3)

    End If

    Next

    End If

End Sub

Many thanks for your help! 非常感谢您的帮助!

Based upon your comments in the discussion below, here's what I think you're after: 根据您在以下讨论中的评论,以下是我认为的建议:

Private Sub Worksheet_Change(ByVal Target As Range)

    If Not Intersect(Target, Range("A1:F20000")) Is Nothing Then

    Dim i As Integer
    Dim range_name As String


    For i = 4 To 53

      If Sheets("Size Selection").Cells(i, 3) <> "" Then

       ActiveWorkbook.Names.Add _
          Name:= Sheets("Size Selection").Cells(i, 3).Text, _
          RefersToR1C1:="=OFFSET('Size Selection'!$F$" & i & ", 0, 0, 1, COUNT(IF('Size Selection'!$F$" & i & ":$AZ$" & i & "="", "", 1)))"

      End If

    Next

    End If

End Sub

It might be wrong on the RefersToR1C1 part, but it should at least now make sense to you on how to proceed. RefersToR1C1部分上可能是错误的,但至少现在应该对您有意义。

Hope this does the trick!! 希望这能解决问题!!

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

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