Results 1 to 4 of 4

Thread: [RESOLVED] Replacing a filtered range

  1. #1

    Thread Starter
    Addicted Member Fonty's Avatar
    Join Date
    May 2006
    Location
    New York
    Posts
    173

    Resolved [RESOLVED] Replacing a filtered range

    I have a macro, which as the last step, has to take a column from a workbook and replace another in the active workbook. For this I simply use
    WorkBooks(workboook1).WorkSheets(sheetx).Range("X1:X111").Value=WorkBooks(workboook2).WorkSheets(she etz).Range("Z1:Z111").Value

    It worked perfectly. But I have just discovered that it doesn't work if I have initially filtered some value in the "sheetx". How can I replace a whole range even if part of this range is filtered?
    Last edited by Fonty; Feb 16th, 2007 at 03:28 PM.

  2. #2
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Replacing a filtered range

    It worked perfectly. But I have just discovered that it doesn't work if I have initially filtered some value in the "sheetx". How can I replace a whole range even if part of this range is filtered?
    The only way that i can think of is looping through all the cells in the columns and rows and doing what you are doing above

    for example

    VB Code:
    1. Private Sub CommandButton1_Click()
    2.  
    3. startrange = 1
    4. endrange = 15 'last cell of the row
    5. startrange1 = 1
    6. endrange1 = 6 'last cell of the column
    7.  
    8. For i = startrange To endrange
    9.     For j = startrange1 To endrange1
    10.     'sheets2-Destination sheet
    11.     'sheets1-Source Sheet
    12.  
    13.     Sheets("sheet2").Cells(i, j).Value = Sheets("sheet1").Cells(i, j).Value
    14.  
    15.     Next j
    16. Next i
    17.  
    18. End Sub

    Hope this helps...
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  3. #3
    Addicted Member malik641's Avatar
    Join Date
    Sep 2005
    Location
    South Florida :-)
    Posts
    221

    Re: Replacing a filtered range

    Found these two procedures on the help files. I modified it to remove the filter on sheetx and then restore it:
    VB Code:
    1. Option Explicit
    2.  
    3. Dim w As Worksheet
    4. Dim filterArray()
    5. Dim currentFiltRange As String
    6.  
    7. Public Sub RemoveFilters()
    8.     Dim f As Long
    9.    
    10.     Set w = Worksheets("Sheet1")
    11.     With w.AutoFilter
    12.         currentFiltRange = .Range.Address
    13.         With .Filters
    14.             ReDim filterArray(1 To .Count, 1 To 3)
    15.             For f = 1 To .Count
    16.                 With .Item(f)
    17.                     If .On Then
    18.                         filterArray(f, 1) = .Criteria1
    19.                         If .Operator Then
    20.                             filterArray(f, 2) = .Operator
    21.                             filterArray(f, 3) = .Criteria2
    22.                         End If
    23.                     End If
    24.                 End With
    25.             Next
    26.         End With
    27.     End With
    28.    
    29.     w.AutoFilterMode = False
    30. '    w.Range("A1").AutoFilter field:=1, Criteria1:="S"
    31. End Sub
    32.  
    33. Public Sub RestoreFilters()
    34.     Dim col As Long
    35.    
    36.     Set w = Worksheets("Sheet1")
    37.     w.AutoFilterMode = False
    38.     For col = 1 To UBound(filterArray(), 1)
    39.         If Not IsEmpty(filterArray(col, 1)) Then
    40.             If filterArray(col, 2) Then
    41.                 w.Range(currentFiltRange).AutoFilter field:=col, _
    42.                     Criteria1:=filterArray(col, 1), _
    43.                         Operator:=filterArray(col, 2), _
    44.                     Criteria2:=filterArray(col, 3)
    45.             Else
    46.                 w.Range(currentFiltRange).AutoFilter field:=col, _
    47.                     Criteria1:=filterArray(col, 1)
    48.             End If
    49.         End If
    50.     Next
    51. End Sub
    I commented out what the help originally had (to find it, in the VBE type in "Autofilter Object" and go to that link).

    Anyway you can use it like this:
    VB Code:
    1. Option Explicit
    2.  
    3. Public Sub TestMe()
    4.     Dim wb1 As Excel.Workbook
    5.     Dim wb2 As Excel.Workbook
    6.     Dim rng1 As Excel.Range
    7.     Dim rng2 As Excel.Range
    8.    
    9.     Set wb1 = Workbooks("Book1")
    10.     Set wb2 = Workbooks("Book2")
    11.    
    12.     Set rng1 = wb1.Sheets("Sheet1").Range("A2:A10")
    13.     Set rng2 = wb2.Sheets("Sheet1").Range("A2:A10")
    14.    
    15.     Call RemoveFilters
    16.    
    17.     rng1.Value = rng2.Value
    18.    
    19.     Call RestoreFilters
    20. End Sub
    As a small example. If your original filter doesn't match the filter you had before replacing the values, it will still filter to the original criteria without problems (it will be under "Custom..." in the Autofilter dropdown).

    Hope this helps too!




    If you find any of my posts of good help, please rate it

  4. #4

    Thread Starter
    Addicted Member Fonty's Avatar
    Join Date
    May 2006
    Location
    New York
    Posts
    173

    Re: Replacing a filtered range

    In fact, also regarding the posts I posted this week, I'm starting to think that somehow AutoFilter is causing a lot of bugs in my file. It seems that it suddenly disables Calculate after trying to run the macros with filters on.

Posting Permissions

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



Click Here to Expand Forum to Full Width