Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vba 代码仅在单步执行代码竞争条件时运行?_Vba_Excel - Fatal编程技术网

Vba 代码仅在单步执行代码竞争条件时运行?

Vba 代码仅在单步执行代码竞争条件时运行?,vba,excel,Vba,Excel,当我使用F8单步执行代码时,我有一个宏似乎可以工作,但当我尝试运行整个sub或从用户将在工作表中按下的按钮调用它时,宏就不能工作 当我完整地运行代码时,我可以看出它执行了一些步骤,但不是所有步骤 我已经阅读了一些关于这种情况的现有帖子,似乎每次此人都在使用大量。选择。激活,等等。我没有使用这些命令,我试图更动态地设置我的工作表和变量。我还包括了Application.screenUpdate=False 因为我没有使用这些类型的命令,所以我假设这是某种竞争条件,需要更多的时间来暂停。我尝试添加两

当我使用F8单步执行代码时,我有一个宏似乎可以工作,但当我尝试运行整个sub或从用户将在工作表中按下的按钮调用它时,宏就不能工作

当我完整地运行代码时,我可以看出它执行了一些步骤,但不是所有步骤

我已经阅读了一些关于这种情况的现有帖子,似乎每次此人都在使用大量
。选择
。激活
,等等。我没有使用这些命令,我试图更动态地设置我的工作表和变量。我还包括了
Application.screenUpdate=False

因为我没有使用这些类型的命令,所以我假设这是某种竞争条件,需要更多的时间来暂停。我尝试添加两行
应用程序。Wait(Now+TimeValue(“00:00:01”))
行,但当我将这些行添加到VBA代码时,当我尝试运行整个代码时,它将完全冻结Excel。不知道为什么会这样,但我必须在任务管理器中删除Excel

这是VBA,很抱歉我添加了这么多评论:

Sub CombineExcels()
'***** This sub is to autofilter for each available filter option and put the matching Excel file paths into one cell on the FINAl sheet *****

UserForm1.Show vbModeless

'***** Setting variables *****
Dim RngOne As Range, cell As Range
Dim LastCell As Long
Dim LastCellC As Long
Dim Row As Long
Dim i As Integer
Dim count As Integer
Dim s As String
Dim EnterVal As Range
Dim FirstUsedRow As Long
Dim FirstEmptyCell As Long

'***** In the event of an error, we will skip to our Error Handler *****
On Error GoTo EH

'***** Turn off Excel Screen Updating so the screen doesn't keep flashing and slow the macro *****
Application.ScreenUpdating = False

'***** Finding the last used row, first empty row, and largest range that we will work with *****
With Sheets("Final")
    LastCell = .Range("A" & Sheets("Final").Rows.count).End(xlUp).Row
    LastCellC = .Range("C" & Sheets("Final").Rows.count).End(xlUp).Row + 1
    Set RngOne = .Range("A2:A" & LastCell)
End With

