正如标题所示,我正在运行下面的宏。奇怪的是,msgBox从不提示我结束时间,也不会在宏停止运行时弹出错误消息。文件夹中的数据是相当直接的东西,但是它经历了大约一千个文件
关于如何自行解决问题的任何指示?这是我需要变得更好的东西,因此有关如何做到这一点的建议会很好。
Sub AbesLoop()
Dim wbk As Workbook
Dim ws As Integer
Dim Filename As String
Dim path As String
Dim rCell As Range
Dim rRng As Range
Dim wsO As Worksheet
Dim StartTime As Double
Dim SecondsElapsed As Double
Dim sheet As Worksheet
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Application.Calculation = xlCalculationManual
StartTime = Timer
'On Error Resume Next
path = "PATHtoStuff" & "\"
Filename = Dir(path & "*.xl??")
Set wsO = ThisWorkbook.Sheets("Sheet1")
Do While Len(Filename) > 0
DoEvents
Set wbk = Workbooks.Open(path & Filename, True, True)
ws = wbk.Worksheets.count
'For i = 1 To ws
For Each sheet In ActiveWorkbook.Worksheets
If sheet.Index > 1 Then
Set rRng = Range("b1:b308")
For Each rCell In rRng.Cells
If rCell <> "" And rCell.Value <> "Not Tested" And rCell.Value <> 0 Then
wsO.Cells(wsO.Rows.count, 1).End(xlUp).Offset(1, 0).Value = rCell
wsO.Cells(wsO.Rows.count, 1).End(xlUp).Offset(0, 1).Value = rCell.Offset(0, -1)
wsO.Cells(wsO.Rows.count, 1).End(xlUp).Offset(0, 2).Value = Mid(Right(ActiveWorkbook.FullName, 15), 1, 10)
End If
Next rCell
End If
Next
'Next i
wbk.Close False
Filename = Dir
Loop
Application.ScreenUpdating = True
Application.DisplayAlerts = True
Application.Calculation = xlCalculationAutomatic
SecondsElapsed = Round(Timer - StartTime, 2)
MsgBox "This code ran successfully in " & SecondsElapsed & " seconds", vbInformation
End Sub
编辑:事实证明,对于某些原因,循环代码正在查看工作表选项卡1.我更改了代码以尝试并解释该问题,但由于某种原因,仍在查看第一页。错误是第一张表格上的仪表板(我一直试图规避)之前的“不匹配类型”一直遇到公式错误。
答案 0 :(得分:1)
在VBA中处理应用程序的一部分时,使用应用程序提供的对象非常重要,而不是通用的Selection或ActiveWorkbook等。此代码是做到这一点,这很好: - )
然而,与此同时,VBA需要能够唯一地识别它应该工作的对象。用&#34;选择&#34;或者&#34;活跃[Thing]&#34;这没有必要,所以这一步并不是使用宏录制器轻松学习的。
对于此问题中的代码
Set rRng = Range("b1:b308")
Excel不清楚应该从哪个工作表中获取Range,因此它默认为工作簿中的第一个工作表。相反,使用:
Set rRng = sheet.Range("b1:b10")"