【问题标题】:Data Validation using named Ranges containing only strings, no addresses使用仅包含字符串,没有地址的命名范围进行数据验证
【发布时间】:2018-07-07 14:47:12
【问题描述】:

我正在为一组不太熟悉的 excel 用户创建一个工作簿/跟踪器,因此我已命名所有范围,如下图链接所示(抱歉,我没有足够的代表将其放入我的邮政)。他们的范围是工作簿。

我给它们都起了一个名字,即“TEST”和“TESTMEMBERS”,并在 UI 中手动填充了它们的内容 (Ctrl F3),这样用户可以根据需要轻松修改命名范围。它们的内容只是一个字符串,而不是引用工作簿中某处的一系列单元格。

例如:

“TESTMEMBERS”有以下数据(又名指):="Member1, Member2"

我的问题是如何从命名范围中获取数据以用于使用 VBA 对单元格进行数据验证?

我在 Set regOffice 处收到以下错误:

运行时错误“1004” 对象“_Worksheet”的方法“范围”失败

以下是我的代码中有问题的部分。值得注意的是,子是Worksheet_Change。如果您需要更多,请告诉我。我会很高兴地发布它。我已经包含了 DIM 部分,以确保我已经按照应有的方式设置了所有内容。



Dim regOffice As Range                   'Range which will contain all the regional offices
Dim hearingLoc As Range                 'Range which will contain the hearingLoc specific to the Reg Office
Dim wsTracker As Worksheet              'Worksheet var which will ref the Tracker Worksheet
Dim colCounter As Long                  'Counter
Dim i As Long


Dim hearingDates As Range
Dim day As Range
Dim tableHeaders As Range
Dim header As Range
Dim colLoc As Long
Dim colOpened As Long
Dim colScheduled As Long
Dim colRecording As Long
Dim colAdjourned As Long
Dim foundLocation As Range

Set wsTracker = Application.ActiveSheet


            Set regOffice = Range("TEST") 'The purpose here is to use regOffice range later and iterate through it's values.

            'Office Location Validation
            With Range("B24", "B100").Validation
                .Delete
                .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, _
                    Formula1:="=TEST"
                .IgnoreBlank = True
                .InCellDropdown = True
                .InputTitle = ""
                .ErrorTitle = ""
                .InputMessage = ""
                .ErrorMessage = ""
                .ShowInput = True
                .ShowError = False
            End With

            'Member Name Validation
            With Range("D24", "D100").Validation
                .Delete
                .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, _
                    Formula1:="=TESTMEMBERS"
                .IgnoreBlank = True
                .InCellDropdown = True
                .InputTitle = ""
                .ErrorTitle = ""
                .InputMessage = ""
                .ErrorMessage = ""
                .ShowInput = True
                .ShowError = False
            End With

Picture of how the Named Range is set-up

【问题讨论】:

  • 范围必须引用到单元格。您创建了一个不引用单元格的名称,当您尝试将其分配给 RegOffice 时,您会收到错误消息。这就解释了为什么你会得到错误。 ThisWorkbook.Names("TEST").RefersTo 之类的东西可能可以帮助您访问所需的信息,但我建议将所有成员放在单元格中,然后创建引用这些单元格的名称“TEST”。这样一来,您的代码应该可以工作
  • 感谢 Foxfire,你说得对,我创建了一个名字。我不知道那些存在。我不得不使用 Evaluate("TEST") 重构我的代码以将字符串放入 regOffice。从那里我 split("regOffice", ", ") 将值拆分为单个位置。同样,对于我的代码的 Formula1:= 部分,我不得不稍微改变一下,出现了一些语法错误。新代码只是再次使用了 Evaluate 函数: .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, _ Formula1:=Evaluate("TEST") 感谢您的帮助。我了解了一个新的 vba 对象 :)
  • 很高兴为您提供帮助。我会把它作为答案发布

标签: vba excel


【解决方案1】:

范围必须引用到单元格。您创建了一个不引用单元格的名称,当您尝试将其分配给 RegOffice 时,您会收到错误消息。这就解释了为什么你会得到错误。类似的东西

 ThisWorkbook.Names("TEST").RefersTo 

也许可以帮助您访问所需的信息,但我建议将所有成员放在单元格中,然后创建引用这些单元格的名称“TEST”。这样一来,您的代码应该可以工作

【讨论】:

    【解决方案2】:

    回答我自己的问题:(很抱歉,如果这不正确,如果遇到类似问题,最好让其他人知道)。

    经过大量研究,并在各地发布。确定它是一个名称而不是一个范围。

    我不得不重新编写一些代码并更改一些内容以使其使用数组和拆分函数工作,以使用 for 循环遍历数组。

    关于我发布的代码,这里是固定的,但我只保留了声明部分有变化的东西。

    Dim regOffice As Variant                  'Range which will contain all the regional offices
    Dim hearingLoc As Variant                'Range which will contain the hearingLoc specific to the Reg Office
    
    
            regOffice = Evaluate("TEST") 'The purpose here is to use regOffice range later and iterate through it's values.
    
            'Office Location Validation
            With Range("B24", "B100").Validation
                .Delete
                .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, _
                    Formula1:=Evaluate("TEST")
                .IgnoreBlank = True
                .InCellDropdown = True
                .InputTitle = ""
                .ErrorTitle = ""
                .InputMessage = ""
                .ErrorMessage = ""
                .ShowInput = True
                .ShowError = False
            End With
    
            'Member Name Validation
            With Range("D24", "D100").Validation
                .Delete
                .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, _
                    Formula1:=Evaluate("TESTMEMBERS")
                .IgnoreBlank = True
                .InCellDropdown = True
                .InputTitle = ""
                .ErrorTitle = ""
                .InputMessage = ""
                .ErrorMessage = ""
                .ShowInput = True
                .ShowError = False
            End With
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-09-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多