Amendment on existing code

Kabous

New Member
Joined
Mar 29, 2013
Messages
5
Good day, Hope you would be able to assist. I got the following code from Use AutoFilter to filter and copy the results to a existing worksheet and would like to incorporate this into my VBA project. The problem however is that this code were written to perform on one workbook and this is where my problem is. My project is between two different workbooks and cannot seem to get this code modified to do what it is supposed to do between these two workbooks. Everything I have tried so far failed. In short what this code would do is to check the existing data on the one sheet (the source) and extract only the data which is meeting my set criteria, and copy this data to the destination sheet. This is what I would like to do between two workbooks. With this the sample code as provided by Ron de Bruin. The sample workbook could be accessed trough the following link http://www.rondebruin.nl/files/Copy5Code.zip. With this the code for matching and copying on one workbook.
Code:
Option Explicit
'<<<<  Filter/Copy below the existing data of a existing worksheet(Sheet: RecordsOfTheNetherlands)  >>>>>
'This example will copy the filter results below the existing data on the destination sheet.
'Note the sheet "RecordsOfTheNetherlands" must exist in your workbook.
'This example will not copy the header row each time so when you manual add the worksheet
'"RecordsOfTheNetherlands" to your workbook you must add the headers yourself on the first row.
'I commented the Delete line in this tester
'rng.EntireRow.Delete
'Remove the ' before the code line if you want to delete the records you copied
'Note: this example use the function LastRow in the ModReset module
Sub Copy_With_AutoFilter2()
'Note: This macro use the function LastRow
'Important: The DestSh must exist
    Dim My_Range As Range
    Dim DestSh As Worksheet
    Dim CalcMode As Long
    Dim ViewMode As Long
    Dim FilterCriteria As String
    Dim CCount As Long
    Dim rng As Range
    'Set filter range on ActiveSheet: A11 is the top left cell of your filter range
    'and the header of the first column, D is the last column in the filter range.
    'You can also add the sheet name to the code like this :
    'Worksheets("Sheet1").Range("A11:D" & LastRow(Worksheets("Sheet1")))
    'No need that the sheet is active then when you run the macro when you use this.
    Set My_Range = Range("A11:D" & LastRow(ActiveSheet))
    My_Range.Parent.Select
    'Set the destination worksheet
    'Note: the sheet "RecordsOfTheNetherlands" must exist in your workbook.
    Set DestSh = Sheets("RecordsOfTheNetherlands")
    If ActiveWorkbook.ProtectStructure = True Or _
       My_Range.Parent.ProtectContents = True Then
        MsgBox "Sorry, not working when the workbook or worksheet is protected", _
               vbOKOnly, "Copy to new worksheet"
        Exit Sub
    End If
    'Change ScreenUpdating, Calculation, EnableEvents, ....
    With Application
        CalcMode = .Calculation
        .Calculation = xlCalculationManual
        .ScreenUpdating = False
        .EnableEvents = False
    End With
    ViewMode = ActiveWindow.View
    ActiveWindow.View = xlNormalView
    ActiveSheet.DisplayPageBreaks = False
    'Firstly, remove the AutoFilter
    My_Range.Parent.AutoFilterMode = False
    'Filter and set the filter field and the filter criteria :
    'This example filter on the first column in the range (change the field if needed)
    'In this case the range starts in A so Field 1 is column A, 2 = column B, ......
    'Use "<>Netherlands" as criteria if you want the opposite
    My_Range.AutoFilter Field:=1, Criteria1:="=Netherlands"
    'If you want to filter on a cell value you can use this, use "<>" for the opposite
    'This example uses the activecell value
    'My_Range.AutoFilter Field:=1, Criteria1:="=" & ActiveCell.Value
    'This will use the cell value from A2 as criteria
    'My_Range.AutoFilter Field:=1, Criteria1:="=" & Range("A2").Value
    ''If you want to filter on a Inputbox value use this
    'FilterCriteria = InputBox("What text do you want to filter on?", _
     '                          "Enter the filter item.")
    'My_Range.AutoFilter Field:=1, Criteria1:="=" & FilterCriteria

    'Check if there are not more then 8192 areas(limit of areas that Excel can copy)
    CCount = 0
    On Error Resume Next
    CCount = My_Range.Columns(1).SpecialCells(xlCellTypeVisible).Areas(1).Cells.Count
    On Error GoTo 0
    If CCount = 0 Then
        MsgBox "There are more than 8192 areas:" _
             & vbNewLine & "It is not possible to copy the visible data." _
             & vbNewLine & "Tip: Sort your data before you use this macro.", _
               vbOKOnly, "Copy to worksheet"
    Else
        'Copy the visible data and use PasteSpecial to paste to the Destsh
        With My_Range.Parent.AutoFilter.Range
            On Error Resume Next
            ' Set rng to the visible cells in My_Range without the header row
            Set rng = .Offset(1, 0).Resize(.Rows.Count - 1, .Columns.Count) _
                      .SpecialCells(xlCellTypeVisible)
            On Error GoTo 0
            If Not rng Is Nothing Then
                'Copy and paste the cells into DestSh below the existing data
                rng.Copy
                With DestSh.Range("A" & LastRow(DestSh) + 1)
                    ' Paste:=8 will copy the columnwidth in Excel 2000 and higher
                    ' Remove this line if you use Excel 97
                    .PasteSpecial Paste:=8
                    .PasteSpecial xlPasteValues
                    .PasteSpecial xlPasteFormats
                    Application.CutCopyMode = False
                End With
                'Delete the rows in the My_Range.Parent worksheet
                'rng.EntireRow.Delete
            End If
        End With
    End If
    'Close AutoFilter
    My_Range.Parent.AutoFilterMode = False
    'Restore ScreenUpdating, Calculation, EnableEvents, ....
    ActiveWindow.View = ViewMode
    Application.Goto DestSh.Range("A1")
    With Application
        .ScreenUpdating = True
        .EnableEvents = True
        .Calculation = CalcMode
    End With
End Sub
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.

Kabous

New Member
Joined
Mar 29, 2013
Messages
5
Good day,

I know you guys are busy to try and help all of us with issues - therfor would I just like to inform you that I have manage to figure this one out with alot of strougles and experimenting.

What seemed to have caused my problem here with the copy between two workbooks were the statement in the existing code as follow

Code:
    Set My_Range = Range("A11:D" & LastRow(ActiveSheet))
    My_Range.Parent.Select

I have change this part as follow

Code:
    Set My_Range = Workbooks("REQUEST").Sheets("INPUTDATA").Range("A6:S" & LastRow(ActiveSheet))
    My_Range.Copy

After this change were this code working 100%

Thanks for your help till thusfar
 
Upvote 0

Forum statistics

Threads
1,195,990
Messages
6,012,727
Members
441,723
Latest member
iansitorus

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top