Share via

Excel Macro 1004 Error on copy paste image

Anonymous
2022-12-13T00:12:01+00:00

I recorded an excel macro to copy the table into a picture, but sometimes it runs without errors, and sometimes it fails to run, showing a 1004 error. Very annoying, I know it might be a problem with the excel software itself, because after searching a lot on the web, there is no good solution either. I will try to upload the excel file.

Sub Macro1()

'

' Image Copy Macro

'

'

Range("A2:I3").Select 

Selection.Copy 

Range("J2").Select 

ActiveSheet.Pictures.Paste 

Application.CutCopyMode = False 

 

Range("A5:I6").Select 

Selection.Copy 

Range("J5").Select 

ActiveSheet.Pictures.Paste 

Application.CutCopyMode = False 

 

Range("A8:I9").Select 

Application.CutCopyMode = False 

Selection.Copy 

Range("J8").Select 

ActiveSheet.Pictures.Paste 

 

Range("A11:I14").Select 

Application.CutCopyMode = False 

Selection.Copy 

Range("J11").Select 

ActiveSheet.Pictures.Paste 

 

Range("A16:I19").Select 

Application.CutCopyMode = False 

Selection.Copy 

Range("J16").Select 

ActiveSheet.Pictures.Paste 

 

Range("A21:I24").Select 

Application.CutCopyMode = False 

Selection.Copy 

Range("J21").Select 

ActiveSheet.Pictures.Paste 

 

Range("A26:I34").Select 

Application.CutCopyMode = False 

Selection.Copy 

Range("J26").Select 

ActiveSheet.Pictures.Paste 

End Sub

data1 data2 data3 data4 data5 data6 data7 data8 data9
1 name1 1.0 2.0 3.0 4.0 5.0 3.00 50%
data1 data2 data3 data4 data5 data6 data7 data8 data9
2 name2 1.0 2.0 3.0 4.0 5.0 3.00 50%
data1 data2 data3 data4 data5 data6 data7 data8 data9
3 name3 1.0 2.0 3.0 4.0 5.0 3.00 50%
data1 data2 data3 data4 data5 data6 data7 data8 data9
4 name4 1.0 2.0 3.0 4.0 5.0 3.00 50%
5 name5 1.0 2.0 3.0 4.0 5.0 3.00 50%
6 name6 1.0 2.0 3.0 4.0 5.0 3.00 50%
data1 data2 data3 data4 data5 data6 data7 data8 data9
7 name7 1.0 2.0 3.0 4.0 5.0 3.00 50%
8 name8 1.0 2.0 3.0 4.0 5.0 3.00 50%
9 name9 1.0 2.0 3.0 4.0 5.0 3.00 50%
data1 data2 data3 data4 data5 data6 data7 data8 data9
10 name10 1.0 2.0 3.0 4.0 5.0 3.00 50%
11 name11 1.0 2.0 3.0 4.0 5.0 3.00 50%
12 name12 1.0 2.0 3.0 4.0 5.0 3.00 50%
data1 data2 data3 data4 data5 data6 data7 data8 data9
13 name13 1.0 2.0 3.0 4.0 5.0 3.00 50%
14 name14 1.0 2.0 3.0 4.0 5.0 3.00 50%
15 name15 1.0 2.0 3.0 4.0 5.0 3.00 50%
16 name16 1.0 2.0 3.0 4.0 5.0 3.00 50%
17 name17 1.0 2.0 3.0 4.0 5.0 3.00 50%
18 name18 1.0 2.0 3.0 4.0 5.0 3.00 50%
19 name19 1.0 2.0 3.0 4.0 5.0 3.00 50%
20 name20 1.0 2.0 3.0 4.0 5.0 3.00 50%
Microsoft 365 and Office | Excel | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

5 answers

