'Selection' Compile Error

gplhl

Board Regular
Joined
Feb 23, 2002
Messages
176
Hi All,

having a problem with Excel 2016, code that had always worked has stopped working! When it is run I get an error. "Compile error: Expected Function or variable"
and highlighted is "Selection" in the code.

I am trying to write new code to do other jobs and anything with "Selection." in it fails with the same error!

Anyone else come across this or have an answer, it's completely messing up my work at the moment.
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
can you post a sample of your code.
 
Upvote 0
Yep, where I have selection in bold is where it fails, this worked previously:

Sub SelectedRangeToImage()
' modified
Dim tmpChart As Chart, n As Long, shCount As Long, sht As Worksheet, sh As Shape
Dim fileSaveName As Variant, pic As Variant
'Create temporary chart as canvas
Set sht = Selection.Worksheet
Run ("UnprotectWorkbook")
Run ("unprotectSheet")
Run ("Selection")
Range("B1:H17").Copy
Selection.Copy
sht.Pictures.Paste.Select
Set sh = sht.Shapes(sht.Shapes.Count)
Set tmpChart = Charts.Add
tmpChart.ChartArea.Clear
tmpChart.Name = "PicChart" & (Rnd() * 10000)
Set tmpChart = tmpChart.Location(Where:=xlLocationAsObject, Name:=sht.Name)
tmpChart.ChartArea.Width = sh.Width
tmpChart.ChartArea.Height = sh.Height
tmpChart.Parent.Border.LineStyle = 0
'Paste range as image to chart
sh.Copy
tmpChart.ChartArea.Select
tmpChart.Paste
'Save chart image to file
fileSaveName = "N:\Continuous Improvements\Gary\Image_Save\Slide - " & ActiveSheet.Name & ".jpg" 'Application.GetSaveAsFilename(fileFilter:="Image (*.jpg), *.jpg")
If fileSaveName <> False Then
tmpChart.Export Filename:=fileSaveName, FilterName:="jpg"
End If
'Clean up
sht.Cells(1, 1).Activate
sht.ChartObjects(sht.ChartObjects.Count).Delete
sh.Delete

Run ("ProtectSheet")
End Sub
 
Upvote 0
Even this doesn't work anymore:

Sub CentreAcrossSelection()


With Selection
.HorizontalAlignment = xlCenterAcrossSelection
.VerticalAlignment = xlCenter
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
End Sub
 
Upvote 0
Do you have any Subs/Functions or variables called "Selection"
 
Upvote 0
Mmm I’ll take a look and report back in the morning.
I may have inadvertently created one!
 
Upvote 0
What is Selection meant to refer to here?
Code:
Set sht = Selection.Worksheet
 
Upvote 0
Glad it's sorted & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,214,975
Messages
6,122,537
Members
449,088
Latest member
RandomExceller01

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