簡體   English   中英

對於每個循環,需要跳過

[英]For Each Loop, need to skip

我遇到了一個問題,就是我的For Each Next循環陷入無限循環(無限到范圍的盡頭)。 我知道我無法向后循環,當然也不能有獨立的“下一個”:

Dim region As Range
Set region = Range("C4:CC4")

For Each Cell In region
    If Len(Cell) > 0 Then
        Cell.Offset(0, 1) = Cell.Value
        **Need "Next Cell" Here**
    End If
Next Cell

編輯 :到目前為止,感謝您的回答,但是我想我需要澄清一下:問題本身不是無限循環。 現在,它正在我的范圍內並逐個單元地查看它是否在單元格中寫入了內容。 如果是這樣,則將其復制到它右邊的那個並循環。 特別是問題在於,如果確實復制了一個值,則它需要跳過該值剛剛被復制到的那個單元格,否則它將開始無限循環。 本質上,我需要一種在If語句中間“跳過”或“下一個單元格”的方法。

編輯2再次感謝-Cool Blue的評論鏈接是解決方案!

嘗試這個。 希望對您有所幫助。

Dim region As Range
Dim firstcell, lastcell As String
firstcell = "C4"
lastcell = "CC4"

Set region = Range(firstcell & ":" & lastcell)

For Each CELL In region
    If Len(CELL) > 0 Then
        CELL.Offset(0, 1) = CELL.Value
        ' **Need "Next Cell" Here**
        If Replace(CELL.Address, "$", "") = lastcell Then Exit For
    End If
Next CELL

這個答案對我來說並不晚,但是無論如何...它可能非常簡單,您甚至不需要任何if語句。 訣竅是只循環具有某些(恆定)內容的(特殊)單元,而不是循環所有單元。 HTH

Dim region As Range
Dim myCell As Range

Set region = Range("C4:CC4")

For Each myCell In region.SpecialCells(xlCellTypeConstants)
    myCell.Offset(0, 1) = myCell.Value
Next myCell

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM