Rows getting deleted through Excel VBA code but shapes not re-aligning - excel

gwbkEach.Worksheets(6).Range(rngDeleteStart, rngDeleteEnd.Offset(-1)).EntireRow.Delete
The above code deletes row 11-54($11:$54). But the shapes which lies beyond Row-54 does not shift upwards. All shapes has the placement property set to xlMoveAndSize.
When I manually delete the rows the shapes re-position themselves
accordingly as expected.
Strangely, when I put a break-point in the above line of code then
everything works fine and the shapes re-position themselves as
expected.
I tried to use DoEvents , Application.Wait around the above line of code, but nothing seems to work. Please help me guys.

The issue got fixed when I added a code to activate the sheet:
gwbkEach.Worksheets(6).Activate ' FIXED THE ISSUE
gwbkEach.Worksheets(6).Range(rngDeleteStart, rngDeleteEnd.Offset(-1)).EntireRow.Delete
NOTE: You have to avoid using Application.ScreenUpdating = False in order for the above solution to work

Related

Copying Editable Excel chart to Word Error 4605

I have a macro that is intended to copy a chart from excel into a word document in the same way as manually copy and pasting using "Keep Source Formatting and Embed Workbook." Below is the code that, to my understanding, should accomplish this.
Set reductionChart = graphWorksheet.ChartObjects("reduction")
reductionChart.Copy
masterReport.Paragraphs.Last.Range.PasteAndFormat wdChart
graphWorksheet is the worksheet that contains the graphs, masterReport is the word document.
The issue I am having is error 4605 command not available on the PasteAndFormat line. I happened to discover that manually copying the graph then running the line worked without issue. Thinking that maybe right clicking copied in a different way than .copy so I record a macro of the action and ended up with:
ActiveSheet.ChartObjects("Reduction").Activate
ActiveChart.ChartArea.Copy
Even substituting this in the error still occurs. What is happening here?
After some additional testing I am thinking that possibly when using .copy the chart is sort of only stored within excel and not the clipboard so when paste and format looks for something it see an empty clipboard and has an error, but right clicking copy stores it to the clipboard hence why is available still even after I run ActiveChart.ChartArea.Copy again.
I've attempted to create new workbook with a single sheet and chart. Also tried using late binding instead on the off chance that did something. This is the full code still giving the same issue
Sub test()
Dim masterWord As Object
Dim masterReport As Object
Set masterWord = CreateObject("Word.Application")
Set masterReport = masterWord.Documents.Add
masterWord.Visible = True
ThisWorkbook.Worksheets(1).ChartObjects("Chart 1").Copy
masterReport.Paragraphs.last.Range.PasteAndFormat wdChart
End Sub
After much testing this is the best workaround I'm managed to figure out.
reductionChart.Chart.ChartArea.Copy
masterReport.Paragraphs.Last.Range.PasteAndFormat wdFormatOriginalFormatting
masterReport.InlineShapes(masterReport.InlineShapes.Count).LinkFormat.BreakLink
wdFormatOriginalFormatting and wdPasteDefault both work and don't seem to make a difference to the outcome as far as I have seen.
This has one issue that I've found which is that on ActiveDocument.Fields.Update an information box will popup warning that the linked file is unavailable. This will occur for each unlinked item. I attempted to use Application.DisplayAlerts = False but this did not prevent the popup. This may come back to bite me, but I simply removed this line as it was unnecessary for my purposes.

How to reset picture back from artistic effect in Excel VBA

I am trying to figure out something. I have a picture in my Excel file. Whenever I click on it I want it to become grayscale so I use the following vba code:
Sheets("Dashboard-3").Shapes("ReportsIcon").Fill.PictureEffects.Insert msoEffectPencilGrayscale
And it works perfectly fine. But then I want it to get back to its original color and formatting when I click again (Which is the Reset Picture button in Excel). unfortunately recording Macro doesnt get that. I use msoEffectNone, msoEffectTypeNone, but doesnt work. Also searched a lot but seems no one has tried to reset the artistic effects back. Does anyone have a clue what should I use?
You don't need to set a different effect, you have to remove it with Delete from the list of PictureEffects that are assigned to the picture. The following Sub shows how to toggle it (if it is present, it is removed, else applied)
Sub toggleEfect(sh As Shape, efecttype As Long)
Dim fe As PictureEffect, found As Boolean
For Each fe In sh.Fill.PictureEffects
If fe.Type = efecttype Then
fe.Delete
found = True
End If
Next
If Not found Then sh.Fill.PictureEffects.Insert efecttype
End Sub
You can call it with
toggleEfect Sheets("Dashboard-3").Shapes("ReportsIcon"), msoEffectPencilGrayscale

Excel crash after Copy and paste Chart image