Sort by: Most helpful
  1. Anonymous
    2022-12-15T11:26:00+00:00

    Hi,

    try and this approach..

    Sub CopyPaste_Rng_As_Image()

    '## 15-Dec-2022 ##

    Dim ws, s, v1, v2, x

    Set ws = ActiveSheet ' << data in active sheet

    v1 = Array("A2:I3", "A5:I6", "A8:I9", "A11:I14", "A16:I19", "A21:I24", "A26:I34") ' << Source range

    v2 = Array("J2", "J5", "J8", "J11", "J16", "J21", "J26") '<< Target cell

    'STEP-1 / Delete 'old' images (if any)

    On Error Resume Next

    For x = 0 To UBound(v2)

    For Each s In ws.Shapes

    If Not Intersect(s.TopLeftCell, ws.Range(v2(x))) Is Nothing Then

    s.Delete

    End If

    Next s

    Next x

    On Error GoTo 0

    'STEP-2 / Add new images

    For x = 0 To UBound(v1)

    ws.Range(v1(x)).CopyPicture xlScreen, xlBitmap

    ws.Range(v2(x)).PasteSpecial

    Application.CutCopyMode = False

    Next x

    Range(v2(0)).Select

    End Sub

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2022-12-14T16:59:12+00:00

    Hi Habib,

    It's my pleasure to assist you for this issue, Thank you update, I am glad that I was able to help you.

    Best Regards Sneha

    Was this answer helpful?

    0 comments No comments
  3. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more

  4. Anonymous
    2022-12-13T22:11:46+00:00

    Hi Sneha,

    Thank you for your reply.

    "To resolve this issue, you can specify the exact rows and columns that you want to include in the range. For example, instead of using the Range("A2:I3") function, you could use the Range("A2:I3,A5:I6,A8:I9,A11:I14,A16:I19,A21:I24,A26:I34") function to select all of the ranges of cells that you want to copy. "

    My requirement is that 7 pictures need to be copied. which Range("A2:I3 to a picture and Range(A5:I6 to another picture etc.

    Ultimately I want to get 7 images.

    Actually, after the macro runs, I save the excel as a web page so I can get the pictures.

    "Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False"

    I modified the macro according to your instructions, and it works normally for the time being, but the previous macro also works normally sometimes. I will continue to test and if there are new results, I will continue to update the discussion.

    I paste the modified macro so that someone can refer to it in the future.

    Sub Macro1()

    '

    ' Image Copy Macro

    '

    '

    Range("A2:I3").Select 
    
    Selection.Copy 
    
    Range("J2").Select 
    
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False 
    
    ActiveSheet.Pictures.Paste 
    
    Application.CutCopyMode = False 
    
     
    
    Range("A5:I6").Select 
    
    Selection.Copy 
    
    Range("J5").Select 
    
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False 
    
    ActiveSheet.Pictures.Paste 
    
    Application.CutCopyMode = False 
    
     
    
    Range("A8:I9").Select 
    
    Application.CutCopyMode = False 
    
    Selection.Copy 
    
    Range("J8").Select 
    
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False 
    
    ActiveSheet.Pictures.Paste 
    
    Application.CutCopyMode = False 
    
     
    
    Range("A11:I14").Select 
    
    Application.CutCopyMode = False 
    
    Selection.Copy 
    
    Range("J11").Select 
    
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False 
    
    ActiveSheet.Pictures.Paste 
    
    Application.CutCopyMode = False 
    
     
    
    Range("A16:I19").Select 
    
    Application.CutCopyMode = False 
    
    Selection.Copy 
    
    Range("J16").Select 
    
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False 
    
    ActiveSheet.Pictures.Paste 
    
    Application.CutCopyMode = False 
    
     
    
    Range("A21:I24").Select 
    
    Selection.Copy 
    
    Range("J21").Select 
    
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False 
    
    ActiveSheet.Pictures.Paste 
    
    Application.CutCopyMode = False 
    
     
    
    Range("A26:I34").Select 
    
    Selection.Copy 
    
    Range("J26").Select 
    
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False 
    
    ActiveSheet.Pictures.Paste 
    
    Application.CutCopyMode = False 
    

    End Sub

    Thank you.

    Best ,

    Habib

    Was this answer helpful?

    0 comments No comments
  5. Anonymous
    2022-12-13T01:20:13+00:00

    Hi Habib R,

    I'm Sneha and I'd be happy to help you out with your question. Sorry for the inconvenience caused.

    The 1004 error in Excel typically indicates that there is an issue with the range of cells that you are trying to copy and paste. In your code, it looks like you are trying to copy a specific range of cells and paste them as a picture in another location.

    One potential issue with your code is that you are not specifying the exact range of cells that you want to copy. For example, in the first line of your code, you are using the Range("A2:I3") function to select a range of cells, but you are not specifying the exact rows and columns that you want to include in the range.

    To resolve this issue, you can specify the exact rows and columns that you want to include in the range. For example, instead of using the Range("A2:I3") function, you could use the Range("A2:I3,A5:I6,A8:I9,A11:I14,A16:I19,A21:I24,A26:I34") function to select all of the ranges of cells that you want to copy.

    Another potential issue with your code is that you are using the ActiveSheet.Pictures.Paste function to paste the copied cells as a picture. However, this function may not work correctly if the active sheet does not contain any pictures.

    To resolve this issue, you can use the Selection.PasteSpecial function to paste the copied cells as a picture. For example, you could replace the ActiveSheet.Pictures.Paste function with the following code:

    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False This will paste the copied cells as a picture in the currently selected range of cells.

    Overall, there are a few potential issues with your code that may be causing the 1004 error. To resolve these issues, you can try specifying the exact range of cells that you want to copy and paste, and using the Selection.PasteSpecial function to paste the copied cells as a picture.

    If you have any other questions or need assistance with anything, please don't hesitate to let me know. I'm here to help to the best of my ability.

    Give back to the Community. Help the next person who has this issue by indicating if this reply solved your problem. Click Yes or No below.

    Best Regards, Sneha

    Was this answer helpful?

    0 comments No comments