VBA problems after upgrading to Excel 2016

vandango05

Board Regular
Joined
Oct 6, 2008
Messages
110
Hi.

I've got a worksheet with many macros that have worked fine in Excel 2010 that i've just stopped using buy many colleagues still are. Here is an example of some code that will no longer run.

Code:
Sub deleteimages()Dim s As String
Dim pic As Picture
Dim rng As Range


' Set ws = ActiveSheet
Set ws = ActiveWorkbook.Worksheets("Quotation")


Set rng = ws.Range("A26:L8000")


For Each pic In ActiveSheet.Pictures
With pic
s = .TopLeftCell.Address & ":" & .BottomRightCell.Address
End With
If Not Intersect(rng, ws.Range(s)) Is Nothing Then
pic.Delete
End If
Next


End Sub

It fails at the line
Code:
For Each pic In ActiveSheet.Pictures

another section of code i'm having problems with simply filters some data

Code:
ActiveSheet.Range("$K$5:$K$7475").AutoFilter Field:=1, Criteria1:=">0", _
        Operator:=xlAnd

It appears that 2016 doesn't much like the ActiveSheet part. No doubt i'm going to have lots more issues as more users adopt 2016 so any help would be appreciated.
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
This line of code works perfectly with Excel 2016
Code:
ActiveSheet.Range("$K$5:$K$7475").AutoFilter Field:=1, Criteria1:=">0", Operator:=xlAnd

There is probably something else going on that makes Excel think something else is active

ActiveSheet is not reliable if something else is activated

It is safer to refer to the correct sheet by name or codename and that problem will disappear
 
Upvote 0
Thank you for your reply.

The error I receive is this:

run-time error '-2147319784 (80028018)': method 'select' of object'_worksheet failed

Which I should have googled as hopefully i'll find the answer.
 
Upvote 0
I suspect this is a similar problem

Code:
Sub deleteimages()
    Dim s As String
    Dim pic As Picture
    Dim rng As Range    
    ' Set ws = ActiveSheet
    Set ws = ActiveWorkbook.Worksheets("Quotation")
    Set rng = ws.Range("A26:L8000")

    For Each pic In[COLOR=#ff0000] ActiveSheet.Pictures[/COLOR]
    With pic
        s = .TopLeftCell.Address & ":" & .BottomRightCell.Address
    End With
    If Not Intersect(rng, ws.Range(s)) Is Nothing Then
        pic.Delete
    End If
Next


End Sub


Your code suggests that everything should refer to sheet Quotation

So perhaps (untested)
Code:
For Each pic In[COLOR=#ff0000] ws.Pictures[/COLOR]
 
Upvote 0
Sadly that failed. Definitely calling the ActiveSheet is causing the problems, frustrating as i've never had the issue with Excel and i've used ActiveSheet alot :(
 
Upvote 0
The error I receive is this:

run-time error '-2147319784 (80028018)': method 'select' of object'_worksheet failed
That error makes no sense with the code you have provided, as you are not selecting anything.
Are you sure that is the code that is failing & that that is the correct error message?
 
Upvote 0
Ok here is what happens. I run this:

Code:
Sub finalisequote()'
' finalisequote Macro
'
deleteimages


Sheets("Customer Data").Select
    Range("K31:l31").Select
    Selection.Copy
    Range("K32:l32").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Sheets("Quotation").Select
    
     Range("X3").Select
    Calculate
    ActiveCell.FormulaR1C1 = Date
   


    hoistnoteetc
        
    ActiveSheet.Range("$K$5:$K$7475").AutoFilter Field:=1, Criteria1:=">0", _
        Operator:=xlAnd
   
       
    Application.PrintCommunication = False
    With ActiveSheet.PageSetup
        .PrintTitleRows = ""
        .PrintTitleColumns = ""
    End With
    Application.PrintCommunication = True
    ActiveSheet.PageSetup.PrintArea = "$A$1:$I$7475"
    Application.PrintCommunication = False
    With ActiveSheet.PageSetup
        .FitToPagesWide = 1
        .FitToPagesTall = 30
        .PrintErrors = xlPrintErrorsDisplayed
        .OddAndEvenPagesHeaderFooter = True
        .DifferentFirstPageHeaderFooter = True
        .ScaleWithDocHeaderFooter = True
        .AlignMarginsHeaderFooter = True
        
    End With
    
   
    
    Application.PrintCommunication = True
        Sheets("Customer Data").Select
     Range("G31").Select
    ActiveCell.FormulaR1C1 = "On"
    Sheets("Quotation").Select
    
       
        ActiveWindow.View = xlPageBreakPreview
        
   
End Sub

and get the error Run-time error '-2147417848 (80010108)': Automation error The object invoked has disconnected from its clients.

Debug goes to line
Code:
ActiveSheet.Range("$K$5:$K$7475").AutoFilter Field:=1, Criteria1:=">0", _
        Operator:=xlAnd

I press stop and then run the same macro and then get the object error
 
Upvote 0
Selecting everything is not required and is less reliable (not because it does not work, it does, but more other things need to be correct at the time the code is running)

instead of
Code:
Sheets("Customer Data").Select
Range("K31:l31").Select
Selection.Copy
Range("K32:l32").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False

try
Code:
With Sheets("Customer Data").Range("K31:l31")
    .Copy
    .Offset(1).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
End With
 
Last edited:
Upvote 0
What is "hoistnoteetc"?
Also how are you calling the macro?
 
Upvote 0
I'm calling the Macro with a button, hoistnoteetc is just the name of another macro that runs at that stage of the code which appears to work fine.
 
Upvote 0

Forum statistics

Threads
1,223,952
Messages
6,175,594
Members
452,656
Latest member
earth

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top