|
-
Jul 6th, 2012, 06:40 AM
#1
Thread Starter
Lively Member
[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
-
Jul 6th, 2012, 08:15 AM
#2
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
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...
-
Jul 6th, 2012, 12:50 PM
#3
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
-
Jul 7th, 2012, 05:58 PM
#4
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?
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
-
Jul 10th, 2012, 08:11 AM
#5
Thread Starter
Lively Member
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
-
Jul 16th, 2012, 11:37 AM
#6
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|