Arlen Beiler
Arlen Beiler

Reputation: 15866

VBA Error 1004 - PasteSpecial method of Range class

Recently I started getting the error 1004: PasteSpecial method of Range class failed. I know people have said before that it might be that it is trying to paste to the active sheet when there is none, however, as you can see, everything is based on ThisWorkbook so that shouldn't be the problem. It happens extra much when Excel doesn't have the focus.

'references the Microsoft Forms Object Library
Sub SetGlobals()
    Set hwb = ThisWorkbook' home workbook
    Set mws = hwb.Worksheets("Code Numbers") ' main worksheet
    Set hws = hwb.Worksheets("Sheet3") ' home worksheet (Scratch pad)
    Set sws = hwb.Worksheets("Status") ' Status sheet
    Set aws = hwb.Worksheets("Addresses") ' Addresses sheet
End Sub
Sub Import()

    Call SetGlobals
    hws.Select
    'a bunch of code to do other stuff here.
    For Each itm In itms
        Set mitm = itm
        body = Replace(mitm.HTMLBody, "<img border=""0"" src=""http://www.simplevoicecenter.com/images/svc_st_logo.jpg"">", "")
        Call Buf.SetText(body)
        Call Buf.PutInClipboard
        Call hws.Cells(k, 1).Select
        Call hws.Cells(k, 1).PasteSpecial

        For Each shape In hws.Shapes
            shape.Delete
        Next shape

        'Some code to set the value of k 
        'and do a bunch of other stuff.
    Next itm
End Sub

Update: mitm and itm have two different types, so I did it for intellisense and who knows what else. This code takes a list of emails and pastes them into excel so that excel parses the html (which contains tables) and pastes it directly into excel. Thus the data goes directly into the sheet and I can sort it and parse it and whatever else I want.

I guess I'm basically asking for anyone who knows another way to do this besides putting it in an html file to post it. Thanks

Upvotes: 0

Views: 3039

Answers (1)

Peter Albert
Peter Albert

Reputation: 17475

This probably will not exactly answer your problem - but I noticed a few things in your source code that are too long to place in a comment, so here it is. Some of it is certainly because you omitted it for the example, but I'll mention it anyway, just in case:

  • Use Option Explicit - this will avoid a lot of errors as it forces you to declare every variable
  • Call SetGlobals can be simplified to SetGlobals - same for Call Buf.SetText(body) = Bof.SetText Body, etc.
  • No need to '.Select' anything - your accessing everything directly through the worksheet/range/shape objects (which is best practice), so don't select (hws.Select, hws.Cells(k,1).Select)
  • Why Set mitm = itm? mitm will therefore be the same object as itm - so you can simply use itm
  • You're deleteing all shapes in hwsmultiple times - for each element in itms. However, once is enough, so move the delete loop outside of the For Each loop
  • Instead of putting something in the clipboard and then pasting it to a cell, just assign it directly: hws.Cells(k, 1).Value = body - this should solve your error!
  • Instead of using global variables for worksheets that you assign in 'SetGlobals', simply use the sheet objects provided by Excel natively: If you look at the right window in the VBE with the project tree, you see worksheet nodes Sheet1 (sheetname), Sheet2 (sheetname), etc.. You can rename these objects - go to their properties (F4) and change it to meaningful names - or your current names (hwb, mws, ...) if you want. Then you can access them throughout your code without any assignment! And it'll work later, even if you change the name of Sheet3to something meaningful! ;-)

Thus, taking it all into account, I end up with the following code, doing the same thing:

Option Explicit

Sub Import()

    'a bunch of code to do other stuff here.

    For Each shape In hws.Shapes
        shape.Delete
    Next shape

    For Each itm In itms
        Call hws.Cells(k, 1) = Replace(itm.HTMLBody, "<img border=""0"" src=""http://www.simplevoicecenter.com/images/svc_st_logo.jpg"">", "")

        'Some code to set the value of k 
        'and do a bunch of other stuff.
    Next itm
End Sub

Upvotes: 4

Related Questions