'***** This section is a loop that will apply the filter for each option and combine the results onto the Final sheet *****
For Each cell In RngOne
    With Sheets("Folder Output")
        '***** If a filter is already applied, we will remove the filter *****
        If .FilterMode Then .ShowAllData
        '***** Clearing any remaining data from the location we will temporarily store file paths in *****
        Worksheets("Final").Range("Q1:Q100").Clear
        '***** Apply the filter. The criteria is named CELL which is a loop for each filter option *****
        .Columns("A").AutoFilter Field:=1, Criteria1:=cell
        '***** Find the last row of filter results in Column C *****
        Row = .Range("C" & Sheets("Folder Output").Rows.count).End(xlUp).Row
        '***** If the row number returned is 2 then we know that there is only 1 file path result *****
        If Row = "2" Then Row = .Range("C" & Sheets("Folder Output").Rows.count).End(xlUp).Row + 1
        '***** Setting a new range for only the filtered results in Column C *****
        Dim rng As Range: Set rng = .Range("C2:C" & Row).SpecialCells(xlCellTypeVisible)
            Dim rngCell As Range
            '***** Loop to get each result and place it on the FINAL sheet in column Q for now *****
            For Each rngCell In rng
                    If Sheets("Final").Range("Q1").Value = "" Then
                        FirstEmptyCell = .Range("Q" & Sheets("Final").Rows.count).End(xlUp).Row
                        Worksheets("Final").Range("Q" & FirstEmptyCell) = rngCell.Value
                    Else
                        FirstEmptyCell = .Range("Q" & Sheets("Final").Rows.count).End(xlUp).Row + 1
                        Worksheets("Final").Range("Q" & FirstEmptyCell) = rngCell.Value
                    End If
            '***** Continue to the next filtered result until all file paths for that filter are complete *****
            Next rngCell

        '***** Finding the last used row from the pasted file path results in Column Q *****
        count = Sheets("Final").Cells(Rows.count, "Q").End(xlUp).Row
        '***** Loop to combine all the paths into one string but separate the paths with a ; *****
        For i = 1 To count
            If Cells(i, 17).Value <> "" Then s = s & Cells(i, 17).Value & ";"
        Next
            '***** Find the last used row from Column C in the Final sheet. Then paste the combined file paths to Column C *****
            Set EnterVal = Worksheets("Final").Range("C" & LastCellC)
            EnterVal.Value = s
            Set EnterVal = Nothing
            s = ""
        '***** This tells the macro to move a row down next time through the loop *****
        LastCellC = LastCellC + 1
    End With
Next

'***** Once the loop is finished, we will end this sub in the CleanUp section *****
GoTo CleanUp

'***** Before exiting the sub we will turn Screen Updating back on and notify the user the Excel file paths are combined *****
CleanUp:
    On Error Resume Next
    Application.ScreenUpdating = True
    UserForm1.Hide
    MsgBox ("Excel File Paths Have Been Concatenated!")
Exit Sub
'***** If an error occurs during the loop, we go here to redirect to turn updating on and end the sub *****
EH:
    ' Do error handling
    GoTo CleanUp

