简体   繁体   中英

Excel VBA looping through empty cells and retrieving row values

Me, again!

What I'm trying to do is to search in my range for any empty cells, and for each empty cell it finds, return some values from its row (offset).

The code I've pulled off so far:

 Private Sub buscarbtn_Click()
Dim fecha As Date: fecha = fechabsc.Value
Dim DESCUBIERTO As Boolean: DESCUBIERTO = False
Dim LR As Long
Dim r As Long
Dim i As Long
Dim diasem
Dim cell As Range


Hoja9.Range("L8").Value = fecha
fec = Hoja9.Range("L9").Value

If fec = "1" Then
    diasem = "D"
End If
If fec = "2" Then
    diasem = "L"
End If
If fec = "3" Then
    diasem = "M"
End If
If fec = "4" Then
    diasem = "W"
End If
If fec = "5" Then
    diasem = "J"
End If
If fec = "6" Then
    diasem = "V"
End If
If fec = "7" Then
    diasem = "S"
End If

Hoja9.Range("L10").Value = diasem
Set fecdes = prontuario1.Range("S:S").Find(what:=diasem, LookIn:=xlValues, LookAt:=xlWhole)

For Each cell In prontuario1.Range("S:S")

    If fecdes.Offset(0, 5).Value = "" Then
      DESCUBIERTO = True
      Debug.Print fecdes.Offset(0, 2).Value & " " & fecdes.Offset(0, 3).Value & " - " & fecdes.Offset(0, 4).Value
    End If
Next cell

End Sub

That Debug.Print I'm using was only a temporary solution for testing, since trying to print the whole row was only returning one result... and now I get multiple results of the same row.

I can only guess I'm overlooking something or using the For each loop totally wrong.

I know my code is reaaaally messy, but I'd be forever grateful if you could point me in the right direction.

Thanks!

It looks as though your loop is referring to fecdes each time instead of cell, so for each cell in column S, it's re-evaluating fecdes.offset(0,5) and then printing values next to the fecdes cell. But presumably you at least want to have it evaluate the cell you're looping through in column S, right?

Private Sub buscarbtn_Click()
Dim fecha As Date: fecha = fechabsc.Value
Dim DESCUBIERTO As Boolean: DESCUBIERTO = False
Dim LR As Long
Dim r As Long
Dim i As Long
Dim diasem as string
Dim cell As Range


Hoja9.Range("L8").Value = fecha
fec = Hoja9.Range("L9").Value


select case fec
case 1
    diasem = "D"
case 2
    diasem = "L"
case 3
    diasem = "M"
case 4
    diasem = "W"
case 5
    diasem = "J"
case 6
    diasem = "V"
case 7
    diasem = "S"
end select

Hoja9.Range("L10").Value = diasem
r = prontuario1.cells(1,19).end(xldown).row

For i=2 to r
If prontuario1.cells(i,19).Offset(0, 5).Value = "" Then
  DESCUBIERTO = True
  Debug.Print prontuario1.cells(i,19).Offset(0, 2).Value & " " & prontuario1.cells(i,19).Offset(0, 3).Value & " - " & prontuario1.cells(i,19).Offset(0, 4).Value
End If
Next i

End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM