如何调用宏时修复溢出错误[复制]

问题描述 投票:-1回答:1

这个问题已经在这里有一个答案:

即时得到溢出错误,当我打电话给我的宏。当我单独运行他们,它是好的,但是当我打电话给他们一前一后,我得到一个错误。第一个导入的数据,然后第二个宏确实导入的数据的在一个单独的片小分析。

第一个宏

Sub ImportFiles()

    Dim sheet As Worksheet
    Dim total As Integer
    Dim intChoice As Integer
    Dim strPath As String
    Dim i As Integer
    Dim wbNew As Workbook
    Dim wbSource As Workbook
    Set wbNew = ActiveWorkbook

    'allow the user to select multiple files
    Application.FileDialog(msoFileDialogOpen).AllowMultiSelect = True
    'make the file dialog visible to the user
    intChoice = Application.FileDialog(msoFileDialogOpen).Show

    Application.ScreenUpdating = False
    Application.DisplayAlerts = False

    'determine what choice the user made
    If intChoice <> 0 Then
        'get the file path selected by the user
        For i = 1 To Application.FileDialog(msoFileDialogOpen).SelectedItems.Count
            strPath = Application.FileDialog(msoFileDialogOpen).SelectedItems(i)

            Set wbSource = Workbooks.Open(strPath)

            For Each sheet In wbSource.Worksheets
                total = wbNew.Worksheets.Count
                wbSource.Worksheets(sheet.Name).Copy _
                after:=wbNew.Worksheets(total)
            Next sheet

            wbSource.Close
        Next i
    End If
End Sub

第二宏(在数据行错误=数据行+ 1)

Sub Analysis()

Dim dataSheet As Worksheet
Dim thisSheet As Worksheet
Dim thisWorkbook As Workbook

Set thisWorkbook = ActiveWorkbook
Set thisSheet = ActiveSheet

For i = 1 To thisWorkbook.Sheets.Count

    If Not thisWorkbook.Sheets(i).Name = thisSheet.Name Then

        Set dataSheet = thisWorkbook.Sheets(i)

    End If

Next i



If thisWorkbook.Sheets.Count >= 2 Then

    'dataSheet now contains the sheet we need to pull data from.
    Dim summaryRow(1 To 7) As Integer
    Dim dataRow As Integer

    dataRow = 1

    summaryRow(1) = 10
    summaryRow(2) = 13
    summaryRow(3) = 16
    summaryRow(4) = 19
    summaryRow(5) = 22
    summaryRow(6) = 28
    summaryRow(7) = 31

    For i = 1 To UBound(summaryRow)

        Do While Not dataSheet.Range("U" & dataRow) = "Nominal"
            dataRow = dataRow + 1 'ERROR HERE
        Loop

        dataRow = dataRow + 1

        thisSheet.Range("I" & summaryRow(i)) = dataSheet.Range("U" & dataRow)
        If Not dataSheet.Range("AH" & (dataRow + 1)) = "" Then
            thisSheet.Range("J" & summaryRow(i)) = Application.WorksheetFunction.CountIf(dataSheet.Range("AH" & (dataRow + 1) & ":" & "AH" & (dataRow + 7)), "=" & "PASS")
            thisSheet.Range("K" & summaryRow(i)) = Application.WorksheetFunction.CountIf(dataSheet.Range("AH" & (dataRow + 1) & ":" & "AH" & (dataRow + 7)), "=" & "EVALUATE")
            thisSheet.Range("L" & summaryRow(i)) = Application.WorksheetFunction.CountIf(dataSheet.Range("AH" & (dataRow + 1) & ":" & "AH" & (dataRow + 7)), "=" & "FAIL")
        Else
            thisSheet.Range("J" & summaryRow(i)) = "N/A"
            thisSheet.Range("K" & summaryRow(i)) = "N/A"
            thisSheet.Range("L" & summaryRow(i)) = "N/A"
        End If

    Next i


End If

End Sub
excel vba
1个回答
0
投票

有可能你可以在第一次执行打破它,如果你运行第二个宏来测试一个Excel工作表,其中的“目标”值永远不会设置,在这种情况下,以下将生成一个无限循环,将尽快打破为最大值到达整数数据行。这样做虽然不dataSheet.Range(“U”和数据行)=“目标”的DataRow =数据行+ 1“ERROR HERE循环您可能能够避免错误使用的,而不是做的,而这是基于预先计算的数一行或一组最大的约束。我没有办公室我的上网本,但我敢肯定以下逻辑是不会崩溃,除了一些愚蠢的错误,语法我可以做。

Sub Analysis()

Dim dataSheet As Worksheet
Dim thisSheet As Worksheet
Dim thisWorkbook As Workbook

Set thisWorkbook = ActiveWorkbook
Set thisSheet = ActiveSheet

For i = 1 To thisWorkbook.Sheets.Count

    If Not thisWorkbook.Sheets(i).Name = thisSheet.Name Then

        Set dataSheet = thisWorkbook.Sheets(i)

    End If

Next i



If thisWorkbook.Sheets.Count >= 2 Then

    'dataSheet now contains the sheet we need to pull data from.
    Dim summaryRow(1 To 7) As Integer
    Dim dataRow As Integer

    Dim bfound as boolean
    ''dataRow = 1

    summaryRow(1) = 10
    summaryRow(2) = 13
    summaryRow(3) = 16
    summaryRow(4) = 19
    summaryRow(5) = 22
    summaryRow(6) = 28
    summaryRow(7) = 31

    For i = 1 To UBound(summaryRow)

       bfound=false
       For dataRow = 1 to 2147000000
            If dataSheet.Range("U" & dataRow) = "Nominal" Then
                    bfound=true
                    exit for
            End If
    Next

        If bfound Then
            dataRow = dataRow + 1
            thisSheet.Range("I" & summaryRow(i)) = dataSheet.Range("U" & dataRow)
            If Not dataSheet.Range("AH" & (dataRow + 1)) = "" Then
                thisSheet.Range("J" & summaryRow(i)) = Application.WorksheetFunction.CountIf(dataSheet.Range("AH" & (dataRow + 1) & ":" & "AH" & (dataRow + 7)), "=" & "PASS")
                thisSheet.Range("K" & summaryRow(i)) = Application.WorksheetFunction.CountIf(dataSheet.Range("AH" & (dataRow + 1) & ":" & "AH" & (dataRow + 7)), "=" & "EVALUATE")
                thisSheet.Range("L" & summaryRow(i)) = Application.WorksheetFunction.CountIf(dataSheet.Range("AH" & (dataRow + 1) & ":" & "AH" & (dataRow + 7)), "=" & "FAIL")
            Else
                thisSheet.Range("J" & summaryRow(i)) = "N/A"
                thisSheet.Range("K" & summaryRow(i)) = "N/A"
                thisSheet.Range("L" & summaryRow(i)) = "N/A"
            End If
        Else
           '' I do not have idea what you want to do in this case
        End if
    Next i


End If

End Sub
© www.soinside.com 2019 - 2024. All rights reserved.