Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/14.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/24.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 需要帮助更正嵌套在循环中的子例程中的“with”语句吗_Vba_Excel - Fatal编程技术网

Vba 需要帮助更正嵌套在循环中的子例程中的“with”语句吗

Vba 需要帮助更正嵌套在循环中的子例程中的“with”语句吗,vba,excel,Vba,Excel,下面是我一直在处理的一个宏,它使用名为BW TB的主工作表中的数据更新所有数字工作表中的一组值,即具有数字名称的工作表 出于某种原因,子程序ClearContents清除了所有数字表中的数据,但母版中也清除了数据,因此没有任何内容是使用其他两个子例程复制粘贴的,我不明白为什么!完整的代码如下;请看一看: Option Explicit Dim BW As String, FirstRow As Integer, LastRow As Integer, ColNo As Integer, i A

下面是我一直在处理的一个宏,它使用名为BW TB的主工作表中的数据更新所有数字工作表中的一组值,即具有数字名称的工作表

出于某种原因,子程序ClearContents清除了所有数字表中的数据,但母版中也清除了数据,因此没有任何内容是使用其他两个子例程复制粘贴的,我不明白为什么!完整的代码如下;请看一看:

Option Explicit

Dim BW As String, FirstRow As Integer, LastRow As Integer, ColNo As Integer, i As Integer

Sub Refresh_Data()

    Application.CutCopyMode = False
    Application.ScreenUpdating = False
    Application.Calculation = xlCalculationManual

    'Defines the range of rows and columns in the refreshed BW query
    BW = "BW TB"
    Worksheets(BW).Activate
    Range("A1").Activate

    Dim MyCell As Range
    Set MyCell = Cells.Find(What:="Overall Result", After:=ActiveCell, LookIn:=xlFormulas, _
    LookAt:=xlWhole, SearchOrder:=xlByColumns, SearchDirection:=xlNext, MatchCase:= _
    True, SearchFormat:=False)
    FirstRow = MyCell.End(xlUp).Row + 1
    LastRow = MyCell.Row - 1
    ColNo = MyCell.Column

    'loop to update numeric sheets
    For i = 1 To Sheets.Count
    If IsNumeric(Sheets(i).Name) Then
        Call Clearcontents
        Call PasteGLCodes
        Call PasteTBValues
    End If
    Next

    Call CheckTotals

    Application.ScreenUpdating = True
    Application.Calculation = xlCalculationAutomatic

End Sub


Private Sub Clearcontents()

'clears the contents of the sheet of Row 6 to 1000 for every column containing data in Row 6
Dim ColRange As Integer
With Worksheets(i)
    ColRange = .Cells(6, .Columns.Count).End(xlToLeft).Column
    .Range("A6", .Cells(1000, ColRange)).Clearcontents
End With    
End Sub

Private Sub PasteGLCodes()

'Pastes the range of GL codes from ColumnA
With Worksheets(BW)
    Range(.Cells(FirstRow, ColNo), .Cells(LastRow, ColNo)).Copy
End With
Worksheets(i).Range("A5").PasteSpecial xlPasteValues

End Sub

Private Sub PasteTBValues()

'Copies the formula from top row and drags to the last row
Range("B5:L5").Copy
Range("B5:L5", Range("B5:L5").Offset(LastRow - FirstRow, 0)).PasteSpecial xlPasteFormulas

'Recalculates the formulae
ActiveSheet.Calculate

'Pastes the values from the second row down to the last row
Range("B6:L6", Range("B6:L6").Offset(LastRow - FirstRow, 0)).Copy
Range("B6").PasteSpecial xlPasteValues

End Sub

Private Sub CheckTotals()

Application.Goto Worksheets("Control sheet").Range("AU114"), True
MsgBox "Update complete - check control totals"

End Sub
如果我将ClearContent替换为:

它工作正常,但显然是一种不太干净的解决方案


一如既往,非常感谢您的帮助

如果工作簿中有任何图表,那么您将引用不同的工作表,如使用工作表的刷新数据方法和使用工作表的ClearContents方法

工作表集合包含工作表和图表工作表

“工作表”集合仅包含工作表


因此,在ClearContents方法中使用工作表。

如果工作簿中有任何图表,那么您将引用不同的工作表,就像在使用工作表的刷新数据方法和在使用工作表的ClearContents方法中一样

工作表集合包含工作表和图表工作表

“工作表”集合仅包含工作表

因此,请使用ClearContents方法中的工作表。

尝试更改

.RangeA6、.Cells1000、ColRange.Clearcontents

.Range.RangeA6、.Cells1000、ColRange.Clearcontents

在你的Clearcontents子系统中

编辑我看到了您的问题:ClearContent和PasteGLCodes都不会激活第I张工作表,因此您对PasteTBValues的调用总是会在您在跑步开始时激活的工作表上运行。您需要更改最后一个接头,使其在第i页上运行…

尝试更改

.RangeA6、.Cells1000、ColRange.Clearcontents

.Range.RangeA6、.Cells1000、ColRange.Clearcontents

在你的Clearcontents子系统中


编辑我看到了您的问题:ClearContent和PasteGLCodes都不会激活第I张工作表,因此您对PasteTBValues的调用总是会在您在跑步开始时激活的工作表上运行。您需要更改最后一个接头,使其在第i张图纸上运行…

我与实际情况不同-但感谢您指出不一致之处:我与实际情况不同-但感谢您指出不一致之处:恐怕也是同样的问题。我将继续深入研究,明天再汇报:首先要做的是去掉那些全局变量,而是将i作为参数添加到Clearcontents中。或者只是重构并将clearcontents代码直接放回主例程中。此外,最好使用工作簿引用来限定所有工作表引用:例如Thisworkbook.Sheets.Count,而不仅仅是Sheets.Count-如果您打开了多个工作簿,则不使用此选项会造成混乱。恐怕同样的问题。我将继续深入研究,明天再汇报:首先要做的是去掉那些全局变量,而是将i作为参数添加到Clearcontents中。或者只是重构并将clearcontents代码直接放回主例程中。此外,最好使用工作簿引用来限定所有工作表引用:例如Thisworkbook.Sheets.Count,而不仅仅是Sheets.Count-如果打开了多个工作簿,则不这样做会造成混淆。请尝试调试、查看和检查ColRange的值,请尝试调试查看并检查ColRange的值,我。。。
Private Sub Clearcontents()

    Sheets(i).Activate
    Range("A6").EntireRow.Select
    Range(Selection, Selection.Offset(1000, 0)).Clearcontents

End Sub