2014-03-25 242 views
3

我是VBA的新手。Excel VBA根據單元格值選擇範圍

For each cell in columns("c:c") 
If cell.value = "TRUE" Then 
'vba is required for selecting corresponding cells in columns A and B 
Next cell 
Else: 
exit sub 
End if 
end sub 

請適當修正

+0

什麼ahve你試過這麼遠嗎? –

+0

hi simico,請看看上面編輯過的代碼。只是我寫了大概bcoz我不知道多少編碼。 – ganesh

+0

你是否只選擇第一行,其中C列是FASLE,還是C列中的所有行都是false? –

回答

3

試試這個:

Sub test() 
    Dim lastrow As Long 
    Dim c As Range, rng As Range 
    'change Sheet1 to suit 
    With ThisWorkbook.Worksheets("Sheet1") 
     lastrow = .Cells(.Rows.Count, "C").End(xlUp).Row 
     For Each c In .Range("C1:C" & lastrow) 
      If UCase(c.Text) = "FALSE" Then 
       If rng Is Nothing Then 
        Set rng = .Range("A" & c.Row).Resize(, 2) 
       Else 
        Set rng = Union(rng, .Range("A" & c.Row).Resize(, 2)) 
       End If 
      End If 
     Next c 
    End With 

    If Not rng Is Nothing Then rng.Select 
End Sub 
+0

是其完美命中。那就是我需要的。謝謝simico。如果您不介意我可以收到您的電子郵件ID,或者我可以在哪裏直接接受本網站以外的幫助? – ganesh

相關問題