VBA Excel Range method - excel

I need to move a row of data to another worksheet when a user chooses a value in a listbox. I am trying to use the following code to select the row on the new worksheet I want to move the data to:
'Move data to the "Red" worksheet
Sheets("Red").Range ("A11").Select
I am getting an "Object does not support this property or method" error.
What am I doing wrong?

There should NOT be a space between "Range" and "(".

Is selecting allowed on your sheet? (= selection not prohibited by protection)
I have noticed a space character between the word Range and ("A11"). This might have caused a syntax error.

Use the macro recorder and manually perform the operation you want. Then you can look at the code recorded by Excel and clean it up to suit your needs.
The macro recorder is the easiest way to figure out whatever convoluted syntax VBA wants you to use.

Related

Excel VBA refer to worksheet that uses icon/emoji as part of name

I'm using Excel for Office 365 MSO 64-bit.
I want to write a VBA macro that selects different worksheets in a workbook based on the worksheet's name.
For example, I have two lines of VBA code that activate a workbook and then select a specific sheet in the workbook by the sheet's name.
Windows("myworkbook").Activate
Sheets("mysheet").Select
However, I have to work with some sheets that contain icons or emojis in them. For example, there is a worksheet that has this name: "🚑 Patient".
If I try to paste the icon/emoji into VBA like this: Sheets("🚑 Patient").Select, the icon does not show up in the VBA editor. Instead, I get Sheets("????? Patient").select.
I have also tried to use ChrW() to encode? the ambulance character (see here: https://www.compart.com/en/unicode/U+1F691)
When I run this macro below), I get an invalid procedure call or argument as noted below.
Sub SelectWeirdSheet()
Windows("MYWorkbook.xlsx").Activate
x = ChrW(128657) ' get invalid procedure call or argument here
Sheets(x & " Patient").Activate
End Sub
I also tried code for ambulance... also tried ChrW(&H1F691), but I get the same error.
My suspicion is that I am using the wrong argument for ChrW(), but I'm lost.
edit: So, the docs say that my argument for ChrW() is out of range. That helps explain the error, but I'm still missing a work-around.
Question: Is there a way to refer to use VBA to select worksheets that have an icon/emoji as part of their name?
I know you can also refer to worksheets by index number like this Sheets(3).Select.
However, there will be instances where I don't know the index of the sheet ahead of time, but I will know the name of the sheet, so it is preferable for me to call the worksheets by name.
Thank you.
In addition to the self-answered response, when working in a single workbook, the coder can assign a CodeName to the sheet in the VBA IDE, and then use that CodeName directly. This is really only valid if the Sheet is not re-created (i.e. is a permanent sheet in the book) at any stage, because a new/copied sheet will be automatically given a new CodeName by Excel.
For example, if given the CodeName shtPatient (see picture bellow), the code could be:
Sub SelectWeirdSheet()
' Windows("MYWorkbook.xlsx").Activate '<-- this approach has limitations
shtPatient.Activate ' See my comment below about the limitation - this will not work as expected in this example.
End Sub
Note: https://stackoverflow.com/a/10718179/9101981 explains why not to use Activate, but I have left the code as-is for the purposes of this answer. Also look at Using Worksheet CodeName and Avoiding .Select & .Activate. Another limitation noted is that the CodeName is only valid for the workbook that the code is in - so may not be applicable in this case.
I have highlighted the CodeName parts of the IDE in the image below, see how "Test Patient" is not called "Sheet7", but instead has a meaningful name that I gave it in the properties window below.
In order to properly address the emoji, it should be split into two separate unicode characters.
In this case, it would be x = ChrW(&HD83D) & ChrW(&HDE91)
Those two unicode characters make up the ambulance emoji.
So, this Macro now works.
Sub SelectWeirdSheet()
Windows("MYWorkbook.xlsx").Activate
x = ChrW(&HD83D) & ChrW(&HDE91)
Sheets(x & " Patient").Activate
End Sub
Found the solution on reddit of all places https://www.reddit.com/r/excel/comments/6pq1r1/vba_how_can_i_write_emojis_using_chrw/

Excel VBA - pasted object into Powerpoint loses selection for manipulation

Just to let you all know, I'm a beginner to VBA and I'm using Excel and PowerPoint 2013.
The aim: I'm writing code in Excel VBA to paste a chart from Excel into PowerPoint. Then when it's pasted, move it to a set place within the slide.
The code I used: The framework of the code I started with is in http://peltiertech.com/Excel/XL_PPT.html#chartppt, the part called 'Paste the Active Excel Chart into the Active PowerPoint Slide (Early Binding)'.
The main difference is around the 'Paste' part, this is instead a 'PasteSpecial' and my code is as follows:
.
Charty.CopyPicture
ppApp.ActivePresentation.Slides(SlideNumber).Shapes.PasteSpecial (ppPasteEnhancedMetafile)
' Setting the correct position on the slide
ppApp.ActiveWindow.Selection.ShapeRange.Top = ChartTop
ppApp.ActiveWindow.Selection.ShapeRange.Left = ChartLeft
(Note. ChartTop and ChartLeft have been defined earlier)
The issues: I'm aware that after the PasteSpecial it doesn't have a select statement. However, (1) When I run the above code I get a runtime error '-2147188160' saying the Selection.ShapeRange is an invalid request (in the 'ChartTop' line), ie. it isn't selected so it can't be moved. (2) When I add a select statement to the 'Paste.Special' line I get an 'Object required' error on the new 'PasteSpecial' line, ie. it doesn't recognise it as a paste select statement.
The odd part: When I run the same macro on other people's computers using the exact same Excel and PowerPoint files it works (without the select statement in the 'PasteSpecial' line). I've looked at 'Tools' => 'References', and both of our computer have the same settings.
The questions: Is there a setting that I have overlooked on my computer which prevents the code from working? Is there a way after I've pasted it to reselect it before it is moved (I should tell you that the macro is to paste in many graphs into ppt simultaneously)? How come for other people at my work it works on their computers without the 'select' part of the PasteSpecial statement? How come when I use the Shapes.PasteSpecial (ppPasteEnhancedMetafile).Select statement, it doesn't recognise it as an object?
I hope that's clear.
Thanks in advance.
Dim oSh as Shape
Set oSh = ppApp.ActivePresentation.Slides(SlideNumber).Shapes.PasteSpecial (ppPasteEnhancedMetafile)(1)
Now you don't need to select anything (which is best avoided whenever possible). Just manipulate the pasted shape, oSh, directly.
The curious syntax (the (1) at the end) is because Shapes.Paste or .PasteSpecial returns a shaperange, not a shape; the (1) causes it to assign the first shape in the range to the oSh variable. Since there's only one shape in the range, that suits us just fine.

