+ Reply to Thread
Results 1 to 8 of 8

Runtime error PasteSpecial method of Range class failed

Hybrid View

  1. #1
    Registered User
    Join Date
    12-17-2012
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    4

    Runtime error PasteSpecial method of Range class failed

    Help would be appreciated to resolve intermittentt runtime error, code fails at PasteSpecial. I would like to understand the error and not just a fix.
    Any help to the issue would be great.

    Set xlApp = New Excel.Application 'opens new instance
    Set xlBook = xlApp.Workbooks.Open("c:\temp\inventory")
    Set xlSheet = xlBook.Worksheets("Sheet1")
    Set oRange = xlSheet.Columns(1)

    Set aCell = oRange.Find(What:=MyName, LookIn:=xlValues, LookAt:=xlWhole, _
    SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False)
    If Not aCell Is Nothing Then
    firstName = aCell.Address

    Do
    nextRow = ActiveSheet.Cells(Rows.Count, "A").End(xlUp).Row + 1
    aCell.Resize(, 4).Copy
    ActiveSheet.Cells(nextRow, 1).PasteSpecial Paste:=xlValues
    Application.CutCopyMode = False
    Set aCell = oRange.FindNext(aCell)
    Loop While Not aCell Is Nothing And aCell.Address <> firstName
    Application.Quit
    End If
    End Sub
    Last edited by russ50; 12-18-2012 at 12:46 AM. Reason: Issue solved

  2. #2
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,651

    Re: Runtime error PasteSpecial method of Range class failed

    I don't know why it error's.

    Can I ask if there is a reason to create a new instance of Excel? You could open your inventory workbook within the same instance.

    I suspect the error is due to multiple instances of Excel and maybe (guessing) that some instances are not closed when no MyName match is found because the Application.Quit is within the If-block and would only execute for a match.

    When it errors, do a Ctrl+Alt+Delete to open the Windows Task Manager. How many instances of Excel do you see in the Processes list?

  3. #3
    Registered User
    Join Date
    12-17-2012
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    4

    Re: Runtime error PasteSpecial method of Range class failed

    Thanks for information, I did notice several instances of excel open and tried to correct with Application.Quit. Fairly new with VBA and still learning.

  4. #4
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,651

    Re: Runtime error PasteSpecial method of Range class failed

    Quote Originally Posted by russ50 View Post
    Thanks for information, I did notice several instances of excel open and tried to correct with Application.Quit. Fairly new with VBA and still learning.
    This should open your inventory workbook in the same Excel instance.

    Sub Test()
        
        Dim MyName As String
        Dim xlBook As Workbook
        Dim oRange As Range
        Dim wsDest As Worksheet
        
        Set wsDest = ActiveSheet
        MyName = ????
        
        Application.ScreenUpdating = False
        
        Set xlBook = Workbooks.Open("c:\temp\inventory.xlsx")
        Set oRange = xlBook.Worksheets("Sheet1").Columns(1)
        
        Set aCell = oRange.Find(What:=MyName, LookIn:=xlValues, LookAt:=xlWhole, _
                                SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False)
        If Not aCell Is Nothing Then
            firstName = aCell.Address
            Do
                nextRow = wsDest.Cells(wsDest.Rows.Count, "A").End(xlUp).Row + 1
                aCell.Resize(, 4).Copy
                wsDest.Cells(nextRow, 1).PasteSpecial Paste:=xlValues
                Set aCell = oRange.FindNext(aCell)
            Loop While Not aCell Is Nothing And aCell.Address <> firstName
        End If
        
        Application.CutCopyMode = False
        xlBook.Close SaveChanges:=False 'close inventory workbook
        Application.ScreenUpdating = True
        
    End Sub

  5. #5
    Registered User
    Join Date
    12-17-2012
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    4

    Re: Runtime error PasteSpecial method of Range class failed

    Thanks again but I'm questioning the use of an explicit variable with .PasteSpecial method. I'm not nitpicking just learning.

  6. #6
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,651

    Re: Runtime error PasteSpecial method of Range class failed

    Quote Originally Posted by russ50 View Post
    Thanks again but I'm questioning the use of an explicit variable with .PasteSpecial method. I'm not nitpicking just learning.
    I don't know what that means? Is that a question or comment?

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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