2014-06-27 174 views
1

我找不到任何要求VBA的小區選擇完全是我找的...基於價值

我使用兩個條件來設置我的選擇(查找值「減速」,然後在細胞直接在它下面,找到「」(一個空單元格)。

我很難找到一種方法來選擇符合這些條件的單元格,然後列出單元格地址(我想在消息框中顯示單元格地址,提醒他們'錯誤'的位置)

最終會有更多的細胞需要尋找,這就是爲什麼我要通過多個細胞進行搜索的原因。

所以,總之,我希望我的代碼,找了兩個標準,選擇單元格的標準匹配,並顯示一個彈出消息,指出錯誤是在哪個小區。

Private Sub Worksheet_Change(ByVal Target As Range) 
If ActiveSheet.Range("J11").Value < 0 Then 
    MsgBox "You have exceeded the maximum allowable pipe length for this section. Please review your selection before continuing. ", vbOKOnly, "Inoperable Pipe Length" 
End If 

Do While ActiveSheet.Range("J17,J7").Value = "Reducer" 
    If ActiveSheet.Range("J18,J8").Value = "" Then 
     G = Sheets("Pipe Calcs").Range("J18,J8").Address 
     MsgBox "Please Select a reducer size in cell " & G & ActiveCell.Address(False, False), vbCritical, "No Reducer Size Selected" 
     Exit Sub 
    Else 
     End 
    End If 
Loop 
End Sub 

回答

1
Private Sub Worksheet_Change(ByVal Target As Range) 
If ActiveSheet.Range("A1").Value < 0 Then 
    MsgBox "You have exceeded the maximum allowable pipe length for this section. Please review your selection before continuing. ", vbOKOnly, "Inoperable Pipe Length" 
End If 

For Each cell In Range("J1:J1000") 
    If cell.Value = "Reducer" Then 
     If Range(cell.Address).Offset(1, 0) = "" Then 
     G = Sheets("Pipe Calcs").Range(cell.Address).Offset(1, 0).Address 
     MsgBox "Please Select a reducer size in cell " & G 
     Range(Cell.Address).Offset(1, 0).Select 
     Exit Sub 
     End If 
    End If 
Next 


End Sub 

上面的代碼會檢查第J列的「Reducer」,如果找到,它會看到下面的單元格是否包含一個值,如果沒有,它會提示用戶輸入單元格並退出子單元。當用戶更新單元格時,它們會觸發Worksheet_Change語句並使宏再次運行。

+0

這樣做的伎倆,非常感謝。我想我正試圖與我檢查過的細胞過於精確。出於某種原因,我永遠無法獲得單元格地址以便在我的代碼中出現。 無論如何,非常感謝! – Keegan

+0

@Keegan你永遠無法讓'.address'與你的範圍一起工作的原因是因爲你的範圍在多個單元格中是固定的。 '.address'需要一個單元來查看返回地址。使用上面的代碼,因爲您一次循環遍歷1個單元格,所以'cell.address'總是隻引用一個單元格,而不是'範圍(J18,J8)',它同時查看2個單元格。 – SilverShotBee

+0

你非常感謝你的幫助。這使現在更有意義。 – Keegan