首页 > 解决方案 > VBA 模块似乎无缘无故停止。

问题描述

我有这个 VBA 代码,它在大 for 循环之后结束,在“msgbox”h”之前。msgbox 是为了检查它是否继续。代码通过循环运行,但仅此而已。有人可以帮我理解为什么吗?

Sub countPT()
'Select file
    Application.ScreenUpdating = False

    Dim i As Integer, lastRow As Integer, tellerPoE(13) As Integer, 
telleruPoE(13) As Integer, SwitchInd As Integer
    Dim wb As Workbook, wb2 As Workbook
    Dim krrom As String, Comment As String

    For i = 1 To 13
        tellerPoE(i) = 0
        telleruPoE(i) = 0
    Next i

    Set wb = ActiveWorkbook

    openFile = Application.GetOpenFilename("Excel-files,*.xls*", 1, _
        "Select a file to open", , False)
    Application.ScreenUpdating = False
    If Len(openFile) = 0 Then
        MsgBox "No file selected.", vbExclamation, "Sorry!"
        End
    End If
    Workbooks.Open openFile

    Set wb2 = ActiveWorkbook


'Read through and count -> put to array on index
    lastRow = wb2.Worksheets("Rådata").Range("F" & Rows.Count).End(xlUp).Row
    For i = 114 To lastRow
        wb2.Activate
        If CStr(wb2.Worksheets("Rådata").Cells(i, "G")) = "528" Then
            krrom = CStr(wb2.Worksheets("Rådata").Cells(i, "F"))
            SwitchInd = SwitchCode(krrom)
            'If SwitchInd = 0 Then
                'GoTo ContinueLoop
            'End If

            Comment = LCase(CStr(wb2.Worksheets("Rådata").Cells(i, "M")))

            If (InStr(Comment, "poe") Or InStr(Comment, "kamera") Or 
InStr(Comment, "cam")) Then
                If Len(wb2.Worksheets("Rådata").Cells(i, "L").Value) > 0 Then
                    tellerPoE(SwitchInd) = tellerPoE(SwitchInd) + 1
                End If
                tellerPoE(SwitchInd) = tellerPoE(SwitchInd) + 1
            Else
                If Len(wb2.Worksheets("Rådata").Cells(i, "L").Value) > 0 Then
                    telleruPoE(SwitchInd) = telleruPoE(SwitchInd) + 1
                End If
                telleruPoE(SwitchInd) = telleruPoE(SwitchInd) + 1
            End If


'ContinueLoop

        End If
    Next i

'Check up to existing
    'Update values
    'Give message on change

    MsgBox "h"
    For j = 1 To 13
        If tellerPoE(j) > CInt(Cells(5 + j, "E")) * 2 Or telleruPoE(j) > 
CInt(Cells(5 + j, "G")) Then
            Cells(6 + j, "K") = "Punkter økt"
        End If
        Cells(5 + j, "E") = tellerPoE(j)
        Cells(5 + j, "G") = telleruPoE(j)
     Next j

'Empty and close
    Application.CutCopyMode = False
    Set wb = ActiveWorkbook
    wb2.Close
    Application.ScreenUpdating = True

End Sub

一些代码被注释掉以尝试解决问题或更容易找到块

标签: vba

解决方案


之后MsgBox,删除循环并编写以下内容:

MsgBox "h"
For j = 1 To 13
    Cells(6 + j, "K") = "Punkter økt"
Next j

检查它是否产生你需要的东西。如果是这样,那么它会起作用并且您的情况是错误的。


推荐阅读