How to select all names in Excel with VBA - excel

I am working with a large Excel file, where I have defined around 2000 names for cells. In addition the excel contains formulas, these formulas are already entered with references to cells (A23-B24). I want to change the cells references by the defined names (instead of A23-B24 having VARIABLE_100-VARIABLE_120).
I know this is possible by selecting "Apply names" and then select the defined names from the list. Because I have around 2000 defined names, I would like to select all the names at once from the menu, but I cannot find an option, so I have to select one by one. I have been looking if there was an option for enabling the multiple selection on the menu, but I have not found such an option.
A work around for me would be to create a macro that applies to the selected range of formulas the selected names. Something like this:
Sub Macro1()
' Macro1 Macro
Selection.ApplyNames Names:=Array ("ATL_BV_XP", "ATL_BV_XP..EUN", "ATL_PK_XP", _
"ATL_PK_XP..EUN", "CHN_PK_IM", "CHN_PK_IM..EUN", "CHN_PK_IM..SHREUN", _
"E15_AG_AH", "E15_AG_EPA", "E15_AG_SFP", "E15_AG_SFP..CF" _
, "E15_APF_FE"), IgnoreRelativeAbsolute:=True, UseRowColumnNames:=True, _
OmitColumn:=True, OmitRow:=True, Order:=1, AppendLast:=False
End Sub
My problem is that because I have a large number of defined names (already wrote like twice that around 2000), listing all names in the macro code becomes really complex. I thought that probably a workaround would be to create a list including all defined names within the code. Does someone has an idea how to do this? I have looking around and some codes suggest a loop while other say it is possible to extract a list. Nonetheless I have not been able to find a way to do it.

The documentation at msdn.microsoft.com says
Names: An array of the names to be applied. If this argument is omitted, all names on the sheet are applied to the range.
see https://msdn.microsoft.com/en-us/library/office/ff196578(v=office.15).aspx
Following on that try this:
Selection.ApplyNames _
IgnoreRelativeAbsolute:=True, UseRowColumnNames:=True, _
OmitColumn:=True, OmitRow:=True, Order:=1, AppendLast:=False
This should work depending on the Scope of the Defined Names and their visibility. Try that and let us know the results.

It may already be available to you. Say we have defined the Names Cost, Profit, Tax
When typing a formula, have the Formula Tab open and you can always pull-down those names:
A macro would be nice, but I don't know how to invoke a macro in the middle of editing.

Related

Do not ignore excel hidden field VBA

I am trying to use one of the excel hidden field for the purpose of referencing. Basically column(A:A) is hidden and it contains specific IDs that I can use it to reference it to another sheet.
I could have moved the column (A:A) further away so that the user does not see it, but my issue is that I have written too many lines of code already. I guess it is poorly constructed, because if I were to move any of my columns, my entire program would definitely break. I could try to fix it, but that would mean I would have to over analyze my own code and I either wouldn't understand it or wouldn't find my mistake.
So, anyways, I have a Range.Find function, which is looking in the hidden field, but returns nothing. I could try to unhide it, and hide it again, but I want to know that if there is a solution in Excel, then to not ignore the hidden field.
Set myCell = Columns(1).Find("search_string", lookat:=xlWhole, LookIn:=xlFormulas)
Debug.Print myCell.Row
Replace "search_string" to the ID you are looking for.

Can't use more than one combo box on excel

I have made two active X combo boxes for a list of 220 sites. Before making the active x combo box i have made a sheet that searches through my data and finds the sites that match with what i am typing as i go.
I then used the name manage, refering to the formula in the first cell of the list
=Sheet1!$G$2:INDEX(Sheet1!$G$2:$G$220,COUNTIF(Sheet1!$G$2:$G$220,"?*"))
I have then writen this in the ListFillRange in the properties of my combo box.
It works fine for one, but once i had made the second one and selected the site the first one will no longer let me search through.
I have used the same formulas on both but they originate from different sheets to see if this fixed the problem however that was unsuccessful. (the boxes are on different sheets) When i click on the next sheet after using the box on the first sheet, it still shows part of it as if it is crashing.
The boxes are independent so I'm not sure what to do next as i need to add another 3 on separate sheets.
I am also using this code for each box
Private Sub ComboBox1_Change()
ComboBox1.ListFillRange = "MList"
Me.ComboBox1.DropDown
End Sub
and similar for the other button but with a different range.
There is no need to use VBA for this, the Change Events specifically, if you just want to use and fill the combo boxes with Named Ranges.
The scenario I think you try to do is:
Create Named Ranges that will be the source of your combobox:
Fill the range with your data, select the range, Right Click, Select Define Name and give the range a name. MList in your case I believe.
Create Combobox:
Goto Developer Tab, Insert in your case ActiveX ComboBox, Draw it on your sheet, right click the ComboBox, select properties, find ListFillRange in properties and enter the name of the Named Range you created in step one
Repeat for Combobox 2, with the same or a different Named Range depending on what you try to do
Leave Design Mode
Boths Comboboxes can now be used to type in what you are looking for.
If this is not what you tried to do, please try edit your question and in detail try to explain what you try to do and what you like to accomplish by doing so.

