【问题标题】:Refer to Named Range from Cell Value in For Loop请参阅 For 循环中单元格值的命名范围
【发布时间】:2017-07-05 13:18:42
【问题描述】:

大家下午好,

自从我转向其他语言编程以来,我对 VBA 有点生疏了,所以我希望有人能够帮助我。

我要做什么

我有两张床单。一个是用户填写的表单页面,另一个(Sheet1 ....我没有命名)基本上是一个数据页面。

在 Sheet1 中,我有一个表格显示 1 或 0,具体取决于表格上的范围是否具有特定选择。单元格值上的两列表示需要输入消息的范围。宏应该找到所有的 1,查找找到两列的命名范围并插入命名范围。不幸的是,我不断收到应用程序或对象定义的错误。我的代码如下:

PcentData = Sheets("Sheet1").Range("PcentData").Value
    If PcentData > 0 Then
        For Each pCell In Sheets("Sheet1").Range("PcentSwitch")
            If pCell.Value = 1 Then
                With Sheets("Payment-Deduction Form").Range(Cells(pCell.Row, pCell.Column + 2)).Validation 'Error here
                .Add Type:=xlValidateInputOnly
                .InputTitle = "Test"
                .InputMessage = "Test"

                End With
            End If

        Next pCell
    End If

编辑:

我已经设法确保代码通过定义一个名为 NamedRange 的字符串并将其与旧的 with 语句相同,指向正确的工作表,从而从正确的工作表中提取命名范围。

Dim NamedRange As String
PcentData = Sheets("Sheet1").Range("PcentData").Value
    If PcentData > 0 Then
        For Each pCell In Sheets("Sheet1").Range("PcentSwitch")
            If pCell.Value = 1 Then
                NamedRange = Sheets("Sheet1").Cells(pCell.Row, pCell.Column + 2).Value
                MsgBox (Sheets("Sheet1").Cells(pCell.Row, pCell.Column + 2).Value)
                With Sheets("Payment-Deduction Form").Range(NamedRange)
                    If .Validation Then .Validation.Delete
                    .Validation.Add /* Error Here */ Type:=xlValidateInputOnly
                    .InputTitle = "Test"
                    .InputMessage = "Test"
                End With
            End If

        Next pCell
    End If

不幸的是,我在 If .validation 部分收到错误对象不支持此属性或方法。

【问题讨论】:

  • 试试这样:With Sheets("Payment-Deduction Form").Range(Sheets("Payment-Deduction Form").Cells(pCell.Row, pCell.Column + 2)).Validation
  • 在同一行仍然会出现相同的错误,尽管我知道为什么这会有所帮助。将其放在模块中而不是工作表中会更好吗?
  • 你可以试试这样:With Sheets("Payment-Deduction Form").Cells(pCell.Row, pCell.Column + 2)).Validation 吗?
  • 我尝试过几次这种配置。它通过了 With 语句本身,但由于某种原因在 .Add Type 上出现错误(1004)。

标签: vba excel range offset


【解决方案1】:

或许你可以这样尝试:

PcentData = Sheets("Sheet1").Range("PcentData").Value
    If PcentData > 0 Then
        For Each pCell In Sheets("Sheet1").Range("PcentSwitch")
            If pCell.Value = 1 Then
                With Sheets("Payment-Deduction Form").Cells(pCell.Row, pCell.Column + 2).validation
                    .delete
                    .Add Type:=xlValidateInputOnly
                    .InputTitle = "Test"
                    .InputMessage = "Test"    
                End With
            End If    
        Next pCell
    End If

我已删除 .Range 并已从当前单元格中删除验证。

【讨论】:

  • 这会起作用,但错误消息会抛出“对象不支持此属性或方法”。我输入了一个空白的 MsgBox (Sheets("Payment-Deduction Form").Cells(pCell.Row, pCell.Column + 1).Value)。所以它没有从表中获取命名范围......出于某种原因。
  • 您可以在 msgbox 中输入以下内容:Sheets("Payment-Deduction Form").Cells(pCell.Row, pCell.Column + 1).address
  • 提出了正确的 $V$2。在此单元格内(在 Sheet1 中)是命名范围。我的猜测是它试图从空白的表单中提取 V2。
  • 尝试以下方法:Sheets("Payment-Deduction Form").Cells(pCell.Row, pCell.Column + 1).parent.name
  • 是的,父项是付款扣除表。我正在想办法从 pCell 所在的工作表中拉出。
【解决方案2】:

在 Vityata 的帮助下,我设法找到了解决问题的方法。问题是原始循环占用了我想从错误的表格中查找的表格的位置。我将其存储在一个名为“NamedRange”的变量中,并将其插入到 With 语句中,而不是尝试将其直接编码到 Range() 中。

下一个问题是数据验证。似乎它不喜欢将 .Validation 与 With 语句分开,因此,如果我需要对单元格进行多次更改,我将需要多个 With 语句。这是工作代码:

Dim NamedRange As String
PcentData = Sheets("Sheet1").Range("PcentData").Value

    If PcentData > 0 Then
        For Each pCell In Sheets("Sheet1").Range("PcentSwitch")
            If pCell.Value = 1 Then
                NamedRange = Sheets("Sheet1").Cells(pCell.Row, pCell.Column + 2).Value
                With ThisWorkbook.Sheets("Payment-Deduction Form").Range(NamedRange).Validation
                    .Delete
                    .Add Type:=xlValidateInputOnly, AlertStyle:=xlValidAlertStop, Operator _
                    :=xlBetween
                    .IgnoreBlank = True
                    .InCellDropdown = True
                    .InputTitle = "test"
                    .ErrorTitle = ""
                    .InputMessage = "test"
                    .ErrorMessage = ""
                    .ShowInput = True
                    .ShowError = True
                End With
            End If

        Next pCell
    End If

感谢您的帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-02-04
    • 2017-02-03
    • 2015-01-07
    • 1970-01-01
    • 2016-01-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多