0

really hoping someone can help me.

I'm new to VBA only started picking bits up a few weeks back. I'm trying to create a nested loop to consolidate data from multiple tabs in Excel.

This is based on the worksheet name beginning ">" and column 3 containing a True or False value. Data is then pasted into a sheet called import.

Both loops seem to work independently but I cant get them to work together.

Thanks in advance.

Sub TestImport()
Dim WS As Worksheet
Dim Criteria As Boolean
Dim C As Integer
Criteria = True
Application.ScreenUpdating = False
Sheets("Import").UsedRange.Offset(1).Clear
For Each WS In ThisWorkbook.Worksheets
If Left(WS.Name, 1) = ">" Then
    For C = 2 To WS.Range("A1000").End(xlUp).Row
    If WS.Cells(C, 3) = Criteria Then
        WS.Range(Cells(C, 5), Cells(C, 12)).Copy
        Worksheets("Import").Range("A100").End(xlUp).Offset(1, 0).PasteSpecial xlPasteFormulasAndNumberFormats
        End If
        Next C
    End If
Next WS
Application.ScreenUpdating = True
Sheets("Import").Select
End Sub
3
  • 4
    I cant get them to work together. - What exactly isn't working? Commented May 4, 2016 at 13:33
  • Why not directly WS.Cells(C, 5).Resize(1, 8).Copy? Commented May 4, 2016 at 13:48
  • Thanks I will use this Commented May 4, 2016 at 13:53

1 Answer 1

2

Since you are looping across the sheets in the workbook, you should fully qualify any ranges with which you are working. Specifically, the following line:

WS.Range(Cells(C, 5), Cells(C, 12)).Copy

should be changed to:

WS.Range(WS.Cells(C, 5), WS.Cells(C, 12)).Copy
Sign up to request clarification or add additional context in comments.

1 Comment

Perfect! Thank you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.