Excel VBA Screenupdating False Infinite Loop

时间:2017-12-02 02:35:10

标签: excel vba excel-vba

我有一个函数可以将数据从一张纸(它已被复制的地方)映射到另一张纸,然后用于进一步分析。当我运行带有屏幕更新的代码时,它总能正常工作。当我关闭屏幕更新时,代码会陷入sub的最后一部分的无限循环中(以粗体突出显示 - 它是代码最后一部分的最内部循环)。如果您然后调试代码并重新启动它正常继续并完成代码。如果离开它将永远不会结束,但下次将正常工作:

Sub simsMap()

Dim simsCol As String
Dim mapCol As String

range("A5:OP253").ClearContents
range("S1:OP1").ClearContents
range("S4:OP4").ClearContents

simsCol = range("A1")
For x = 2 To 250
    If Worksheets("simsData").range(simsCol & x) <> "" Then range("A" & x + 3).Value = Worksheets("simsData").range(simsCol & x)
Next x

simsCol = range("B1")
For x = 2 To 250
    If range("A" & x + 3) <> "" Then
        If Worksheets("simsData").range(simsCol & x) = range("B2") Or Worksheets("simsData").range(simsCol & x) = range("B3") Then
            range(simsCol & x + 3) = "Y"
        Else
            range(simsCol & x + 3) = "N"
        End If
    End If
Next x

Dim simsArray As Variant
Dim mapArray As Variant
simsArray = Array("C1", "D1", "G1")
mapArray = Array("C", "D", "G")

For y = 0 To UBound(simsArray)
    simsCol = range(simsArray(y))
    mapCol = mapArray(y)
    For x = 2 To 250
        If range("A" & x + 3) <> "" Then
            If Worksheets("simsData").range(simsCol & x) = "Y" Then
                range(mapCol & x + 3) = "Y"
            Else
                range(mapCol & x + 3) = "N"
            End If
        End If
    Next x
Next y

simsArray = Array("E1", "F1", "H1", "I1", "J1", "K1", "L1", "M1", "N1", "O1", "P1", "Q1")
mapArray = Array("E", "F", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q")

For y = 0 To UBound(simsArray)
    simsCol = range(simsArray(y))
    mapCol = mapArray(y)
    For x = 2 To 250
        If range("A" & x + 3) <> "" Then
            range(mapCol & x + 3) = Worksheets("simsData").range(simsCol & x)
        End If
    Next x
Next y

Dim realColumn As String
Dim valueColumn As String
Dim columnNumber As Long
Dim realCell As String
Dim valueCell As String
Dim subjectJump As Integer

realColumn = "S"
subjectJump = 8 - Worksheets("menu").range("F17")
For y = 1 To 48
    If Worksheets("menu").range("F19") = "Y" Then
        valueColumn = range(realColumn & 1)
        realCell = realColumn & 1
        realColumn = Split(Cells(1, range(realCell).Column + 1).Address, "$")(1)
        range(realColumn & 1) = valueColumn
        For x = 1 To Worksheets("menu").range("F17")
            valueCell = range(realColumn & 1) & 1
            If range(realColumn & 1) <> "" Then valueColumn = Split(Cells(1, range(valueCell).Column + 1).Address, "$")(1)
            realCell = realColumn & 1
            realColumn = Split(Cells(1, range(realCell).Column + 1).Address, "$")(1)
            range(realColumn & 1) = valueColumn
        Next x
    Else
        If range(realColumn & 1) = "" Then
            realCell = realColumn & 1
            realColumn = Split(Cells(1, range(realCell).Column + 8).Address, "$")(1)
        Else
            For x = 1 To Worksheets("menu").range("F17")
                valueCell = range(realColumn & 1) & 1
                valueColumn = Split(Cells(1, range(valueCell).Column + 1).Address, "$")(1)
                realCell = realColumn & 1
                realColumn = Split(Cells(1, range(realCell).Column + 1).Address, "$")(1)
                range(realColumn & 1) = valueColumn
            Next x
            realCell = realColumn & 1
            realColumn = Split(Cells(1, range(realCell).Column + subjectJump).Address, "$")(1)
        End If
    End If
Next y


realColumn = "S"
For y = 1 To 384
    simsCol = range(realColumn & 1)
    mapCol = realColumn
    If range(mapCol & 1) <> "" Then
        If range("A" & 4) <> "" Then
            range(mapCol & 4) = Worksheets("simsData").range(simsCol & 1)
        End If
    End If
    realColumn = Split(Cells(1, range(realColumn & 1).Column + 1).Address, "$")(1)
Next y

realColumn = "S"
For y = 1 To 384
    simsCol = range(realColumn & 1)
    mapCol = realColumn
    If range(mapCol & 1) <> "" Then
        For x = 2 To 250
            **If range("A" & x + 3) <> "" Then
                range(mapCol & x + 3) = Left(Worksheets("simsData").range(simsCol & x), 1)
            End If**
        Next x
    End If
    realColumn = Split(Cells(1, range(realColumn & 1).Column + 1).Address, "$")(1)
    If y = 384 Then loopCheck = False
Next y

For x = 5 To 253
    If range("A" & x) <> "" Then studentNumber = x
Next x

End Sub

0 个答案:

没有答案