下午好一切,從單元格值
我有點對我的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