2017-02-16 126 views
1

下午好一切,从单元格值

我有点对我的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部分的这个属性或方法。

+0

请试试这样:'With Sheets(“Payment-Deduction Form”)。Range(Sheets(“Payment-Deduction Form”)。Cells(pCell.Row,pCell.Column + 2))Validation' – Vityata

+0

尽管我可以看出为什么这可能会有所帮助,但同一线路上出现同样的错误。把它放在模块而不是表格中会更好吗? – JaayB

+1

你可以这样试试:'With Sheets(“Payment-Deduction Form”)。Cells(pCell.Row,pCell.Column + 2))Validation'? – Vityata

回答

0

在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 

感谢您的帮助!

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,我必须从目前的细胞去除验证。

+0

这将工作,但错误消息抛出“对象不支持此属性或方法”。我把一个MsgBox(Sheets(“Payment-Deduction Form”)。Cells(pCell.Row,pCell.Column + 1).Value)放到空白处。所以它没有得到表中命名的范围......出于某种原因。 – JaayB

+0

你可以在msgbox中输入以下内容:Sheets(“Payment-Deduction Form”)。Cells(pCell.Row,pCell.Column + 1)。地址' – Vityata

+0

拿出$ V $ 2这是正确的。在这个单元格内(在Sheet1中)是命名的范围。我的猜测是它试图从表单中提取V2,这是空白的。 – JaayB