Results 1 to 6 of 6

Thread: [RESOLVED] error when pasting to a new workbook

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jun 2012
    Posts
    90

    [RESOLVED] error when pasting to a new workbook

    Hi

    I have a routine that copies filtered records from a worksheet into a new workbook. Everything was working fine and then it stopped and I don't see why. Error is '438 0 object doesn't support this property or method'.

    The error is happening on the line;

    Code:
    rng.Columns("C:H").SpecialCells(xlCellTypeVisible).Copy Destination:=newWorkBook.Sheet1.Range("A1")
    The new workbook opens but then the error comes. I checked and there is no paste option so nothing is on the clipboard.

    Full code is below;
    Code:
    Dim wbSaveAs As String
    Dim wbSaveName As String
    Dim newWorkBook As Workbook
    Dim rng As Range
    
    wbSaveName = Sheet4.Range("F4").Value
    wbSaveAs = Sheet4.Range("F3").Value & wbSaveName
    
    Set rng = Sheet1.ListObjects("PickList").AutoFilter.Range
    
         With Sheet1.Range("Picklist[#All]")
            
            Sheet1.ListObjects("PickList").Range.advancedfilter xlFilterInPlace, CriteriaRange:=Sheet4.Range("H1:J3"), Unique:=False
            
            Sheet1.Unprotect
            If rng.Columns(1).SpecialCells(xlCellTypeVisible).Count - 1 > 0 Then
                
                If WorkbookIsOpen(wbSaveName) Then
                    MsgBox "The workbook " & wbSaveAs & " is already open." & vbCr & vbCr & "Please close it and click Export once more.", vbInformation + vbDefaultButton1 + vbOKOnly, "Export"
    
    ' check if filters are active for the PICKLIST range and if not then set then on so that all records are
    ' displayed otherwise show all records anyway
    
                    Exit Sub
                End If
    
                lblLastExport.Caption = "Creating Workbook"
                
                Set newWorkBook = Workbooks.Add
                With newWorkBook
                
                    lblLastExport.Caption = "Copying Rows to New Workbook"
     
    ' ERROR is happening on this line           
                    rng.Columns("C:H").SpecialCells(xlCellTypeVisible).Copy Destination:=newWorkBook.Sheet1.Range("A1")
                   
                    lblLastExport.Caption = "Finished Copying Workbook"
                    
                    .SaveAs Filename:=wbSaveAs, FileFormat:=xlNormal, CreateBackup:=False
                    .Close Savechanges:=False
           
                    lblLastExport.Caption = "Workbook Created"
                
                End With
       
            End If
    Do I need to fully qualify the original workbook because the new workbook is open? If so, how to do that?

    Thanks
    Last edited by scoobster; Jul 17th, 2012 at 02:24 AM. Reason: is now resolved

  2. #2
    Don't Panic! Ecniv's Avatar
    Join Date
    Nov 2000
    Location
    Amsterdam...
    Posts
    5,343

    Re: error when pasting to a new workbook

    Do I need to fully qualify the original workbook because the new workbook is open?
    sounds like it.



    You should be holding variable references to the current and new workbooks/sheets.

    to do this - in the original workbook/code:
    Code:
    dim wrk as workbook, sht as worksheet
    dim wrkDest as workbook, shtDest as worksheet
    
    set wrk = activeworkbook
    set sht = wrk.sheets("<name of your sheet>")
    
    set wrkdest = application.workbooks.add
    set shtdest = wrkdest.sheets(1)
    then you can copy from the source to the destination
    something like :
    Code:
    rng.Columns("C:H").SpecialCells(xlCellTypeVisible).Copy
    shtDest.Range("A1").pastespecial xlPastevalues 'or whatever...?
    Just remember to release at the end of your sub...
    Code:
    set wrk=nothing 'etc

    BOFH Now, BOFH Past, Information on duplicates

    Feeling like a fly on the inside of a closed window (Thunk!)
    If I post a lot, it is because I am bored at work! ;D Or stuck...
    * Anything I post can be only my opinion. Advice etc is up to you to persue...

  3. #3
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: error when pasting to a new workbook

    I don't think that you need to fully qualify the new workbook or the old one... the references you have should be fine. The reason that the paste option isn't on is because you gave it a destination to copy to... if you omit that, then it'll copy to the clipboard. Because you're going from one work book to another, you might need to copy to the clipboard, then do a paste into the other.... *shrug*.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

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

    Re: error when pasting to a new workbook

    > Set rng = Sheet1.ListObjects("PickList").AutoFilter.Range

    Can you paste this line of the code after the above line and tell me what do you get?

    Code:
    Msgbox rng.Address
    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

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Jun 2012
    Posts
    90

    Re: error when pasting to a new workbook

    All working fine now so thanks for the input.

    Working code to copy the value of columns C to K onto cell A1 of the newly created workbook is as follows;

    Code:
                    rng.Columns("C:K").SpecialCells(xlCellTypeVisible).Copy
                    shtDest.Range("A1").PasteSpecial xlPasteValues

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

    Re: error when pasting to a new workbook

    Now that we've helped you, you can help us by marking the thread as resolved. If you have JavaScript enabled you can do that by selecting the Mark Thread Resolved item from the Thread Tools menu. Otherwise please insert "[Resolved]" at the start of the Subject and select the green checkmark from the Post Icons.

    If someone has been particularly helpful you also have the ability to affect their forum 'reputation' by rating their post. Only those ratings that you give after you have 20 posts will actually count, but everyone you rate will know that you appreciate their help.
    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

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