2017-02-16 89 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