【发布时间】: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)。