Freezing certain cells - VBA

I have a question about freezing certain cells. But first let me explain the situation.
I have made a search box in my excel sheet and when you search for a letter or word; the results show up in cells below the search box. Now I want to freeze those cells, so that wherever I go in my sheet. I can always use the search box and see the results.
The cells for the searchbox and results are B2:B25. Those are the ones I want to freeze. Also the only sheet I want to use this on is the sheet "Reading". On the rest of my sheets I do not use a search function.
So my question(s) is : Do I need to put the code inside a module or on that sheet, and how do I do this?
Now I have tried the following
Range(Cells(2,2), Cells(25, 2)).Select
ActiveWindow(or maybe Reading?).FreezePanes = True
Inside a module. But it did not work and I do not know what else to do.
Any help is much appreciated! Since I am very new to VBA.
Almost there. Problem is that the 'range' and 'cells' needs to be directed to the 'Reading' sheet, like so:
Sheets("Reading").Range(Sheets("Reading").Cells(2,2), Sheets("Reading").Cells(25, 2)).Select
ActiveWindow.FreezePanes = True
but if it always is B2->B25, why not use:
Sheets("Reading").Range("B2:B25").Select
ActiveWindow.FreezePanes = True
This should work. Select is not very desirable, because it is very slow, but in this case, you need to (as far as I know).
EDIT
BTW, you can do this from within a code module or from within a sheet, but if you choose to do it from within a sheet, you cannot select another sheet. So just use the range.
EDIT 2
whoopsy, typo corrected. 'Sheet' should have been 'Sheets'

Define a custom named list in Excel to be used for data-validation

I am trying to set up a drop-down menu in Excel using a named list, but I would like to write the entries for the named variable myself.
So, say I define a named list (call it TestVar) as (using the Name manager)
={"A","B"}
(alternatively with a semi-colon to create a column), I cannot use it for data validation. So if I under data validation choose the "list" option, and write
=TestVar
as the source, I get the "The Source currently evaluates to an error." message.
I don't understand why TestVar isnt't a valid list, as it works with both the INDEX-function, as well as array formulas.
Also, I know that I could write
A,B
as the source when setting up my data-validation, and that gives me the behaviour I want, but I think it makes more sense to have it defined as a named list, in case one needs to append to the list at some point in the future, and the list is referenced multiple times in the document.
Edit:
Forgot to mention that I use Excel 2013.
If you record a macro you'll see that the list is just a string of values separated by commas. This will do what you want:
Sub Macro1()
Dim ValidationFormula As String
ValidationFormula = "1,2"
With Selection.Validation
.Delete
.Add Type:=xlValidateList, Formula1:=ValidationFormula
End With
End Sub
EDIT:
To set a Data Validation's source to an Excel Name, that Name needs to point to a Named Range. I don't know of a way to have it just refer to a name that's set up like "1,2 or =1,2.
Below I defined a named range ValidationList that refers to C2:C5. Then in A2 I opened the Data Validation dialog and pointed to that named range:
Note that if you do it this way, you'll want it to be a dynamic named range. Also see this Chandoo post.

Dynamically Populate Listbox - Exclude Empty cells

I am creating a form in excel (not a userform) and I am populating the listbox using cells. However, these cells are sometimes A1:10 and sometimes they are A1:A4. Is there a way to dynamically change what is shown in the listbox?
Right now, when I use A1:10 and there are only 4 cells populated, I get the list of 4 populated cells followed by 6 blank entries. I'd like to get rid of the 6 blanks when there are only 4.
Unfortunately, this is more of a workaround than a solution. However, it may be able to do what you need it to do. I've been hitting the same wall you are with trying to make ranges dynamic.
Without seeing some code to know exactly what you're doing, try something like this.
Private Sub ListBox1()
x = 1
'Add items to listbox until you reach an empty cell.
Do while Cells(x,1) <> ""
ListBox1.AddItem Cells(x,1)
Loop
I'm not very familiar with listboxes outside of userforms but this should do approximately what you want to do.
Edit your original post with your code so we can try and get a better understanding of what you've tried and what you're trying to do.
You can create a named range using a dynamic formula. Either of the below formulas will work.
=A1:INDEX($A$1:$A$10,MATCH("",$A$1:$A$10,0)-1)
=OFFSET($J$7,0,0,MATCH("",$J$7:$J$32,0)-1)
To create the named range, click ctrl+F3 then click new, insert one of the two options above into the "refers to:" section, then label the new range whatever you would like. in the "row source" section of the listbox simply type in the name you selected for your new named range.

Resources