End Sub
Sub-CombineExcels()
'****此子项用于自动筛选每个可用的筛选选项,并将匹配的Excel文件路径放入最终工作表的一个单元格中*****
UserForm1.Show vbModeless
“******设置变量*****
作为范围,单元格作为范围
暗淡的最后一个细胞一样长
暗淡的最后一个细胞和长的一样
暗排一样长
作为整数的Dim i
将计数设置为整数
像线一样变暗
暗淡的范围
暗淡的第一个乌塞德罗一样长
暗的第一个空的细胞一样长
'******如果发生错误,我们将跳到错误处理程序*****
关于错误转到EH
'****关闭Excel屏幕更新,使屏幕不会一直闪烁并减慢宏的速度*****
Application.ScreenUpdating=False
“****查找最后使用的行、第一个空行以及我们将使用的最大范围*****
附页(“最终版”)
LastCell=.Range(“A”)和Sheets(“Final”).Rows.count.End(xlUp.Row)
LastCellC=.Range(“C”和Sheets(“Final”).Rows.count).End(xlUp).Row+1
设置RngOne=.Range(“A2:A”和LastCell)
以
“******此部分是一个循环,将为每个选项应用过滤器,并将结果合并到最终工作表上*****
对于RngOne中的每个单元格
带图纸(“文件夹输出”)
“******如果已应用筛选器,我们将删除该筛选器*****
If.FilterMode Then.ShowAllData
'****正在清除我们将临时存储文件路径的位置中的任何剩余数据*****
工作表(“最终”)。范围(“Q1:Q100”)。清除
“******应用过滤器。标准名为CELL,它是每个过滤器选项的循环*****
.列(“A”)。自动筛选字段:=1,准则1:=单元格
'******在C列中查找筛选结果的最后一行*****
行=.Range(“C”和Sheets(“文件夹输出”).Rows.count).End(xlUp).Row
'****如果返回的行号为2,则我们知道只有1个文件路径结果*****
如果Row=“2”,则Row=.Range(“C”和Sheets(“文件夹输出”).Rows.count)。End(xlUp)。Row+1
“******仅为C列中的筛选结果设置新范围*****
尺寸rng作为范围:设置rng=.Range(“C2:C”和Row).SpecialCells(xlCellTypeVisible)
Dim rngCell As范围
'******循环以获得每个结果,并暂时将其放在Q列的最后一张表上*****
对于rng中的每个rng单元
如果图纸(“最终”)范围(“Q1”).Value=”“,则
FirstEmptyCell=.Range(“Q”)和Sheets(“Final”).Rows.count.End(xlUp.Row
工作表(“最终”).Range(“Q”和FirstEmptyCell)=rngCell.Value
其他的
FirstEmptyCell=.Range(“Q”)和Sheets(“Final”).Rows.count.End(xlUp.Row+1
工作表(“最终”).Range(“Q”和FirstEmptyCell)=rngCell.Value
如果结束
'****继续下一个筛选结果,直到该筛选的所有文件路径都完成*****
下一个rngCell
'****从粘贴的文件路径中查找最后使用的行将导致列Q*****
计数=工作表(“最终”)。单元格(行。计数,“Q”)。结束(xlUp)。行
'******循环将所有路径合并为一个字符串,但用一个字符串分隔路径*****
对于i=1进行计数
如果单元格(i,17).Value“”,则s=s&单元格(i,17).Value&“
下一个
'******在最终工作表的C列中查找最后使用的行。然后将组合文件路径粘贴到C列*****
设置EnterVal=工作表(“最终”)。范围(“C”和LastCellC)
EnterVal.Value=s
设置EnterVal=Nothing
s=“”
'******这告诉宏下次在循环中向下移动一行*****
LastCellC=LastCellC+1
以
下一个
'******循环完成后,我们将在“清理”部分结束此子项*****
去清理
'****在退出sub之前,我们将重新打开屏幕更新,并通知用户Excel文件路径已合并*****
清理:
出错时继续下一步
Application.ScreenUpdating=True
UserForm1.Hide
MsgBox(“Excel文件路径已连接!”)
出口接头
'****如果在循环过程中发生错误,我们转到此处重定向以打开更新并结束子循环*****
呃,
'执行错误处理
去清理
端接头
我可以看出,当我运行整个代码时,它正在进行所有过滤,我相信会将结果放在“最终”工作表的Q列中,但这些结果不会与;作为分隔符,然后作为一个包含多个路径的字符串放入C列

所以我认为这个问题发生在这里的某个地方,但不确定:

'***** Finding the last used row from the pasted file path results in Column Q *****
        count = Sheets("Final").Cells(Rows.count, "Q").End(xlUp).Row
        '***** Loop to combine all the paths into one string but separate the paths with a ; *****
        For i = 1 To count
            If Cells(i, 17).Value <> "" Then s = s & Cells(i, 17).Value & ";"
        Next
            '***** Find the last used row from Column C in the Final sheet. Then paste the combined file paths to Column C *****
            Set EnterVal = Worksheets("Final").Range("C" & LastCellC)
            EnterVal.Value = s
            Set EnterVal = Nothing
            s = ""
        '***** This tells the macro to move a row down next time through the loop *****
        LastCellC = LastCellC + 1
    End With
Next
“****从粘贴的文件路径中查找最后使用的行将导致列Q*****
计数=工作表(“最终”)。单元格(行。计数,“Q”)。结束(xlUp)。行
“******L”
count = Sheets("Final").Cells(Rows.count, "Q").End(xlUp).Row
With Sheets("Final)
    count = .Cells(.Rows.count, "Q").End(xlUp).Row
End with
Row = .Range("C" & Sheets("Folder Output").Rows.count).End(xlUp).Row '.Rows.Count as sheet is already qualified