Excel忽略了其他工作簿中可用的代码

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Excel忽略了其他工作簿中可用的代码相关的知识,希望对你有一定的参考价值。

我有在其他几个工作簿中运行的代码,但是在特定的工作簿中似乎被忽略了。

我能看到的唯一的区别是行SaveAs Filename:SaveAs FileName:的区别。我以某种方式无法想象这会导致整个脚本被忽略,但??

另一件事是,当我尝试将代码从Filename更改为FileName时,excel在转到下一行时立即将其更改回。

文件损坏?

为草率的代码表示歉意...:(

```Sub Create_Individual_Files()

Application.ScreenUpdating = False
    Application.Calculation = xlCalculationManual
    Application.DisplayAlerts = False




    Dim i               As Integer
    Dim x               As String
    Dim Lastrow         As Long
    Dim NewBook         As Workbook
    Dim Sourcewb        As Workbook: Set Sourcewb = ThisWorkbook
    Dim FileExtStr      As String
    Dim FileFormatNum   As Long
    Dim FName           As String
    Dim Fpath           As String
    Dim FName2          As String

        Fpath = Sheets("Variables").Range("B1").Text
        FName = Sheets("Variables").Range("B9").Text
        FName2 = Sheets("Variables").Range("B2").Text

    'Find the last row of data in each  tab.
    Lastrow = Sourcewb.Sheets(1).Cells(Sourcewb.Sheets(1).Rows.Count, "A").End(xlUp).Row


    'This section creates each new file, retaining all formulas, from the existing tabs in the master workbook. Then saves the file with the individuals name.
    For i = 2 To Lastrow
        x = Sourcewb.Sheets(1).Range("A" & i).Value
        Sourcewb.Sheets(Array("Summary", "Pivot", "Data", "Modifier %", "Modifier Dollar", "Variables")).Copy
        Set NewBook = ActiveWorkbook
        FileExtStr = ".xlsx": FileFormatNum = 51
        With NewBook
            .SaveAs Filename:=Fpath & FName2 & "-" & x & FName & ".xlsx"
            '.Close False


        End With



    ''''''''''''''''''''''''''''''''''''''''''''
    'The section below deletes data from each tab that is not specific to the individual.

    Sheets("Variables").Select
'    Range("C5").Select
'    Selection.Copy
    Range("B10").Select
    ActiveCell.FormulaR1C1 = x
'    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
'        :=False, Transpose:=False
        Sheets("Summary").Select
    Call Loop_Delete_Summary
        Sheets("Data").Select
        ActiveSheet.ListObjects(1).Unlist
    Call Loop_Delete_Summary
        Sheets("Modifier %").Select
        ActiveWorkbook.ActiveSheet.ListObjects(1).Unlist
    Call Delete_Modifier_Percent
        Sheets("Modifier Dollar").Select
        ActiveSheet.ListObjects(1).Unlist
    Call Loop_Delete_Summary
        'Sheets("Controls").Select
        Sheets("Variables").Select
    ActiveWindow.SelectedSheets.Delete
    Call ResetCursor


    ''''''''''''''''''''''''''''''''''''''''''''
         Next i



        ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''


  Application.ScreenUpdating = True
    Application.Calculation = xlCalculationAutomatic
    Application.DisplayAlerts = True


End Sub```







```Sub Loop_Delete_Summary()

    Dim Firstrow As Long
    Dim Lastrow As Long
    Dim Lrow As Long
    Dim CalcMode As Long
    Dim ViewMode As Long
    Dim rng As Range




    With ActiveSheet


        .Select

        ViewMode = ActiveWindow.View
        ActiveWindow.View = xlNormalView


        'Set the first and last row to loop through
        Firstrow = .UsedRange.Offset(1).Row
        Lastrow = .UsedRange.Rows(.UsedRange.Rows.Count).Row

        'Loop from Lastrow to Firstrow (bottom to top)
        For Lrow = Lastrow To Firstrow Step -1

            'Check the values in the A column
            With .Cells(Lrow, "A")

                If Not IsError(.Value) Then

                    If .Value <> Worksheets("Variables").Range("B10") Then


                        If rng Is Nothing Then
                            Set rng = .Cells
                        Else
                            Set rng = Application.Union(rng, .Cells)
                        End If
                    End If

                End If
            End With

        Next Lrow

    End With

    'Delete all rows at once
    If Not rng Is Nothing Then rng.EntireRow.Delete

    ActiveWindow.View = ViewMode
    With Application
        .ScreenUpdating = True
        .Calculation = CalcMode
    End With




End Sub```







```Sub Loop_Delete_Modifier_Percent()
    Dim Firstrow As Long
    Dim Lastrow As Long
    Dim Lrow As Long
    Dim CalcMode As Long
    Dim ViewMode As Long

    With Application
        CalcMode = .Calculation
        .Calculation = xlCalculationManual
        .ScreenUpdating = False
    End With

    'We use the ActiveSheet but you can replace this with
    'Sheets("MySheet")if you want
    With ActiveSheet

        'We select the sheet so we can change the window view
        .Select

        'If you are in Page Break Preview Or Page Layout view go
        'back to normal view, we do this for speed
        ViewMode = ActiveWindow.View
        ActiveWindow.View = xlNormalView

        'Turn off Page Breaks, we do this for speed
        .DisplayPageBreaks = False

        'Set the first and last row to loop through
        Firstrow = 2
        Lastrow = .Cells(.Rows.Count, "B").End(xlUp).Row

        'We loop from Lastrow to Firstrow (bottom to top)
        For Lrow = Lastrow To Firstrow Step -1

            'We check the values in the A column in this example
            With .Cells(Lrow, "B")

                If Not IsError(.Value) Then

Select Case .Value
Case Is <> Worksheets("Variables").Range("B10").Value: .EntireRow.Delete
End Select

                End If

            End With

        Next Lrow

    End With

    ActiveWindow.View = ViewMode
    With Application
        .ScreenUpdating = True
        .Calculation = CalcMode
    End With
End Sub```

*****使用F8逐步执行时的代码似乎跳过了本节... *****

``` x = Sourcewb.Sheets(1).Range("A" & i).Value
        Sourcewb.Sheets(Array("Summary", "Pivot", "Data", "Modifier %", "Modifier Dollar", "Variables")).Copy
        Set NewBook = ActiveWorkbook
        FileExtStr = ".xlsx": FileFormatNum = 51
        With NewBook
            .SaveAs Filename:=Fpath & FName2 & "-" & x & FName & ".xlsx"
            '.Close False


        End With


   Sheets("Variables").Select
'    Range("C5").Select
'    Selection.Copy
    Range("B10").Select
    ActiveCell.FormulaR1C1 = x
'    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
'        :=False, Transpose:=False
        Sheets("Summary").Select
    Call Loop_Delete_Summary
        Sheets("Data").Select
        ActiveSheet.ListObjects(1).Unlist
    Call Loop_Delete_Summary
        Sheets("Modifier %").Select
        ActiveWorkbook.ActiveSheet.ListObjects(1).Unlist
    Call Delete_Modifier_Percent
        Sheets("Modifier Dollar").Select
        ActiveSheet.ListObjects(1).Unlist
    Call Loop_Delete_Summary
        'Sheets("Controls").Select
        Sheets("Variables").Select
    ActiveWindow.SelectedSheets.Delete
    Call ResetCursor


         Next i```
答案

看来选项卡的顺序是问题所在。我注意到Lastrow = Sourcewb.Sheets(1).Cells(Sourcewb.Sheets(1).Rows.Count, "A").End(xlUp).Row

Lastrow = 0。

我将没有数据的前两个选项卡移到了选项卡的末尾,并且可以使用。

否则,无论出于何种原因,它都会在最后一行找到零,然后直接跳到末尾。

以上是关于Excel忽略了其他工作簿中可用的代码的主要内容,如果未能解决你的问题,请参考以下文章

如何对 Excel VBA 代码进行单元测试

xlam Excel 插件是不是可以覆盖工作簿中的子项?

VBA代码根据excel工作簿中的值从webform下拉列表中选择一个值

Excel2016 文件运行宏,出现可能是因为该宏在此工作簿中不可用,或者所有的宏都被禁用

怎么拆分一个Excel工作簿中的多个工作表?

Excel 宏,用于从工作簿中的主工作表更新其他工作表