从 For 循环中的单元格值引用命名范围
Refer to Named Range from Cell Value in For Loop
大家下午好,
我的 VBA 有点生疏,因为我已经转向其他语言的编程,所以我希望有人能够帮助我。
我想做什么
我有两个 sheet。一个是用户填写的表单页面,另一个(Sheet1 ....我没有命名)基本上是一个数据页面。
在 Sheet1 中,我有一个 table,它显示 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
编辑:
我已经设法确保代码从正确的 sheet 中提取命名范围,方法是定义一个名为 NamedRange 的字符串并使其等于旧的 with 语句,指向正确的 sheet。
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 部分收到错误 Object doesn't support this 属性 or method。
也许你可以这样尝试:
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
并且我已经从当前单元格中删除了验证。
在 Vityata 的帮助下,我设法找到了解决问题的方法。问题是原始循环占据了我想从错误的 sheet 查找的 table 的位置。我将它存储在一个名为 "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
感谢您的帮助!
大家下午好,
我的 VBA 有点生疏,因为我已经转向其他语言的编程,所以我希望有人能够帮助我。
我想做什么
我有两个 sheet。一个是用户填写的表单页面,另一个(Sheet1 ....我没有命名)基本上是一个数据页面。
在 Sheet1 中,我有一个 table,它显示 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
编辑:
我已经设法确保代码从正确的 sheet 中提取命名范围,方法是定义一个名为 NamedRange 的字符串并使其等于旧的 with 语句,指向正确的 sheet。
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 部分收到错误 Object doesn't support this 属性 or method。
也许你可以这样尝试:
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
并且我已经从当前单元格中删除了验证。
在 Vityata 的帮助下,我设法找到了解决问题的方法。问题是原始循环占据了我想从错误的 sheet 查找的 table 的位置。我将它存储在一个名为 "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
感谢您的帮助!