调用宏时出现溢出错误。当我分别运行它们时,这很好,但是当我一个接一个地调用它们时,会出现错误。第一个导入数据,然后第二个宏在单独的工作表上对导入的数据进行少量分析。
第一个宏
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
第二个宏(dataRow中的错误= dataRow +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
答案 0 :(得分:0)
如果您运行第二个宏来测试一个从未设置“标称”值的excel工作表,则可以在第一次执行时将其破坏,在这种情况下,以下内容将生成一个无限循环,该循环将在达到整数dataRow的最大值。 不做时dataSheet.Range(“ U”&dataRow)=“标称” dataRow = dataRow +1'这里出错 环 您可以通过使用for而不是do while来避免该错误,它基于预先计算的行数或最大集作为约束。 我的上网本上没有Office,但我可以肯定以下逻辑不会崩溃,除了我可能会犯的一些愚蠢的语法错误之外。
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