Excel VBA - Programmatically change chart's source? - vba

I've got this Excel document which contains several charts which target Named Ranges that are all predefined. The problem is, the reference the chart needs have to include the name of the document for some reason. I've managed to add the filename of the document in the sheet. But I cannot target this cell directly when assigning the source of the chart.
So, when I edit the chart and select the source, it looks like this:
='MyExcelDocument.xlsm'!Graphs_TotCost
For some reason Excel doesn't understand when I target the cell that contains the filename. So I'll probably have to do this via a macro by first defining a string variable, assigning the document name to that string. And then assign the completed string combined with Graphs_TotCost. So, how do I select a chart that's in Sheet3 and change the source of that chart using a macro?

You can change the source of a graph with this kind of statement:
Charts("Chart1").ChartWizard Source:="Graphs_TotCost"
You could also probably use the SetSourceData method of the Chart object.

Related

Add Chart Inside a Textbox using VBA in word

I want to add a chart inside a textbox using VBA in Microsoft word.
I have tried using
ActiveDocument.InlineShapes.AddChart()
API;
with cursor inside the textbox and still it doesn't produce the desired result
Unless you specify where the chart is to be placed it will simply be added to the document. If you had looked at the help text, IntelliSense or the Object Browser you would have seen that the method takes a Range as an argument. To place the chart in a specific location the range is required.
For example:
Set shp = ActiveDocument.InlineShapes.AddChart(Range:=Selection.Range)

Preventing a Defined Name Range from changing

I'm currently setting up a workbook that utilizes command buttons to import information, chart it, and open a new tab with a specified name. In the initial "Data" tab, I have a named range using the following code:
=OFFSET(**Data**!$B$2,0,0,COUNTA(**Data**!$B:$B)-1)
When the new tab is created, it renames the original "Data" sheet and creates a new one with the original name. My question is how do I prevent the bolded name from changing in the defined name? Is this possible and if not--is there a VBA code that would copy this and force me to give it a name?
Thanks for the help in advance!
As I mentioned in the comments, either you can make a copy of the Data sheet and then rename it. This will work because when you rename the sheet the data reference gets updated automatically where as if you change the name in the copy it wont formulas referencing the copied sheet will get updated.
Else you can add this code to the end of your macro which will recreate the named range
ThisWorkbook.Names("abc").Delete
ThisWorkbook.Names.Add Name:="abc", RefersTo:="=OFFSET(Data!$B$2,0,0,COUNTA(Data!$B:$B)-1)"

Excel VBA - Pull information into user form to update

I am trying to create a userform that allows the users to update issues stored in a specific sheet (called Issues List). I have built a dropdown list using data validation that allows the user to select the unique issue name from a list. I have created a button next to that dropdown which opens up the userform and correctly imports the issue name identified from the dropdown.
What I need to figure out is, when the user form is initiated how do I have it search column B in my Issues List sheet and identify which row contains the issue selected by the user, and populate the fields of the user form with the information found in rows C-X of the Issues List sheet.
What I have been trying to use is an index match function, but have been unsuccessful in getting the code to work. An example of what I have been using is:
Resolved.Value = Application.WorksheetFunction.index
('Issue List'!$X$2:$X$1000,Application.WorksheetFunction.match
('Priority Table'!I35,'Issue List'!$B$2:$B$1000,0))
Any help would be greatly appreciated.
Thanks in advance!
When you use Worksheet Functions in VBA, you still have to pass in the ranges using VBA language:
So instead of:
'Issue List'!$X$2:$X$1000
you would use:
Worksheets("Issue List").Range("X2:X1000")
And instead of:
'Priority Table'!I35
Just use:
Worksheets("Priority Table").Range("I35")
Note that you can also refer to ranges by names, which can make coding easier and also far safer. When you insert rows in spreadsheets, Excel doesn't automatically update ranges in any VBA code. A reference to I35 will always to be I35.
Instead, define a name for cell I35 in Excel as normal, then refer to it in the code.
For example, if you name I35 as "Issue"
You can refer to the cell by:
Range("Issue")
(If it is a global variable, which it is be default as long as it's a unique name in the workbook, you don't need to use the Sheets("Priority Table") qualifier.
Refer to this documentation for more info on how to refer to ranges in Excel from VBA:
https://msdn.microsoft.com/en-us/library/office/gg192736(v=office.14).aspx

Pasting an Excel chart into a Word document so it is editable but not linked

I'm using VBA to create a series of charts in Excel and then copying them into a Word file.
Up till now I've been pasting the charts as pictures, so in Excel I used
ActiveChart.CopyPicture
and then in Word, after selecting the target location:Selection.Paste.
Now I want to change it so the charts will be editable but not linked to the source Excel file.
I copy a chart from Excel using ActiveChart.ChartArea.Copyand look at the paste-special options in Word, the options "use destination theme/keep source formatting & embed workbook" work fine for me:
the chart is editable (also the data is editable which I don't need but is OK) and there is no link to the original Excel file.
BUT - I can't find how to perform this through VBA code. Trying to record this in a macro only give me Selection.Paste - which pastes a linked chart.
I also tried a different approach - pasting a linked chart, and then killing the link. once again, deleting the links in the link editor doesn't get recorded in the macro at all.
Please help with coding any of these two options or suggesting a different approach.
The Range.PasteAndFormat method should work. This takes a WdRecoveryType Enum parameter that lets you specify what kind of result you want.
Selection.PasteAndFormat(wdChart) 'Enum value 14 in case of late binding

change link file path in excel

I would like to know if it is possible to change the file path of a link in excel, using text in another cell. The trick here is that the source workbook is not open at the same time as the one that has the link in it, and is not located in the same folder.
ie: if once cell has a link like:
='C:\thanks\forthehelp\01\[eg.xlsx]worksheet'!A1
and I want to change the file based on text input from a cell, ie: cell A1 has some text: "02"
='C:\thanks\forthehelp\&A1&\[eg.xlsx]worksheet'!A1
to get
='C:\thanks\forthehelp\02\[eg.xlsx]worksheet'!A1
I know the above example doesn't work, but i think it illustrates what i want to do here. Any help would be really appreciated. I hope this makes sense.
Thanks.
You could use the INDIRECT function to implement a constucted string to the destination workbook but INDIRECT does not work on closed workbooks.
A VBA routine could replace the formula in the cell.
Range("A1").Formula = Replace(Range("A1").Formula, "\01\", "\02\")
If you opt for this sort of formula replacement, you should check to ensure that the external workbook exists in the new location with something akin to the Dir function. Using the Range.SpecialCells method with the xlCellTypeFormulas property would speed up going through the cells as opposed to looping through all of the cells and determining if the Range.HasFormula property is true.