变量为空时如何结束循环

问题描述

Sub max()

Sheets(1).Select
Sheets(1).Name = "Sheet1"

Dim rng As Range
Dim celladdress As String
Dim celling As Variant

Do Until IsEmpty(celling)

    If celling > "G4" Then
    
    Set rng = Range("G3:G1000").Find(what:="Description")
    rng.Find what:="Description"
    
    celladdress = rng.Address(-1)
    celling = celladdress
    Else: Call Source

    End If
Loop

MsgBox "done"

End Sub

您好,我试图在我的范围内找到description一词,如果description很奇怪,则应该运行宏然后循环。但是如果变量为空并且找不到变量说明,我希望循环结束并显示msgbox。我尝试使用循环结束循环,直到单元格为空,但似乎无法正常工作。变量单元格引用为空,因此不确定为什么它不起作用。非常感谢任何帮助

解决方法

最大,这值得作为新答案发布,以突出显示FindNext的不直观行为。这有效-比上面的答案更适合接受答案。可能有些古怪,因为在更优雅的解决方案中是可能的:

let httpParams = new HttpParams()
  .append("grant_type","refresh_token")
  .append("refresh_token",token)
  .append("client_id",my_client_id);

  this.http.post("https://accounts.spotify.com/api/token",httpParams.toString(),{ headers: { 
    'Content-Type':'application/x-www-form-urlencoded'
  },}).subscribe(data => {  
    console.log(data['access_token'])
  })
,

'Max,您的意图有点猜测-可能需要您的帮助。这会让您更近吗?我认为我不能在GNU / Linux机器上做得更好。

Sub max()

Sheets(1).Select
Sheets(1).Name = "Sheet1"

Dim rng As Range
Set rng = Range("G3:G1000")

Dim celladdress As String
Dim celling As Range
 
Set celling = rng.Find(what:="Description")
If celling Is Nothing Then
    MsgBox "Not found,exiting"
    Exit Sub
End If

Do 
    'Set celling = range.FindNext    'Keeps returning first range found! Maybe "With" block on rng will work.
    If celling.Row > 4 Then
        'celling.Activate
        celladdress = celling.Offset(-1,0).Address
        MsgBox celladdress
    'Else: Call Source   'What is Source? Not this sub,is it?
    
    End If
    Set celling = range.FindNext 
Loop Until celling Is Nothing

MsgBox "done"

End Sub

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...