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

时间:2016-10-05 13:40:44

标签: excel-vba vba excel

我正在尝试让代码循环遍历范围。例如,应该使用单元格M53中的值,使用它,然后运行代码,并使用单元格M54中的值执行相同的操作。第一次迭代工作,但它似乎继续在单元格M53中继续运行。

我正在努力解决这个问题。

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

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

1 个答案:

答案 0 :(得分:0)

请试一试。

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