简体   繁体   English

Excel VBA在空单元格中循环并检索行值

[英]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. 我正在使用的Debug.Print只是测试的一种临时解决方案,因为尝试打印整个行只会返回一个结果...现在我得到同一行的多个结果。

I can only guess I'm overlooking something or using the For each loop totally wrong. 我只能猜测我正在忽略某些内容或使用For Each循环完全错误。

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. 看起来您的循环似乎是每次都引用fecdes而不是单元格,因此对于S列中的每个单元格,它都将重新计算fecdes.offset(0,5),然后在fecdes单元格旁边打印值。 But presumably you at least want to have it evaluate the cell you're looping through in column S, right? 但是大概您至少想让它评估您在S列中循环通过的单元格,对吗?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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