How can I copy a sheet and have my macros still work?

First off I want to apologize if my question has already been answered elsewhere as I did spend some time searching.
I'm using two macros in a sheet (very simple macros... they filter a table) and this sheet is a template that will be used every week. When I copy the sheet and make a new one, the macros no longer work. The error I receive is Runtime Error 9; Subscript out of range. I looked at the VBA code (which I've never really learned VBA) and see it is referencing a table title. Is there a way to fix this so it doesn't reference that specific table but rather the cells contained in that table? Example of the title name:
ActiveSheet.ListObjects("Table1619").Range.AutoFilter Field:=1
I want to access the data from a cell range of A103:A113. I tried this:
ActiveSheet.ListObjects.Range("$A$103:$A$113").AutoFilter Field:=1
This didn't work either but the error I received was different. "Run-time error '438': Object doesn't support this property or method"
What I know would work is if there was a way to have the same table name across all sheets but my limited research has seemed to point me in the direction of this not being possible.
It's impossible to reference ActiveSheet.ListObjects.Range("$A$103:$A$113") like this - either as above in your first sample, OR convert table to normal range and then use ActiveSheet.Range("$A$103:$A$113").
The problem you're facing is that you access the Range through a Table/ListObject - but the ListObject changes the name during the copy, as each Table must have a unique name. The solution is simple - instead of accessing the ListObject with its name, simply use the index in the worksheet - that will not change. Therefore, replace
ActiveSheet.ListObjects("Table1619").Range.AutoFilter Field:=1
with
ActiveSheet.ListObjects(1).Range.AutoFilter Field:=1
(assuming it is the only/first table in the worksheet.)

Microsoft Excel: Programmatically create a hidden named range

I am told that the Excel object model permits a Range that is not a part of any sheet, yet contains a set of cells and is denoted by a name in the workbook.
Can anyone explain to me how these fit into the Excel object model and how one would go about creating such a thing programatically (either in VBA or .NET source code).
Thanks.
Your question is a little vague, but I'll give it a shot.
Well, as Dave describes, you can give a specific range of cells on a sheet a "Range Name" which you can then refer to programatically, but that doesn't sound like what you are asking.
It sounds like you are asking "is there an abstract RANGE of cells available to be used by VBA code that doesn't literally exist on any worksheet?" The answer to this is no, even named ranges are simply a convenient reference to a real set of cells on a real worksheet.
You can, however, programatically hide a worksheet so that the user doesn't see it, and still work with cells and ranges on that sheet. Just do:
Sheets("Sheet1").Visible = xlSheetHidden
Sheets("Sheet2").Visible = xlSheetVeryHidden
Sheets("Sheet3").Visible = xlSheetVisible
What's "VeryHidden", you ask?
It means that the user can't go to Format, Sheet, Unhide and make the sheet visible.
So if I'm correctly understanding what you want, just programatically hide one of the sheets, then use Dave's technique to create a named reference to a range on this hidden (or VeryHidden) sheet.
That would be a named range. You can reference a selection of cells, and just type a name where it says 'A1' next to the formula bar. That creates a named range that doesn't change.
Alternatively you can create a named range that is based on a formula, and therefore potentially changes as data in the spreadsheet changes. You do this from the 'Define Name' option (which is in the Formulas Ribbon in Office 2010).
Named ranges can be accessed from VBA, and (I'm pretty sure) from .net.
So you'd access the named range from vba like so:
Range["MyNamedRange"]
Yes, there is a NamedRange control in the Microsoft.Office.Tools.Excel namespace in VSTO. This is a host control which is different from the native Range control in the Microsoft.Office.Interop.Excel.Range namespace.

How to insert an excel chart into Word using AddOLEObject

I'm trying to create a linked OLE Object in a Word document using VB.Net.
Existing code uses InlineShapes.AddOLEObject(FileName:="abc.xlsx", LinkToFile:=True, Range:=Some Word Range) to insert a worksheet into a Word document.
I need more control than this. To select a range of cells I've found that extra information after the filename can be useful, for example: FileName:="abc.xlsx!sheet1!R1C1:R20C5"
Is there a way to specify a specific chart within a worksheet? So can I specify the second chart on the worksheet as the object to link to?
Thanks.
Thanks for your help Mark.
I eventually figured out that if the Chart is in it's own sheet, rather than an object in Sheet1, then the AddOLEObject code works correctly with the following setting:
FileName:="abc.xlsx!Chart1"
I'm happy with this solution.
A chart will either be a whole worksheet so address as per your sheet1 e.g. abc.xlsx!sheet1 or an object on a sheet so use the object name e.g. abc.xlsx!sheet1!chart_object

Resources