Consulting

Results 1 to 7 of 7

Thread: Solved: Copying a range from one sheet to a new sheet in the background

  1. #1
    VBAX Contributor
    Joined
    Feb 2007
    Posts
    126
    Location

    Solved: Copying a range from one sheet to a new sheet in the background

    Hi,

    Currently I am filtering for some errors in my sheet using the following code:

    [vba]Application.ScreenUpdating = False
    With Cells
    .Columns("A:L").AutoFilter Field:=1, Criteria1:="0"
    .Rows("2:" & .Rows.Count).SpecialCells(xlCellTypeVisible).EntireRow.Copy
    .Columns("A:L").AutoFilter Field:=1, Criteria1:="<>"
    End With
    Application.ScreenUpdating = True
    [/vba]

    So currently I just delete all the rows selected.

    I would like to however add a new Sheet called "Errors" and copy the data prior deletion into this new sheet:

    [vba] .Rows("2:" & .Rows.Count).SpecialCells(xlCellTypeVisible).EntireRow.Copy
    .Rows("2:" & .Rows.Count).SpecialCells(xlCellTypeVisible).EntireRow.Copy[/vba]

    The problem is I don't seem to figure out how to do this, without making "Error" sheet an Active one... Basically I want this copying to happen in the background as I need to have my current sheet Active for other purposes...

    Thanks a lot

    Mike

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    expression.Copy(Destination)

    expression Required. An expression that returns a Range object.

    Destination Optional Variant. Specifies the new range to which the specified range will be copied. If this argument is omitted, Microsoft Excel copies the range to the Clipboard.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    VBAX Contributor
    Joined
    Feb 2007
    Posts
    126
    Location

    This does not work... for example

    Because as soon as I add sheet "Error" it becomes active...

    [vba]Application.ScreenUpdating = False
    With Cells
    .Columns("A:L").AutoFilter Field:=1, Criteria1:="0"
    Set xlSheet = ActiveWorkbook.Sheets.Add
    xlSheet.Name = "Error"
    .Rows("2:" & .Rows.Count).SpecialCells(xlCellTypeVisible).EntireRow.Copy Destination:=Worksheets("Error")
    .Rows("2:" & .Rows.Count).SpecialCells(xlCellTypeVisible).EntireRow.Delete
    .Columns("A:L").AutoFilter Field:=1, Criteria1:="<>"
    End With
    Application.ScreenUpdating = True[/vba]

  4. #4
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Stop using activesheet.

    Set a sheet variable to the activesheet at the start and then refer to the sheet via that variable, just as you are doing wiith xlSheet for the new sheet.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  5. #5
    VBAX Master
    Joined
    Jul 2006
    Location
    Belgium
    Posts
    1,286
    Location
    [VBA]Sub Copy_Error()
    Dim xlSheet As Worksheet
    Dim Current As Worksheet
    Dim dest_pos As Range
    Dim xlSheet_row As Long
    'Activesheet because I don't know the name
    'of the sheet that you are checking for errors
    Set Current = ActiveWorkbook.ActiveSheet
    Set xlSheet = ActiveWorkbook.Sheets.Add
    xlSheet.Name = "Error"
    'Probably always one because I suppose you will
    'delete the errorsheet afterwards based on the fact
    'that you create this sheet by coding.
    xlSheet_row = xlSheet.Range("A" & Rows.Count).End(xlUp).Row
    Set dest_pos = xlSheet.Range("A" & xlSheet_row)
    Current.Activate
    With Cells
    .Columns("A:L").AutoFilter Field:=1, Criteria1:="0"
    .Rows("2:" & .Rows.Count).SpecialCells(xlCellTypeVisible).EntireRow.COPY dest_pos
    .Rows("2:" & .Rows.Count).SpecialCells(xlCellTypeVisible).EntireRow.Delete
    .Columns("A:L").AutoFilter Field:=1, Criteria1:="<>"
    End With
    End Sub[/VBA]

  6. #6
    VBAX Contributor
    Joined
    Feb 2007
    Posts
    126
    Location

    Worked well except did not copy into "Error" but created 2nd sheet "Sheet4"

    Created sheet "Error" & sheet "Sheet4"

    Left "Error" blank and copied correct stuff to "Sheet4"



    Thanks

    Mike

  7. #7
    VBAX Contributor
    Joined
    Feb 2007
    Posts
    126
    Location

    Sorry I am wrong ;)) It works!!!! :)) Cheers!

    Thanks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •