Exclude cells containing "_" from range given to dropdown list

Jsmithachoo29665

New Member
Joined
May 7, 2018
Messages
3
Hi! Here's an easy one for you guys!
I have this table row of which I want a range including only cells without the underscore char.
The underscores appear in no particular order.

Example:
Apple|Orange|Apple_Nutrients|Orange_Nutrients|Banana|Banana_Nutrients|Banana_Count

Desired range: Apple|Orange|Banana

Thanks a bunch!
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
Hi Aladin, thanks for replying!

You're right I wasn't clear enough.
The source is a table row so "|" means another cell/column

I'm trying to use the desired range in a dropdown list via data validation.

So the Desired range is : {Apple,Orange,Banana}
 
Upvote 0
Sheet1

[Table="width:, class:grid"][tr][td]Row\Col[/td][td]
A​
[/td][td]
B​
[/td][/tr][tr][td]
1​
[/td][td][/td][td]
3​
[/td][/tr]
[tr][td]
2​
[/td][td]Apple[/td][td]Apple[/td][/tr]
[tr][td]
3​
[/td][td]Orange[/td][td]Orange[/td][/tr]
[tr][td]
4​
[/td][td]Apple_Nutrients[/td][td]Banana[/td][/tr]
[tr][td]
5​
[/td][td]Orange_Nutrients[/td][td][/td][/tr]
[tr][td]
6​
[/td][td]Banana[/td][td][/td][/tr]
[tr][td]
7​
[/td][td]Banana_Nutrients[/td][td][/td][/tr]
[tr][td]
8​
[/td][td]Banana_Count[/td][td][/td][/tr]
[/table]


In B1 just enter:
Rich (BB code):
=COUNTIFS(A2:A8,"?*")-COUNTIFS(A2:A8,"*_*")

Name B1 Size via the Name Box.

In B2 control+shift+enter, not just enter, and copy down:
Rich (BB code):
=IF(ROWS($B$2:B2)>Size,"",INDEX($A$2:$A$8,SMALL(IF(ISERROR(FIND("_",$A$2:$A$8)),ROW($A$2:$A$8)-ROW($A$2)+1),ROWS($B$2:B2))))

Next, define DVlist in Formulas | Name Manager as referring to:
Rich (BB code):
=OFFSET($B$2,0,0,Size)

Now you can use DVlist to data validate a cell where it is needed.
 
Upvote 0
Sheet1

[TABLE="class: grid"]
<tbody>[TR]
[TD]Row\Col[/TD]
[TD]
A​
[/TD]
[TD]
B​
[/TD]
[/TR]
[TR]
[TD]
1​
[/TD]
[TD][/TD]
[TD]
3​
[/TD]
[/TR]
[TR]
[TD]
2​
[/TD]
[TD]Apple[/TD]
[TD]Apple[/TD]
[/TR]
[TR]
[TD]
3​
[/TD]
[TD]Orange[/TD]
[TD]Orange[/TD]
[/TR]
[TR]
[TD]
4​
[/TD]
[TD]Apple_Nutrients[/TD]
[TD]Banana[/TD]
[/TR]
[TR]
[TD]
5​
[/TD]
[TD]Orange_Nutrients[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]
6​
[/TD]
[TD]Banana[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]
7​
[/TD]
[TD]Banana_Nutrients[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]
8​
[/TD]
[TD]Banana_Count[/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]
The OP said "The source is a table row so "|" means another cell/column"
 
Upvote 0
Here is a VBA macro solution. First, though, you were not exactly clear where your row of values were located at, so I assumed they started at cell C3. Also, you did not say where the Data Validation List should go, so I assumed cell A3. Change these assumptions to match your actual locations.
Code:
[table="width: 500"]
[tr]
	[td]Sub GetNonUnderscoreCells()
  Dim Addr As String, StartCell As Range
  Set StartCell = Range("[B][COLOR="#FF0000"]C3[/COLOR][/B]")
  Addr = StartCell.Address & ":" & StartCell.End(xlToRight).Address
  With Range("[B][COLOR="#0000FF"]A3[/COLOR][/B]").Validation
    .Delete
    .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:=xlBetween, Formula1:=Join(Split(Replace(Replace(Application.Trim(Join(Application.Index(Evaluate("IF(NOT(ISNUMBER(FIND(""_""," & Addr & "))),SUBSTITUTE(" & Addr & ","" "",""_""),"""")"), 1, 0))), " ", "|"), "_", " "), "|"), ",")
    .IgnoreBlank = True
    .InCellDropdown = True
    .ShowInput = True
    .ShowError = True
  End With
End Sub[/td]
[/tr]
[/table]
 
Upvote 0
The OP said "The source is a table row so "|" means another cell/column"

Great.:rofl: That cell was apparently a sufficient hint to me... Thanks for catching that Rick.

[TABLE="class: grid"]
<tbody>[TR]
[TD]Row\Col[/TD]
[TD]
A​
[/TD]
[TD]
B​
[/TD]
[TD]
C​
[/TD]
[TD]
D​
[/TD]
[TD]
E​
[/TD]
[TD]
F​
[/TD]
[TD]
G​
[/TD]
[/TR]
[TR]
[TD]
1​
[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]
2​
[/TD]
[TD]Apple[/TD]
[TD]Orange[/TD]
[TD]Apple_Nutrients[/TD]
[TD]Orange_Nutrients[/TD]
[TD]Banana[/TD]
[TD]Banana_Nutrients[/TD]
[TD]Banana_Count[/TD]
[/TR]
[TR]
[TD]
3​
[/TD]
[TD]
3​
[/TD]
[TD]Apple[/TD]
[TD]Orange[/TD]
[TD]Banana[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]
4​
[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]


B3, jusn enter:
Rich (BB code):
=COUNTIFS(A2:G2,"?*")-COUNTIFS(A2:G2,"*_*")

Name A3 Size thru the Name Box.

In B3 control+shift+enter, not just enter, and copy across:
Rich (BB code):
=IF(COLUMNS($B$2:B2)>Size,"",INDEX($A$2:$G$2,SMALL(IF(ISERROR(FIND("_",$A$2:$G$2)),
     COLUMN($A$2:$G$2)-COLUMN($A$2)+1),COLUMNS($B$3:B3))))

Next, define DVlist in Formulas | Name Manager as referring to:
Rich (BB code):
=OFFSET(Sheet1!$B$3,0,0,,Size)

Adjust the sheet name to suit.

DVlist is now ready for using as Source in data validation cells.
 
Last edited:
Upvote 0
I used Aladins solution because I haven't reached vba skills just yet :)
If you wanted to try the macro out on a copy of your workbook just to see how it worked, here is the instructions on how to implement the code I posted in Message #6 ...

HOW TO INSTALL MACROs
------------------------------------
If you are new to macros, they are easy to install and use. To install it, simply press ALT+F11 to go into the VB editor and, once there, click Insert/Module on its menu bar, then copy/paste the above code (from Message #6 ) into the code window that just opened up. That's it.... you are done. To use the macro, go back to the worksheet with your data on it and press ALT+F8, select the macro name (GetNonUnderscoreCells) from the list that appears and click the Run button. The macro will execute and perform the action(s) you asked for. If you will need to do this again in this same workbook, and if you are using XL2007 or above, make sure you save your file as an "Excel Macro-Enabled Workbook (*.xlsm) and answer the "do you want to enable macros" question as "Yes" or "OK" (depending on the button label for your version of Excel) the next time you open your workbook.

To make things simpler for you (especially if you will need to call this macro often), when you select the macro from the list, before clicking Run, you can click the Options button and assign a keyboard shortcut to it first and then use that keyboard shortcut the next time you want to run the macro. Alternately, you can add a Forms CommandButton to the worksheet and assign this macro to it, then you would only have to click the button to run the macro.
 
Upvote 0

Forum statistics

Threads
1,224,818
Messages
6,181,152
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top