简体   繁体   中英

need help correcting a 'with' statement inside a subroutine nested within a loop

below is a macro I've been working on that updates a set of values in all "numeric" sheets (ie sheets that have numerical names) using data from a master sheet named "BW TB".

For some reason the sub-routine "ClearContents" clears the data in all the numeric sheets, but also in the mastersheet (and thus nothing is copy-pasted using the other two sub routines) and I can't figure out why! The complete code is below; please take a look:

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

If I replace ClearContents with:

Private Sub Clearcontents()

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

End Sub

it works fine, but its obviously a less-clean solution.

As always, any help much appreciated!

Try changing

.Range("A6", .Cells(1000, ColRange)).Clearcontents

to

.Range(.Range("A6"), .Cells(1000, ColRange)).Clearcontents

in your Clearcontents sub.

EDIT I see your problem: neither Clearcontents nor PasteGLCodes activate the i'th sheet, so your call to PasteTBValues is always going to operate on the sheet you activated at the beginning of your run ("BW TB"). You need to change that last sub so it operates on the i'th sheet...

If you have any charts in the workbook, then you will reference different sheets as in the Refresh_Data Method you are using Sheets and in the ClearContents method you are using WorkSheets

The Sheets colleciton contains worksheets and chart sheets.

The Worksheets collection only contains worksheets.

So, use Sheets in the ClearContents Method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM