Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/27.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
Excel 为什么需要激活特定工作表才能正确运行宏?_Excel_Vba - Fatal编程技术网

Excel 为什么需要激活特定工作表才能正确运行宏?

Excel 为什么需要激活特定工作表才能正确运行宏?,excel,vba,Excel,Vba,在提出这个问题时,我已经确定所有单元格和工作表都经过了适当的鉴定,排除了速度因素(DoEvents等),逐行调试(在调试模式下成功运行),并尝试激活ws1,同时没有其他工作簿保持打开状态。没有生成错误 在运行模式下成功执行代码的唯一情况是在运行的同时查看ws1。为什么? 代码: subq() Application.ScreenUpdating=False Application.Calculation=xlCalculationManual "第一部分, Dim Str1_ Str2_ Str

在提出这个问题时,我已经确定所有
单元格
工作表
都经过了适当的鉴定,排除了速度因素(
DoEvents
等),逐行调试(在调试模式下成功运行),并尝试激活
ws1
,同时没有其他工作簿保持打开状态。没有生成错误

在运行模式下成功执行代码的唯一情况是在运行的同时查看
ws1
。为什么?

代码:

subq()
Application.ScreenUpdating=False
Application.Calculation=xlCalculationManual
"第一部分,
Dim Str1_
Str2_
Str3_
Str4_
Str5_
Str6_
Str7_
Str8_
Str9_
Str10_
Str11_
Str12_
Str13_
Str14_
Str15_
Str16_
Str17作为字符串_
wsf As工作表功能
我_
j_
k,_
M,_
x,_
lRow1_
lRow2_
lRow3_
lRow4_
lRow5_
Long1_
龙2_
Long3_
龙4_
我的日期尽可能长
设置wsf=Application.WorksheetFunction
Str9=“#QU004”
myDate=Application.InputBox(“选择YYYYMMDD格式的日期:”)
Str1=“[String]”_
&myDate
Str2=Str1&“.csv”
Str3=“[String]”_
&Str2
Str4=Str1
暗淡的一年_
月_
天作为整数_
日期
年份=左(myDate,4)
月份=右侧(左侧(myDate,6),2)
天=右(myDate,2)
Dt=日期序列(年、月、日)
Str5=“[String]”_
&格式(日期添加(“d”,1,Dt),“yyyymmdd”)
Str6=Str5&“.xls”
Str7=“[String]”_
&“[字符串]”_
&“[字符串]”_
&Str6
Str8=“[String]”_
&对(myDate,2)_
&左(右(myDate,4),2)_
&右(左(myDate,4),2)
Dim wb1_
wb2_
wb3作为工作簿
设置wb1=ThisWorkbook
设置wb2=工作簿。打开(Str3)
设置wb3=工作簿。打开(Str7,,“[String]”)
wb1.激活
Dim ws1_
ws2_
ws3_
ws4 As工作表
设置ws1=wb1.工作表(“[名称]”)
设置ws2=wb2.工作表(Str4)
设置ws3=wb3.工作表(Str8)
设置ws4=wb1.工作表(“[名称]”)
变型
暗Rng1_
Rng2_
Rng3_
Rng4 As射程
使用ws1
lRow3=.Cells(.Rows.Count,1).End(xlUp).Row
lRow4=.Cells(.Rows.Count,28).End(xlUp).Row
设置Rng3=.Range(.Cells(3,1),.Cells(lRow3,23))
设置Rng4=.Range(.Cells(3,28),.Cells(lRow4,51))
以
暗淡的阴影1_
PTable2_
PTable3_
PTable4作为数据透视表
Set PTable1=wb1.Worksheets(“[Name]”)。单元格(4,2)。数据透视表
Set PTable2=wb1.工作表(“[名称]”).单元格(4,5).数据透视表
Set PTable3=ws4.Cells(4,17).数据透视表
Set PTable4=ws4.Cells(4,20).数据透视表
"第二部分
“融合wb
与ws2
lRow2=.Cells(.Rows.Count,9).End(xlUp).Row
设置Rng2=.Range(.Cells(2,1),.Cells(lRow2,23))
”“好吧
使用ws2.Sort
.SortFields.Clear
.SortFields.Add Key:=范围(“A2:A”&lRow2),顺序:=xl
.SortFields.Add Key:=范围(“B2:B”和lRow2),顺序:=xl
.SortFields.Add Key:=范围(“F2:F”&lRow2),顺序:=xl
.SetRange范围(“A2:W”和lRow2)
.Header=xlNo
.申请
以
如果lRow4>2,则
Rng4.清除
如果结束
Rng2.复制ws1.单元格(3,28)
wb2.关闭保存更改:=False
以
新界西湾
与ws3
lRow1=.Cells(.Rows.Count,9).End(xlUp).Row
设置Rng1=.Range(.Cells(3,1),.Cells(lRow1,23))
'删除Str9
对于i=lRow1至3步骤-1
rMatch=Application.Match(Str9,.Cells(i,9),0)
如果不是IsError(rMatch),则
.第(i)行。删除
如果结束
接下来我
”“好吧
使用ws3.Sort
.SortFields.Clear
.SortFields.Add Key:=范围(“B3:B”和lRow1),顺序:=xl
.SortFields.Add Key:=范围(“I3:I”和lRow1),顺序:=xl
.SortFields.Add Key:=范围(“K3:K”&lRow1),顺序:=xl
.SetRange范围(“A3:W”和lRow1)
.Header=xlNo
.申请
以
如果lRow3>2,则
Rng3,清除
如果结束
Rng1
Sub Q()

    Application.ScreenUpdating = False
    Application.Calculation = xlCalculationManual

    ' Part 1
    
        Dim Str1, _
            Str2, _
            Str3, _
            Str4, _
            Str5, _
            Str6, _
            Str7, _
            Str8, _
            Str9, _
            Str10, _
            Str11, _
            Str12, _
            Str13, _
            Str14, _
            Str15, _
            Str16, _
            Str17 As String, _
            wsf As WorksheetFunction
            
        Dim i, _
            j, _
            k, _
            M, _
            x, _
            lRow1, _
            lRow2, _
            lRow3, _
            lRow4, _
            lRow5, _
            Long1, _
            Long2, _
            Long3, _
            Long4, _
            myDate As Long

            Set wsf = Application.WorksheetFunction
            Str9 = "#QU004"
            myDate = Application.InputBox("Select date in YYYYMMDD format:")
            
            Str1 = "[String]" _
                & myDate
            Str2 = Str1 & ".csv"
            Str3 = "[String]" _
                & Str2
            Str4 = Str1
            
        Dim Year, _
            Month, _
            Day As Integer, _
            Dt As Date
            
            Year = Left(myDate, 4)
            Month = Right(Left(myDate, 6), 2)
            Day = Right(myDate, 2)
            Dt = DateSerial(Year, Month, Day)
            
            Str5 = "[String]" _
                & Format(DateAdd("d", 1, Dt), "yyyymmdd")
            Str6 = Str5 & ".xls"
            Str7 = "[String]" _
                & "[String]" _
                & "[String]" _
                & Str6
            Str8 = "[String]" _
                & Right(myDate, 2) _
                & Left(Right(myDate, 4), 2) _
                & Right(Left(myDate, 4), 2)

        Dim wb1, _
            wb2, _
            wb3 As Workbook
            
            Set wb1 = ThisWorkbook
            Set wb2 = Workbooks.Open(Str3)
            Set wb3 = Workbooks.Open(Str7, , , , "[String]")
            wb1.Activate
        
        Dim ws1, _
            ws2, _
            ws3, _
            ws4 As Worksheet
            
            Set ws1 = wb1.Worksheets("[Name]")
            Set ws2 = wb2.Worksheets(Str4)
            Set ws3 = wb3.Worksheets(Str8)
            Set ws4 = wb1.Worksheets("[Name]")
        
        Dim rMatch As Variant

        Dim Rng1, _
            Rng2, _
            Rng3, _
            Rng4 As Range
         
            With ws1
                lRow3 = .Cells(.Rows.Count, 1).End(xlUp).Row
                lRow4 = .Cells(.Rows.Count, 28).End(xlUp).Row
                Set Rng3 = .Range(.Cells(3, 1), .Cells(lRow3, 23))
                Set Rng4 = .Range(.Cells(3, 28), .Cells(lRow4, 51))
            End With
        
        Dim PTable1, _
            PTable2, _
            PTable3, _
            PTable4 As PivotTable
            
            Set PTable1 = wb1.Worksheets("[Name]").Cells(4, 2).PivotTable
            Set PTable2 = wb1.Worksheets("[Name]").Cells(4, 5).PivotTable
            Set PTable3 = ws4.Cells(4, 17).PivotTable
            Set PTable4 = ws4.Cells(4, 20).PivotTable
        
    ' Part 2
    
        ' Fusion wb
            
            With ws2
            
                lRow2 = .Cells(.Rows.Count, 9).End(xlUp).Row
                Set Rng2 = .Range(.Cells(2, 1), .Cells(lRow2, 23))
            
                ' Sort
                
                    With ws2.Sort
                        .SortFields.Clear
                        .SortFields.Add Key:=Range("A2:A" & lRow2), Order:=xlAscending
                        .SortFields.Add Key:=Range("B2:B" & lRow2), Order:=xlAscending
                        .SortFields.Add Key:=Range("F2:F" & lRow2), Order:=xlAscending
                        .SetRange Range("A2:W" & lRow2)
                        .Header = xlNo
                        .Apply
                    End With
    
                    If lRow4 > 2 Then
                        Rng4.Clear
                    End If
                    Rng2.Copy ws1.Cells(3, 28)
                    wb2.Close SaveChanges:=False
            
            End With
        
        ' NT wb
        
            With ws3
                
                lRow1 = .Cells(.Rows.Count, 9).End(xlUp).Row
                Set Rng1 = .Range(.Cells(3, 1), .Cells(lRow1, 23))
        
                ' Delete Str9
                
                    For i = lRow1 To 3 Step -1
                        rMatch = Application.Match(Str9, .Cells(i, 9), 0)
                        If Not IsError(rMatch) Then
                            .Rows(i).Delete
                        End If
                    Next i
                    
                ' Sort
                
                    With ws3.Sort
                        .SortFields.Clear
                        .SortFields.Add Key:=Range("B3:B" & lRow1), Order:=xlAscending
                        .SortFields.Add Key:=Range("I3:I" & lRow1), Order:=xlAscending
                        .SortFields.Add Key:=Range("K3:K" & lRow1), Order:=xlAscending
                        .SetRange Range("A3:W" & lRow1)
                        .Header = xlNo
                        .Apply
                    End With
            
                If lRow3 > 2 Then
                    Rng3.Clear
                End If
                Rng1.Copy ws1.Cells(3, 1)
                wb3.Close SaveChanges:=False
            
            End With
                                    
            ' Part 3
                                                   
                With ws1
                
                    For j = 3 To lRow1 + 3
                    
                        ' NT data reformatting
                            
                            If Not j > 3 Then
                            
                                For M = 3 To lRow1 + 3
                                    
                                    If .Cells(M, 2) = "[String]" Then
                                        .Cells(M, 2) = Left(.Cells(M, 2), 1) _
                                                        & Right(Left(.Cells(M, 2), 15), 1) _
                                                        & Right(Left(.Cells(M, 2), 19), 1)
                                        ElseIf .Cells(M, 2) = "[String]" Then
                                            .Cells(M, 2) = UCase(Left(.Cells(M, 2), 3))
                                    End If
                                    If .Cells(M, 2) = "[String]" Then
                                        .Cells(M, 2) = UCase(Left(.Cells(M, 2), 1) _
                                                            & Right(Left(.Cells(M, 2), 14), 1) _
                                                            & Left(Right(.Cells(M, 2), 9), 1) _
                                                            & Left(Right(.Cells(M, 2), 9), 1) _
                                                            & Right(Left(.Cells(M, 2), 23), 1))
                                        ElseIf .Cells(M, 2) = "[String]" Then
                                            .Cells(M, 2) = Left(.Cells(M, 2), 1) _
                                                            & Right(Left(.Cells(M, 2), 10), 1) _
                                                            & Right(Left(.Cells(M, 2), 20), 1)
                                    End If
                                    If .Cells(M, 2) = "[String]" Then
                                        .Cells(M, 2) = Left(.Cells(M, 2), 1) _
                                                        & Right(Left(.Cells(M, 2), 7), 1) _
                                                        & Right(Left(.Cells(M, 2), 14), 1) _
                                                        & Right(Left(.Cells(M, 2), 23), 1) _
                                                        & Left(Right(.Cells(M, 2), 7), 1)
                                        ElseIf .Cells(M, 2) = "[String]" Then
                                            .Cells(M, 2) = Left(.Cells(M, 2), 3)
                                    End If
                                    If .Cells(M, 2) = "[String]" Then
                                        .Cells(M, 2) = UCase(Left(.Cells(M, 2), 7))
                                    End If
                                    
                                    .Cells(M, 9) = Right(.Cells(M, 9), 6)
                                    If Left(.Cells(M, 9), 1) <> "Q" And .Cells(M, 2) <> "" Then
                                        .Cells(M, 9) = "Q" & .Cells(M, 9)
                                    End If
        
                                Next M
                                
                            End If
                            
                            ' j
                                ' NT
                                    Str10 = .Cells(j, 2)
                                    Str11 = .Cells(j, 9)
                                    Long1 = .Cells(j, 11)
                                ' Fusion
                                    Str12 = .Cells(j, 28)
                                    Str13 = .Cells(j, 29)
                                    Long2 = .Cells(j, 33)
                        
                            ' Part 4
                            
                                ' Matching and alignment
                                                               
                                    If Str12 = "" Or Str10 = "" Then
                                        Do
                                            Exit Do
                                        Loop
                                        ElseIf Str10 <> Str12 Or _
                                        Str11 <> Str13 Or _
                                        Long1 <> Long2 Then
                                            k = j
                                            Do
                                                k = k + 1
                                                If k = lRow1 Then
                                                    x = j
                                                    Do
                                                        x = x + 1
                                                        Str16 = .Cells(x, 28)
                                                        Str17 = .Cells(x, 29)
                                                        Long4 = .Cells(x, 33)
                                                        If Str16 = Str10 And _
                                                            Str17 = Str11 And _
                                                            Long4 = Long1 Then
                                                            .Range(.Cells(j, 1), .Cells(j + lRow4, 23)).Cut _
                                                                Destination:=.Cells(x, 1)
                                                                Exit Do
                                                        End If
                                                    Loop While x < lRow2
                                                End If
                                                Str14 = .Cells(k, 2)
                                                Str15 = .Cells(k, 9)
                                                Long3 = .Cells(k, 11)
                                                If Str14 = Str12 And _
                                                    Str15 = Str13 And _
                                                    Long3 = Long2 Then
                                                    .Range(.Cells(j, 28), .Cells(j + lRow1, 42)).Cut _
                                                        Destination:=.Cells(k, 28)
                                                        Exit Do
                                                End If
                                            Loop While k < lRow1
                                    End If
                    Next j
                            
                    lRow5 = .Cells(.Rows.Count, 28).End(xlUp).Row
                    .Range(.Cells(3, 24), .Cells(lRow5, 27)).FillDown
                    PTable1.RefreshTable
                    PTable2.RefreshTable
                    PTable3.RefreshTable
                    PTable4.RefreshTable
                    
                    ws4.Cells(2, 3) = myDate
           
                End With

    Application.Calculation = xlCalculationAutomatic

End Sub
With ws2.Sort
    .SortFields.Clear
    .SortFields.Add Key:=Range("A2:A" & lRow2), Order:=xlAscending
    .SortFields.Add Key:=Range("B2:B" & lRow2), Order:=xlAscending
    .SortFields.Add Key:=Range("F2:F" & lRow2), Order:=xlAscending
    .SetRange Range("A2:W" & lRow2)
    .Header = xlNo
    .Apply
End With
With ws2.Sort
    .SortFields.Clear
    .SortFields.Add Key:=ws2.Range("A2:A" & lRow2), Order:=xlAscending
    .SortFields.Add Key:=ws2.Range("B2:B" & lRow2), Order:=xlAscending
    .SortFields.Add Key:=ws2.Range("F2:F" & lRow2), Order:=xlAscending
    .SetRange ws2.Range("A2:W" & lRow2)
    .Header = xlNo
    .Apply
End With