Consulting

Results 1 to 5 of 5

Thread: Sleeper: Copy filtered table

  1. #1

    Sleeper: Copy filtered table

    I am using Excel 2016

    I have the code below which worked perfectly the day I created it. Now I get an error on the line in bold. The error is 91: Object variable or with block variable not set. How was it set the other day and not today?
    Part 2 of my question is once this code is run and I have a new table on another sheet with a new name how do I assign a variable to the new table name?
    Thank you in advance.

    Sub CreateCustomerTable()
    Dim CustomerName As String
    CustomerName = InputBox("Which Customer")
    Sheets("FabricatedParts").Activate
        ActiveSheet.ListObjects("Parts").Range.AutoFilter Field:=5, Criteria1:= _
           CustomerName
    ActiveSheet.AutoFilter.Range.Copy
    Sheets.Add After:=Sheets("Summary")
        ActiveSheet.Paste
    Application.CutCopyMode = False
        'Insert table and name it.
        ActiveSheet.ListObjects.Add(xlSrcRange, Selection, , xlYes).Name = _
            "CustomerName" & "Table"
        With ActiveSheet
        .Name = Range("E2").Value
        End With
    End Sub

  2. #2
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,645
    Please, use code tags !

    Sub M_snb()
       sheets.add(,sheets("summary")).name="customer"
    
       with Sheets("FabricatedParts").ListObjects("Parts").Range
         .autofilter 5, InputBox("Which Customer")
         .Copy sheets("customer").cells(1)
         .autofilter
       end with
    
       with sheets("customer")
         .ListObjects.Add(xlSrcRange, Selection, , xlYes).Name ="CustomerName" & "Table"
         .Name = .Range("E2")
           End With
        End Sub

  3. #3
    I can understand why your code is in some ways better than mine but the results are that it copies the whole table not just the filtered results. In addition, mine is once again working perfectly which begs the original question of why now and not yesterday? Sorry about not tagging the code.

  4. #4
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,645
    it copies the whole table not just the filtered results
    It doesn't; it only copies the filtered result.

  5. #5
    TableScreenshot2.jpgScreenshot of table built with your code. Not sure you can see but it includes all parts from all customers, not just the parts from the one customer.

Posting Permissions

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