SOLVED

Macro working only on debugging mode

Brass Contributor

Hi 

 

The Macro Runs on debugging mode but does not run normally where it either breaks at a different step everytime or runs smoothly but doesn't paste the graph shape and images in the compare sheet (line 32).

 

File attached for your reference.

 

 

 

Sub CompareButton()


Dim var As Integer
'

Sheets("Input").Select
ActiveSheet.ChartObjects("TopGraph").Copy
Sheets("workbench").Select
Range("C3").Select
ActiveSheet.Pictures.Paste.Select
Selection.Name = "GroupGraph"

Sheets("Input").Select
Range("Table2[ITM]").Copy

Sheets("Picture Query").Select
Range("B3").PasteSpecial xlPasteValues

ActiveSheet.Shapes.Range(Array("_P1", "_P2", "_P3", "_P4", "_P5", "_P6", "_P7", "_P8", "_P9", "_P10", "_P11", "_P12", "_P13", "_P14", "_P15", "_P16", "_P17", "_P18", "_P19", "_P20")).Select
Selection.Copy
Sheets("workbench").Select
Range("M2").Select
ActiveSheet.PasteSpecial Format:="Picture (Enhanced Metafile)", Link:=False, DisplayAsIcon:=False
Application.CutCopyMode = False
Selection.Name = "GroupPics"
ActiveSheet.Shapes.Range(Array("GroupPics", "GroupGraph", "_BG")).Select
Selection.Copy
Sheets("Compare").Select
var = Range("B1").Value
Range("A2").Offset((var) * 26, 0).Select
ActiveSheet.Paste
ActiveCell.Value = var + 1
Range("A2").Select
Sheets("workbench").Select
ActiveSheet.Shapes.SelectAll
Selection.ShapeRange.Delete

Sheets("Input").Select
ActiveSheet.Shapes("_BG").Visible = True
ActiveSheet.Shapes("_BG").Copy
Sheets("workbench").Select
Range("A1").Select
ActiveSheet.Paste

Sheets("Picture Query").Select
Range("A2").Select
Sheets("Input").Select
ActiveSheet.Shapes("_BG").Visible = False
Range("A1").Select


End Sub

 

 

 

 

I would really appreciate any help at the earliest.

 

Thanks in advance

Vignesh

 

 

3 Replies

@V-GEe7 

It appears to be a timing problem. In the version below, I inserted a DoEvents line, Does it work for you?

Sub CompareButton()
    Dim var As Integer
    Sheets("Input").Select
    ActiveSheet.ChartObjects("TopGraph").Copy
    Sheets("workbench").Select
    Range("C3").Select
    ActiveSheet.Pictures.Paste.Select
    Selection.Name = "GroupGraph"
   
    Sheets("Input").Select
    Range("Table2[ITM]").Copy
    
    Sheets("Picture Query").Select
    Range("B3").PasteSpecial xlPasteValues
    
    ActiveSheet.Shapes.Range(Array("_P1", "_P2", "_P3", "_P4", "_P5", "_P6", "_P7", "_P8", "_P9", "_P10", "_P11", "_P12", "_P13", "_P14", "_P15", "_P16", "_P17", "_P18", "_P19", "_P20")).Select
    Selection.Copy
    DoEvents
    Sheets("workbench").Select
    Range("M2").Select
    ActiveSheet.PasteSpecial Format:="Picture (Enhanced Metafile)", Link:=False, DisplayAsIcon:=False
    Application.CutCopyMode = False
    Selection.Name = "GroupPics"
    ActiveSheet.Shapes.Range(Array("GroupPics", "GroupGraph", "_BG")).Select
    Selection.Copy
    Sheets("Comparison").Select
    var = Range("B1").Value
    Range("A2").Offset((var) * 26, 0).Select
    ActiveSheet.Paste
    ActiveCell.Value = var + 1
    Range("A2").Select
    Sheets("workbench").Select
    ActiveSheet.Shapes.SelectAll
    Selection.ShapeRange.Delete
     
    Sheets("Input").Select
    ActiveSheet.Shapes("_BG").Visible = True
    ActiveSheet.Shapes("_BG").Copy
    Sheets("workbench").Select
    Range("A1").Select
    ActiveSheet.Paste
    
    Sheets("Picture Query").Select
    Range("A2").Select
    Sheets("Input").Select
    ActiveSheet.Shapes("_BG").Visible = False
    Range("A1").Select
End Sub
It seems to be working now though,
Thank you so much for your response.
Could you also help me understand why that helps?

Thanks in advance,
V
best response confirmed by V-GEe7 (Brass Contributor)
Solution

@V-GEe7 Sometimes, the application cannot keep up with the code. DoEvents gives it a moment to catch up.

1 best response

Accepted Solutions
best response confirmed by V-GEe7 (Brass Contributor)
Solution

@V-GEe7 Sometimes, the application cannot keep up with the code. DoEvents gives it a moment to catch up.

View solution in original post