Una famiglia di software per fogli di calcolo Microsoft con strumenti per l'analisi, la creazione di grafici e la comunicazione dei dati.
Se(se) ho capito....in vari modi. Mettiamo tu abbia dei valori nel Foglio1, Range A1:C5. Puoi scorrere una singola colonna o più colonne. Puoi farlo tramite un ciclo for/next, un ciclo for each/next, tramite Do, ecc. Vediamone alcuni. Ciclo For/Next per una colonna(A1:A5) dove se trovo un valore no(ma può essere qualsiasi valore) all'interno di una cella, esco dal ciclo segnalando in una MsgBox la cella:
Public Sub m()
Dim sh As Worksheet
Dim lng As Long
Set sh = ThisWorkbook.Worksheets("Foglio1")
With sh
For lng = 1 To 5
If .Cells(1, lng).Value = "no" Then
MsgBox .Cells(1, lng).Address
Exit For
End If
Next
End With
Set sh = Nothing
End Sub
Posso fare la stessa cosa con un ciclo For Each/Next ciclando le celle come oggetti Range:
Public Sub m()
Dim sh As Worksheet
Dim rng As Range
Dim c As Range
Set sh = ThisWorkbook.Worksheets("Foglio1")
With sh
Set rng = .Range("A1:A5")
For Each c In rng
If c.Value = "no" Then
MsgBox c.Address
Exit For
End If
Next
End With
Set c = Nothing
Set rng = Nothing
Set sh = Nothing
End Sub
Questa seconda routine può sembrare più complessa. In determinati contesti risulta invece più semplice. Vediamo di fare la ricerca su più colonne del nostro Range A1:C5. Con il ciclo For/Next:
Public Sub m()
Dim sh As Worksheet
Dim lRiga As Long
Dim lCol As Long
Set sh = ThisWorkbook.Worksheets("Foglio1")
With sh
For lCol = 1 To 3
For lRiga = 1 To 5
If .Cells(lRiga, lCol).Value = "no" Then
MsgBox .Cells(lRiga, lCol).Address
Exit For
End If
Next
Next
End With
Set sh = Nothing
End Sub
Con il ciclo For Each/Next:
Public Sub m()
Dim sh As Worksheet
Dim rng As Range
Dim c As Range
Set sh = ThisWorkbook.Worksheets("Foglio1")
With sh
Set rng = .Range("A1:C5")
For Each c In rng
If c.Value = "no" Then
MsgBox c.Address
Exit For
End If
Next
End With
Set c = Nothing
Set rng = Nothing
Set sh = Nothing
End Sub
E, come vedi, questa volta la seconda routine risulta più *semplice*. Se non è questa la risposta che cerchi, per favore resta sempre in questo thread e prova di essere più chiaro. Grazie.
Mauro Gamberini - Microsoft© MVP(Excel)