I Need to Copy an picture/image of a chart which is in another workbook (WbO), and paste the picture in a worksheet in this workbook (TWb), using Excel vba.
I figured out a simple code to do this.
The good thing is that the code works. Stepping line by line throught the code everything completes perfectly.
However, if I run the code (no Stepping) it also runs until the end without any error and completes the job. BUT, by the end Excel Crashes without any error message, restarts and reopens the workbook. (all work lost).
The weirdiest thing is that it crashes 3 to 5 seconds after ending the run. 3 to 5 seconds is an enormous lenght of time for computing. What is it doing during this time??
For testing, I added a 'Msgbox "Completed", vbokonly' as last code line. This made vba stop and show the message, and no crash while the message is on screen. Clicking ok, the macro runs to the end and then, after some seconds ... Crash.
Searched for similar problems in the web and found some old posts refering errors related to copy/paste, but no complete crash like this.
Tried this same file/code in different PCs, both with up-to-date office 365 but different update channels (not same build). The result was exactly the same.
Tried changing the .CopyPicture statement to a normal Copy, and then using PasteSpecial as Picture. Same Result.
Even tried moving the copy/Paste instructions to different subs (suggestion from a 2018 post), and inserting DoEvents in between .. without any luck.
Does someone have a clue why this is hapenning?
How to overcome this issue?
Help welcomed
Sample code below:
Application.EnableEvents= False
Set WbO = /Workbooks.Open("WbOFileName",0)
Set ChrtObj= Worksheets("MyCharts").ChartObjects(1) 'Set handle to the Chart obj
TWb.Activate ' Activate destination Workbook = The Wb which contains the macro
ChrtObj.CopyPicture
RangeToPasteOn(1,1).Select ' Select Top-Left cell to paste the Chart on
ActiveSheet.paste ' Paste the Chart
' ... here Code to ajust Size and position to fit destination range
Application.CutCopyMode = False
Set ChartObj = Nothing 'Release handle
WbO.Close
Set WbO = Nothing
Application.EnableEvents= True

How to position a chart copied from excel into powerpoint using CommandBars?

I am trying to copy some charts (embedded) in different worksheets of an excel workbook using vbscript into different slides of a powerpoint. I would like to keep the link between the excel sheet and the powerpoint while doing so and therefore I used the below piece of code that allows me to keep formatting and link (instead of a simple paste or PasteSpeical. Is there any other way?) :
For i = 1 to TotalNumWorkSheets 'I iterated with indices.
Set pptSlide = pptPres.Slides.Add(i, 11) 'There is one opening slide before this.
set ws = wb.Worksheets(i)
ws.ChartObjects(1).Chart.ChartArea.Copy
pptApp.CommandBars.ExecuteMso("PasteExcelChartSourceFormatting")
pptApp.CommandBars.ReleaseFocus
With pptSlide
.Shapes.Title.TextFrame.TextRange.Text = objCurSheet.Name
'Adding some more textboxes here. Working fine. Position checks out in PPT.
.Shapes(.Shapes.Count).Left = 20 'Doesn't work for all slides.
End With
Next
The code works and copies all the charts, creates titles, adds new text as expected, but I am not able to position the charts on individual slides because after the ExecuteMso command, I don't know how to access the reference to the chart. I read in one of the SO posts that pasting using this method looses the chart selection but you can access the last .Shapes object since pasting always adds the object to the end of the list. Is that always the case? I tried accessing and positioning my chart by accessing the last object but it only works for the first slide of the loop (i.e. the first chart pasted is shifted to Left=20). The rest all charts in other slides are centered. Can someone explain where and how to add the formatting chart options? I ran into an even weirder problem. If I increase the number of worksheets, even the first plot looses it's Left formatting. The above code is the only place where I add formatting so I don't know what is happening. I am sure I am not formatting it correctly.
Many thanks for your suggestions.
Edit: One additional thing which I tested. I am using
WScript.Sleep 500
code before the For loop ends since it gives enough time for earlier operations to finish (at least that's what I understood from many other google searches).
So after some more searching and testing, I found a solution (for my case at least).
Moved the WScript.Sleep 1000 (500 didn't work for me) statement just below CommandBars.ReleaseFocus.
It makes some sense now since it is the Chart copying, pasting and linking from excel that needs time. Especially with source formatting. After that there is only text generation which I believe is not so heavy.
Cleared all the set Object variables when not used. Especially the ones associated with the "With" keyword.
set obj = CreateObject()
With obj
'Do something here.
End With
set obj = Nothing
Not clearing them, apparently, can also prevent you from closing the applications even after you use the .Close and .Quit method. At least that's what I observed. I found PowerPoint.exe running in the task manager when nothing was opened.
After doing the above, I am able to copy with format the charts and also able to set the position of the charts. The weird problem of larger number of worksheets also disappeared. Hopefully, it might help others. If someone thinks the observations are incorrect or troublesome, please correct.

Excel VB Listbox losing Value and Text Property

I'm trying to solve this problem along 2 days but I've not found the solution.
I have a lot of listboxes on my excel and each of these listboxes are filled with different data, also I use these listboxes to change some filters at a pivot table using a VB code.
The problem is that after some usage these listboxes stop to work, they stop to get the values that are selected.
I've discovered that two properties, 'Value' and 'Text' has been disappeared after the problem happened. To use the listboxes again I need to close excel and open it again, also if I try to save the workbook, it stop to work.
I've tried to bring some images of it, but I got no reputation yet.
Who can help me?
Updating:
Here is the images:
And here is the code behind the listboxes (all listboxes has a similar code):
If ListFaturaFatura.List(ListFaturaFatura.ListIndex) = "Total" Or _
ListFaturaFatura.List(ListFaturaFatura.ListIndex) = "" Then
Sheets("Fatura_Data").PivotTables("DinamicaFatura1").PivotFields("FATURA").ClearAllFilters
Else
Sheets("Fatura_Data").PivotTables("DinamicaFatura1").PivotFields("FATURA").CurrentPage = _
ListFaturaFatura.List(ListFaturaFatura.ListIndex)
End If
Also, I think that is an excel problem, because when it happens all workbooks are affected with the same problem.
I saw that one of the listboxes was filled with a named range, I just removed the named range and filled the listbox manually, this seems to work fine and didn't give me more problems.

Resources