简体   繁体   English

每个单元格的 Excel 不起作用?

[英]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.例如,它应该获取单元格 M53 中的值,使用它,然后运行代码,并对单元格 M54 中的值执行相同的操作。 The first iteration works, but then it just seems to continuously keep running in cell M53.第一次迭代有效,但随后它似乎一直在单元格 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: Checker 函数定义为:

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结束子

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

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