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
Related
I want to be able to control and update the source (range) of an excel chart through VBA. The chart is ultimately going to be presented in a powerpoint presentation and the whole process shall update automatically.
I have tried using tables which doesn't give me the level of control I want. For example if i delete one datapoint the chart doesn't "shrink" accordingly.
ActiveChart.SetSourceData (Sheets("Sheet2").Range("E5:E9"))
The current script doesn't run. But I just want to "set" a source range for a particular chart, preferably accessed by name. And if new datapoints are added I just run the script again and the new range will be the top cell in the column down to .End(xlDown)
Thanks guys! I manged to find a solution that works for me and made a function of it, if anyone else has the same problem. Nothing fancy but you can set a range like "A2:A14" and the chart will update with the new range. The function is called by sheet name, chart name and range like below:
Call changecharts("Sheet2", "Chart1", "A2:A11")
Public Function changecharts(sheeet As String,chartname As String, rnge As String)
Worksheets(sheeet).ChartObjects(chartname).Activate
With ActivateChart
ActiveChart.SetSourceData Source:=ActiveWorkbook.Sheets(sheeet).Range(rnge)
End With
End Function
I am trying to look up some data in an excel sheet to populate a field in a Lotus Notes app on demand. I am using an Action button with LotusScript like so:
Dim v As String
Dim v2 As String
'Open XL sheet for access to data
Set oExcel = CreateObject ( "Excel.Application" )
v="\\msp2\mi\CSD\Confidential\IT and PPST budget.xlsm"
Msgbox("opening " & v)
Set oWorkbook = oExcel.workbooks.open(v)
Set oWorkSheet= oWorkbook.worksheets (4)
v2=Cstr(oWorkSheet.Cells(1,1).value)
Messagebox(v2)
This code does work in that it pulls data from cell A1 - but which sheet?
The sheet containing the data I want is "Sheet4" renamed as "Logic-Detail" but if I use 4 as a parameter as above I get data from the 4th sheet from the left. I need to be able to cope with sheets being hidden as well. I spent 20 minutes on MSDN's excel object model "help" getting nowhere :-(
I feel sure it must be dead easy when you know the answer !
Guessing at the correct syntax is frustrating, isn't it? When referring to sheets in Excel-VBA there are several options:
You can list them by index-number, as you already did in the code-sample in your question
You can refer to it by its name, in your case this would probably be oWorkbook.Worksheets("Logic-Detail")
You can refer to it by its codename, in your case this would probably be oWorkbook.Sheet4. The codename can be changed when you view the properties of the worksheet in the VBA editor.
There may be even more ways to refer to the sheet, but these are the ones which come to mind at the moment. As we know from the question and comments, at least the two first options also work in LotusScript.
I am new to excel vba and I have some questions regarding referencing a worksheet
I noticed that when I used
Worksheets(3)
The worksheet would be obtained according to the sequence of the worksheet in the workbook
When I used
Worksheets("Name")
It would be retrieved according to the name of the worksheet
However, I found that both approach is troublesome because for method 1, I need to fix the sequence of the worksheet. Once I dragged the worksheet around, the reference would become incorrect.
Method 2 would need me to fix the work sheet name , which is not that flexible.
I noticed that at the left panel of VBA editor, under the Microsoft Excel Objects, whenever the worksheet is created, a new sheet like
Sheet1 (Name) would be created.
Is there any way that I could reference the worksheet based the the Sheet1 variable, which I could fixed it so that I could freely drag the sheet around or change the worksheet name?
Thanks.
The name you refer to is called the CodeName. You can refer to a sheet by this name.
Eg, for your example Sheet1 (Name) can be referenced as
Worksheets("name")
or
Sheet1
Eg Worksheets("name").Activate or Sheet1.Active would both work
Note that you can change this name to something meaningful in the Properties window of the VBA IDE, but you can't change it at run time
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.
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.