Consulting

Results 1 to 8 of 8

Thread: How can I fix my autofilter?

  1. #1

    How can I fix my autofilter?

    I think my error might be with the selected range to copy maybe..? I'm trying to check if cell in column D is empty and if so, then copy the contents of cell in column C and paste into D. I'm doing this for a very large amount of data. Currently I'm trying to do this through an autofilter (open to other suggestions if anyone knows of a better way!) so that the visible contents of column C are pasted into D. But it's coming up with an error message when I run the macro: (the line beginning 'Range("D2:D" & LR.....' is the line highlighted in Debug)


        LR = Cells(Rows.Count, 1).End(xlUp).Row
        Rows("1:1").Select
        Selection.AutoFilter
        ActiveSheet.Range("D2:D" & LR).AutoFilter Field:=4, Criteria1:="="
        Range("C2", Range("C2").End(xlDown)).Select
        Selection.Copy
        Range("D2:D" & LR).SpecialCells(xlCellTypeVisible).PasteSpecial xlValues
        Rows("1:1").Select
        Application.CutCopyMode = False
        ActiveSheet.ShowAllData
        Selection.AutoFilter
        Range("A1").Select
    Is this an obvious error? ;_; My range of data will change a lot so I need this to be able to run for however many rows there are which is where the LR comes in.. Thanks in advance!

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,445
    Location
    This works

    Dim LR As Long
    Dim rngArea As Range
        LR = Cells(Rows.Count, 1).End(xlUp).Row
        Rows("1:1").AutoFilter
        ActiveSheet.Range("D2:D" & LR).AutoFilter Field:=4, Criteria1:="="
        For Each rngArea In Range("C2:C" & LR).SpecialCells(xlCellTypeVisible).Areas
        
            rngArea.Copy
            rngArea.Offset(0, 1).PasteSpecial xlValues
        Next rngArea
        ActiveSheet.ShowAllData
        Rows("1:1").AutoFilter
        Range("A1").Select
    ____________________________________________
    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
    Perfect, this worked great! Many thanks

  4. #4
    Banned VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,648
    wouldn't this ?

    Sub M_snb()
        [D1:D2000] = [if(D1:D2000="",C1:C2000,D1:D2000)]
    End Sub

  5. #5
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,094
    Location
    True snb, but what if the range was bigger ( given the the OP suggested that the range had to be for how ever many rows) in post #1
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  6. #6
    Banned VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,648
    What about changing 2000 ? no big deal I gather.

    but alternatively:

    Sub M_snb() 
      usedrange.columns(4).name="snb"
      usedrange.columns(3).name="snb1"
      [snb] = [if(snb="",snb1,snb)] 
    End Sub

  7. #7
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,645
    @snb
    since 'SNB1' is a cell address just change it to, eg, snbxx.
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  8. #8
    Banned VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,648
    Good idea; snb0 would do as well.

Tags for this Thread

Posting Permissions

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