+ Reply to Thread
Results 1 to 22 of 22

pastespecial method of worksheet class failed

Hybrid View

  1. #1
    Registered User
    Join Date
    02-27-2014
    Location
    Melbourne
    MS-Off Ver
    Excel 2003
    Posts
    22

    pastespecial method of worksheet class failed

    Hi
    I am actually trying to copy named ranges (have 10 different named range sections) to a new csv file for each of the named range.

    Instead of copying the formula forma, I want to copy just the values of the named range.

    So here is my macro in MODULE :-



    VB:
    Sub sheetToCSV() 
        Dim MyPath As String 
        Dim MyFileName As String 
         'The path and file names:
        MyPath = "D:\_ShopFloor BI queries" 
        MyFileName = "MyFileName" & Format(Date, "ddmmyy") 
         'Validation: Making sure the path name ends with "\":
        If Not Right(MyPath, 1) = "\" Then MyPath = MyPath & "\" 
         'validation: Making sure the filename ends with ".csv"
        If Not Right(MyFileName, 4) = ".csv" Then MyFileName = MyFileName & ".csv" 
         'This copies the sheet to a new workbook. Change the name between "" to your sheet name.
         'Sheets("Book2").Copy
        ThisWorkbook.Names("Day_AC_Out").RefersToRange.Select 
        Selection.Copy 
        Workbooks.Add 
        ActiveSheet.PasteSpecial xlPasteValues 
        Application.CutCopyMode = False 
         'The new workbook becomes Activeworkbook:
        With ActiveWorkbook 
             'Saves the new workbook to given folder / filename:
            .SaveAs Filename:= _ 
            MyPath & MyFileName, _ 
            FileFormat:=xlCSV, _ 
            CreateBackup:=False 
             'Closes the file
            .Close False 
        End With 
        ActiveWorkbook.Save 
        ActiveWindow.Close 
        Range("Day_AC_Out").Select 
        Selection.ClearContents 
    End Sub
    And MACRO IN THISWORKBOOK:-



    VB:
    Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean) 
        Call sheetToCSV 
    End Sub
    I am getting ERROR in the module section " ActiveSheet.PasteSpecial xlPasteValues"
    Any suggestions...

    ps:- I am pretty new to VB macro stuff so plz be patient.

  2. #2
    Forum Guru :) Sixthsense :)'s Avatar
    Join Date
    01-01-2012
    Location
    India>Tamilnadu>Chennai
    MS-Off Ver
    2003 To 2010
    Posts
    12,770

    Re: pastespecial method of worksheet class failed

    Here is the error part

    ActiveSheet.PasteSpecial xlPasteValues
    When you use Sheet to paste then it should be

    ActiveSheet.Paste
    When it is range then you can use in that way like this...

    ActiveSheet.Range("A1").PasteSpecial xlPasteValues


    If your problem is solved, then please mark the thread as SOLVED>>Above your first post>>Thread Tools>>
    Mark your thread as Solved


    If the suggestion helps you, then Click *below to Add Reputation

  3. #3
    Registered User
    Join Date
    02-27-2014
    Location
    Melbourne
    MS-Off Ver
    Excel 2003
    Posts
    22

    Re: pastespecial method of worksheet class failed

    THanks for the quick reply..,.
    so does that mean I can define name range in the ActiveSheet.Range("NAMED RANGE").PasteSpecial xlPasteValues ?

  4. #4
    Registered User
    Join Date
    02-27-2014
    Location
    Melbourne
    MS-Off Ver
    Excel 2003
    Posts
    22

    Re: pastespecial method of worksheet class failed

    ACtually I got - application object error object defined error for this step :

     ActiveSheet.Range("Day_AC_Out").PasteSpecial xlPasteValues

  5. #5
    Forum Guru :) Sixthsense :)'s Avatar
    Join Date
    01-01-2012
    Location
    India>Tamilnadu>Chennai
    MS-Off Ver
    2003 To 2010
    Posts
    12,770

    Re: pastespecial method of worksheet class failed

    Pasting bunch of data on a limited range (Named Range) may be the root cause for this issue

    Just select the first cell of that named range and paste so that the data get pasted easily

    Try like this...

    ActiveSheet.Range("Day_AC_Out").Cells(1).PasteSpecial xlPasteValues

  6. #6
    Registered User
    Join Date
    02-27-2014
    Location
    Melbourne
    MS-Off Ver
    Excel 2003
    Posts
    22

    Re: pastespecial method of worksheet class failed

    ACtually, it works if put in the actual range and not mention the DEFINE NAME, as follows :-

    ThisWorkbook.Names("All_Data_v2").RefersToRange.Select
        Selection.Copy
        Workbooks.Add
        ActiveSheet.Range("$B$39:$O$74").PasteSpecial xlPasteValues
        Application.CutCopyMode = False
    I can live with the above approach by defining the range values and not names,

    However whenever I try to hit SAVE button > it runs the macro successfully but also crashes the EXCEL (giving me option to restart Excel)...

    whats causing this ?

  7. #7
    Forum Guru :) Sixthsense :)'s Avatar
    Join Date
    01-01-2012
    Location
    India>Tamilnadu>Chennai
    MS-Off Ver
    2003 To 2010
    Posts
    12,770

    Re: pastespecial method of worksheet class failed

    You can write it on two ways...

    ThisWorkbook.Names("All_Data_v2").RefersToRange.Cells(1).Copy
    Or

    ThisWorkbook.Range("All_Data_v2").Cells(1).Copy
    No need of Selection.Copy when we apply the copy process directly

  8. #8
    Forum Guru :) Sixthsense :)'s Avatar
    Join Date
    01-01-2012
    Location
    India>Tamilnadu>Chennai
    MS-Off Ver
    2003 To 2010
    Posts
    12,770

    Re: pastespecial method of worksheet class failed

    Oopss... Ignore my Post #7 content

    $B$39:$O$74 refers to a named range? What is the name of that named range?

  9. #9
    Registered User
    Join Date
    02-27-2014
    Location
    Melbourne
    MS-Off Ver
    Excel 2003
    Posts
    22

    Re: pastespecial method of worksheet class failed

    it is All_Data_V2...

    And also after implementing the changes you mentioned
    ThisWorkbook.Names("All_Data_v2").RefersToRange.Cells(1).Copy
    It is now not copying the data in the whole range but jst the first cell.

    will I need to define the range instead of using .Cells(1) place ?

  10. #10
    Registered User
    Join Date
    02-27-2014
    Location
    Melbourne
    MS-Off Ver
    Excel 2003
    Posts
    22

    Re: pastespecial method of worksheet class failed

    hmmm.. back to square 1.. it works but crashing the excel...

    The complete code is as follows :

    Sub sheetToCSV()
        Dim MyPath As String
        Dim MyFileName As String
        'The path and file names:
        MyPath = "D:\_ShopFloor BI queries"
        MyFileName = "MyFileName1" & Format(Date, "ddmmyy")
        'Validation: Making sure the path name ends with "\":
        If Not Right(MyPath, 1) = "\" Then MyPath = MyPath & "\"
        'validation: Making sure the filename ends with ".csv"
        If Not Right(MyFileName, 4) = ".csv" Then MyFileName = MyFileName & ".csv"
        'This copies the sheet to a new workbook. Change the name between "" to your sheet name.
        'Sheets("Book2").Copy
        ThisWorkbook.Names("All_Data_v2").RefersToRange.Select
        Selection.Copy
        Workbooks.Add
        ActiveSheet.Range("$B$39:$O$74").PasteSpecial xlPasteValues
        Application.CutCopyMode = False
        'The new workbook becomes Activeworkbook:
        With ActiveWorkbook
        'Saves the new workbook to given folder / filename:
            .SaveAs Filename:= _
                MyPath & MyFileName, _
                FileFormat:=xlCSV, _
                CreateBackup:=False
        'Closes the file
            .Close False
        End With
        ActiveWorkbook.Save
        ActiveWindow.Close
        Range("All_Data_v2").Select
        Selection.ClearContents
    End Sub
    Please advise of the changes if you can, and your prompt reply and help is mch appreciated.

    Cheers
    A.B.
    Last edited by abhi900; 04-16-2014 at 02:10 AM.

  11. #11
    Forum Guru :) Sixthsense :)'s Avatar
    Join Date
    01-01-2012
    Location
    India>Tamilnadu>Chennai
    MS-Off Ver
    2003 To 2010
    Posts
    12,770

    Re: pastespecial method of worksheet class failed

    In your post #10 code change this line

    ActiveSheet.Range("$B$39:$O$74").PasteSpecial xlPasteValues
    to

    ActiveSheet.Range("B39").PasteSpecial xlPasteValues
    Please check and confirm

  12. #12
    Registered User
    Join Date
    02-27-2014
    Location
    Melbourne
    MS-Off Ver
    Excel 2003
    Posts
    22

    Re: pastespecial method of worksheet class failed

    ok.. one more update > if I comment out the line
     End With
        ActiveWorkbook.Save
        'ActiveWindow.Close
        Range("All_Data_v2").Select
        'Selection.ClearContents
    This stops the EXCEL to CRASH and the process works just fine.

    Thanks mate.

  13. #13
    Registered User
    Join Date
    02-27-2014
    Location
    Melbourne
    MS-Off Ver
    Excel 2003
    Posts
    22

    Re: pastespecial method of worksheet class failed

    Can you tell me the difference in using complete Range in the syntax v/s using Range.Cells(1) approach ?

    What does or will this code "range.cells(1)" do ?

  14. #14
    Forum Guru :) Sixthsense :)'s Avatar
    Join Date
    01-01-2012
    Location
    India>Tamilnadu>Chennai
    MS-Off Ver
    2003 To 2010
    Posts
    12,770

    Re: pastespecial method of worksheet class failed

    Quote Originally Posted by abhi900 View Post
    What does or will this code "range.cells(1)" do ?
    Assume that you selected 15 cells A1:C5 and done the copy and you write code and ask excel to paste the data in C2:C3.

    In A1:C5 there is 15 cells with three columns and 5 rows each
    In C2:C3 which possess only 2 cells with two columns.

    So in that case both the ranges are not similar in size so excel cannot paste that data.

    Using range(C2:C3).cells(1) is like placing the cursor in C2 cell and doing the paste process in this case excel will auto determine the paste ranges.

    range(C2:C3).cells(1) refers to first cell of that range so ultimately it will be C2

  15. #15
    Registered User
    Join Date
    02-27-2014
    Location
    Melbourne
    MS-Off Ver
    Excel 2003
    Posts
    22

    Re: pastespecial method of worksheet class failed

    That makes sense... but in my case I am defining a range from it needs to copy the values and paste it in a new file so in effective it is pasting from A1 location onwards.
    Hence it wont apply to this situation right ?

    Also can I ask you, If I want to run the same code 9 times for each range then I will copy the code one below the other 9 times and jst change the filename / Define Range / Actual Range for each one of them.
    And when I hit the save button it will create 9 different files for each range defined.

    I tried doing this, initially for 2 more range but it came with an error of Ambiguous name detected SUB sheetToCsv()

    What do I need to do over here ?

  16. #16
    Forum Guru :) Sixthsense :)'s Avatar
    Join Date
    01-01-2012
    Location
    India>Tamilnadu>Chennai
    MS-Off Ver
    2003 To 2010
    Posts
    12,770

    Re: pastespecial method of worksheet class failed

    Quote Originally Posted by abhi900 View Post
    paste it in a new file so in effective it is pasting from A1 location onwards.
    Hence it wont apply to this situation right ?
    Refer Post #2 to know about the Pasting method. Pasting on a sheet requires just .paste but on the range it differs as .pastespecial.

    At the same time keep in mind that pasting on the sheet will paste the data on the activecell on which the cursor is present.

    If I want to run the same code 9 times for each range
    Use looping (For Loop / Do Loop) system in this case

  17. #17
    Registered User
    Join Date
    02-27-2014
    Location
    Melbourne
    MS-Off Ver
    Excel 2003
    Posts
    22

    Re: pastespecial method of worksheet class failed

    one uses loop to rerun the same code again and again..till a condition is met,

    but over here I do need to run the same code again but I have to change the file name for each range and a different range_name and Range of data for 9 different Ranges.
    How would 1 do this activity ?

  18. #18
    Forum Guru :) Sixthsense :)'s Avatar
    Join Date
    01-01-2012
    Location
    India>Tamilnadu>Chennai
    MS-Off Ver
    2003 To 2010
    Posts
    12,770

    Re: pastespecial method of worksheet class failed

    Enter the named ranges on a range of cells with desired file names and use a loop on it

  19. #19
    Registered User
    Join Date
    02-27-2014
    Location
    Melbourne
    MS-Off Ver
    Excel 2003
    Posts
    22

    Re: pastespecial method of worksheet class failed

    Sorry mate,

    Can you show me for jst 2 such ranges..in a loop from my codes above, please.

  20. #20
    Forum Guru
    Join Date
    08-26-2007
    Location
    London
    Posts
    4,606

    Re: pastespecial method of worksheet class failed


  21. #21
    Registered User
    Join Date
    02-27-2014
    Location
    Melbourne
    MS-Off Ver
    Excel 2003
    Posts
    22

    Re: pastespecial method of worksheet class failed

    apologies Stephen... i thought I am using 2 different forumns 1. OZGRID forum 2. excelforum and hence my double post.

    did'nt knew they basically point to the same underlying forum.

  22. #22
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,663

    Re: pastespecial method of worksheet class failed

    ...did'nt knew they basically point to the same underlying forum.
    They do not; but, some members belong to both. That means you have people duplicating the work of others without knowing about it.
    Ben Van Johnson

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. vba error 1004 pastespecial method of Worksheet class failed
    By gdewolf in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-28-2014, 08:09 PM
  2. [SOLVED] PasteSpecial Method of Range Class Failed
    By mrbickelsworth in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 12-05-2013, 06:40 PM
  3. PasteSpecial Method of Worksheet class failed
    By Gitch28 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-14-2012, 08:54 PM
  4. Run-Time error'1004' PasteSpecial Method of Worksheet class failed
    By gj in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 07-31-2006, 06:48 AM
  5. PasteSpecial method of Range class failed
    By windsor in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 10-06-2005, 06:05 PM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1