Preventing a Defined Name Range from changing - vba

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)"

Related

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

Importing data into a master worksheet

I have a doubt about my program to the VBA/EXCEL
I made a file called "MASTER WORKSHEET" with various data from a client, my goal is: To create a VBA to open any file in Excel and that I import chosen cells to fill out all data of the "MASTER WORKSHEET", for example, all excel file I open with the button "IMPORT DATA", it will automatically capture only those cells that I choose, but my program it captures but is with configuration problems on VALUE or NUMBER.
If your question is simply "how do I copy a cell from one place to another", an example would be:
Workbooks("Input Workbook").Worksheets("Input Worksheet").Range("J1").Copy(Workbooks("Master Workbook").Worksheets("Master Worksheet").Range("A4"))
If you just want to copy the value, without the formula and formatting, you could use
Workbooks("Master Workbook").Worksheets("Master Worksheet").Range("A4").Value = Workbooks("Input Workbook").Worksheets("Input Worksheet").Range("J1").Value
Obviously, the workbook, sheet, and range names need to be changed to your particular situation.

Excel Reference External Sheet

An excel question for you gurus. I've tried searching high and low and haven't come up with an effective solution.
I'm trying to create a formula that will lookup a value in an external sheet. I'm using the SUMPRODUCT formula and it works perfectly. Formula is below:
=SUMPRODUCT(--('File\Path\[file.xlsx]SheetName!$D$1:$D$1000=$B3), --('File\Path\[file.xlsx]SheetName'!$O$1:$O$1000=$A3), 'File\Path\[file.xlsx]SheetName'!$Q$1:$Q$1000)
The issue I'm running into, however, is that the source file is updated every day. Although the workbook name stays the same, the sheet name changes. A random string gets assigned to the source sheet name each time it is updated. As such SheetName becomes SheetName ase341.
Is there a way to have the formula read the external sheet number instead of the name? I want the formula to update regardless of the sheet name. If there's no way to read the sheet position is there a way to change the sheet name via a formula in an external workbook?
Usage Example
I have a workbook (analysis) and it pulls data from another workbook (source). Source is updated every day with new data. The data in Source is updated by downloading a report from the internet and saving over the old source file. As such, the file name stays the same but whatever is inside the file is always different (including the sheet name). There is always only ever one sheet in the Source with the same number of columns, always in the same position.
There is a really neat way to refer to a block of cells in an external workbook in which the sheetname or even the block address may vary. Say we have:
=SUM('C:\Users\James\Desktop\[Book1.xlsx]Sheet1'!$B$2:$B$9)
however the sheetname may vary. First assign a Defined Name to the block in Book1 (say XXX)
Then we can use:
=SUM('C:\Users\James\Desktop\Book1.xlsx'!XXX)
It does not matter if the sheetname changes, the Defined Name will change with it!
Your issue would be most efficiently solved with VBA, but if you're just getting started this might not be the best route.
You can get the sheetname or filename with just a formula, though:
http://www.ozgrid.com/VBA/return-sheet-name.htm

Is it possible to name a range on a sheet that does not exist yet?

I am trying to create a named range for a worksheet that does not yet exist in the workbook. I will be using a macro to import a user-defined text file as the source.
Am I restricted to only defining these named ranges once the worksheet has been imported, or can I name a future range on a sheet that does not exist yet?
In Refers to:
=INDIRECT("NotYetASheet!$A$1")
I'm assuming the names will be created upon import of the data. I'm also assuming it's ok if your unnamed ranges return errors until the data has been imported. You can manage this using the IFERR or IFERROR functions.
Enter the following in a cell of a new worksheet:
=notyetnamed
Then select another cell, enter any old value, and give it the name notyetnamed.
I hope this answers your question. If not, you're welcome to clarify and I'll do my best to help.

Excel VBA - Programmatically change chart's source?

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.