下午好一切,从单元格值
我有点对我的VBA生锈参考命名范围For循环,因为我已经移动到其他语言编程,所以我希望有人能够帮助我。
我所搭售做
我有两个表。一个是用户填写的表单页面,另一个(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' – Vityata
尽管我可以看出为什么这可能会有所帮助,但同一线路上出现同样的错误。把它放在模块而不是表格中会更好吗? – JaayB
你可以这样试试:'With Sheets(“Payment-Deduction Form”)。Cells(pCell.Row,pCell.Column + 2))Validation'? – Vityata