简体   繁体   中英

Excel For Each Cell Not Working?

I am trying to get the code to loop through the range. For example, it should be taking the value in cell M53, using it, then running through the code, and doing the same thing with the value in cell M54. The first iteration works, but then it just seems to continuously keep running in cell M53.

I am struggling to work this one out.

Sub TestMacro1()

Dim n As Integer
Dim Strike As Range

Set Strike = Range("M53:M54")
n = 1
Application.ScreenUpdating = False

For Each cell In Strike
    cell.Select
    Selection.Copy
    Range("E19").PasteSpecial xlPasteValues

    If Checker = True Then
        Range("E26").Select
        Selection.Copy
        Range("N53").Offset(n).PasteSpecial xlPasteValues
        n = n + 1
    End If
Next cell
Application.ScreenUpdating = True

End Sub

The Checker Function is defined as:

Function Checker() As Boolean

Dim tmp
Dim c As Object

tmp = False
ActiveSheet.Calculate
ActiveSheet.Calculate

With ActiveSheet.UsedRange
    Set c = .Find("request", LookIn:=xlValues)
    If Not c Is Nothing Then
        Application.OnTime Now + TimeValue("00:00:06"), "TestMacro1"
        tmp = False
    Else
        tmp = True
    End If
End With

Checker = tmp

End Function

Please try this.

Sub TestMacro2()
    Sub TestMacro2()
Dim cell As Range
Dim Strike As Range
Set Strike = Range("M53:M54")

Application.ScreenUpdating = False
For Each cell In Strike
    cell.Copy
    Range("E19").PasteSpecial xlPasteValues 'fixed range

        If Checker = True Then
            ActiveSheet.Calculate
            Range("E26").Copy
            cell.Offset(0, 1).PasteSpecial xlPasteValues
        End If
Next cell
Application.ScreenUpdating = True

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