Select value from dropdown box but populate hidden column - excel

I have a set of unique IDs and names:
ID NAME
aa Jeff
bb Matt
cc Trung
dd Trung
All IDs are unique. Names are not.
On a worksheet I have a series of columns:
Date Time ID Name Value
1/1 1:30 aa Jeff 123124
1/2 2:20 cc Trung 12443234
Right now, a user will populate the ID field, the vlookup will return Name.
Is there a way to set up a dropdown on the ID cell that shows a concatenation of the ID and Name, but when selected, stores only the ID?
The idea is that the concatenated value that appears in the dropdown (Ex: aa | Jeff) is more user-friendly that just "aa".

Well, hope my answer will help. If not, please tell me and I will try to improve it.
The code is inside the Worksheet
Private Sub ComboBox1_Change()
'Just use the frist part of the string, ID
Range("I1").Value = Split(Me.ComboBox1.Value, " | ")
'Optional, if you want to put the name using code.
'If not, just use the VLOOLUP
Range("J2").Value = Split(Me.ComboBox1.Value, " | ")(1)
End Sub
Private Sub Worksheet_Activate()
Dim r
Dim c
Dim i
Dim L
Dim myRngID As Range
r = Range("C2").End(xlDown).Row 'the final row of the ID column
c = Range("D2").Column ' the number of the column of the name
Set myRngID = Range(Cells(2, 3), Cells(r, 3)) 'use only the ID range
'Just to clean the ComboBox everytime to avoid duplicates
Me.ComboBox1.Value = ""
L = Me.ComboBox1.ListCount
On Error Resume Next
For i = 0 To L
With Me.ComboBox1
.RemoveItem i
End With
Next i
On Error GoTo 0
'Pupulate the ComboBox with the string {ID[space]|[space]NAME}
For Each i In myRngID
With Me.ComboBox1
.AddItem i.Value & " | " & i.Offset(0, 1).Value
End With
Next i
End Sub
In the worksheet just this
As you can see, the only formula in the sheet is in J1, the VlookUp. In J2 the name is inserted using VBA.
The ComboBox has any special property. Everything is in the code above.
The result is that the name is always taken from the ComboBox, and then no matter which one is selected, always will be the right one, as in the VlookUp.

This should work
Select the Cell on the Second Sheet (the one that will be the vlookup)
Go to the Data Tab > Data Validation > Data Validation
On Settings Select List > Click Source button at the right of the input box > Select Range from the first workbook Screenshot
Now copy and paste that cell in the column on the second workbook

Related

How to get an output from a list box to populate some cells

I've created a userform with a couple of list boxes in. Listbox1 has all items in it, the user can select multiple values to then move into Listbox2. once they are in listbox2 I need them to populate some cells. With 1 list item per cell.
I'm having a pain trying to work out how to do it. So far all I've got is:
Private Sub CommandButton1_Click()
Dim tmpMsg As String
Dim t As Long
tmpMsg = "Selected categories:"
For t = 1 To ListBox2.ListCount
tmpMsg = tmpMsg & vbNewLine & ListBox2.List(t - 1)
Next
Worksheets("Specialist Prices").Activate
Range("a1").Select
ActiveCell.Value = tmpMsg
End Sub
This populates cell A1 with the entire set of list items. but I don't know how to put 1 value in a1 then move down and put the next in A2 and so on until all items are accounted for.
Any help would be appreciated.
Right now you are creating one string. It has linebreaks in it, but it´s still one text. If you are not using the tmpmsg for something else, you could use the for-loop to populate your sheet
For t = 1 To ListBox2.ListCount
Thisworkbook.sheets("Specialist Prices").Range("A" & t).value = ListBox2.List(t - 1)
Next
You can even do it faster by adding an array to the entire range. ListBox2.List is an array of values and you can paste that array into a range of cells:
ThisWorkbook.sheets("Specialist Prices").Cells(1,1).Resize(ListBox2.ListCount,1) = ListBox2.List
The Resize method here makes sure that the range contains the same amount of cells as there are elements in ListBox2. It resizes the range from 1 cell to a range of ListBox2.ListCount amount of rows and 1 column.

Add Conditons to Unhide column in a sheet with Mulitple Criteria

A little Help again please.
Codes below work for hiding columns that do not match B5.
Now my problem is, I want to unhide column that matches values
from B6 and B7 at the same time.
Reference Values from Command Sheet Column B Row 5,6,7.
Let B5 is MARCH
Let B6 is JANUARYsample picture
Let B7 is FEBRUARY
Sheet Name (GRA_NewGen CI) Note that All Data's per row/column are here.
Range from Sheet Name to Match B5,B6,B7 is Column C Row 4 up to End of Column in row with Values.
Below is the 'Code
'Sub GRA_NewGen_CI()
Dim cell As Range
Application.ScreenUpdating = False
With Sheets("GRA_New Gen CI")
For Each cell In .Range("C4", .Range("XFD4").End(xlToLeft))
cell.EntireColumn.Hidden = cell.Value <> Sheets("Command").Range("B5") And Not IsEmpty(cell)
Next cell
End With
Application.ScreenUpdating = True
'End Sub
If all you want is to hide all rows marked "JANUARY", "FEBRUARY" etc. you will have more flexibility and faster action by using Excel's Filter functionality. Learn here about Filters.
That gave me quite a ride. All those hidden columns are tricky. But now it's your turn. Please follow the instructions.
On your 'Command' sheet, find a blank column and enter "Show All" in one of the cells and this function in the cell below that:
="Show "& B5
I prefer you to have all the 12 months in B5:B16, but if you have only Jan to Mar or prefer to change the content on the fly that is OK as well. Copy the formula down for as long as you have relevant data (month names or column captions) in column B. Give the range I just described a name. I gave it the name "DropdownList". Make sure the named range has a 'Scope' of "Workbook" (meaning, it is visible from all parts of the workbook).
Place a command button on the GRA_New sheet in position A4. Perhaps you already have a button elsewhere. In that case I will ask you to play along and make another one for now. Later you can move this button to any other location, including another sheet, but not in a column to be possibly hidden. This command button will be a Validation drop-down. Enter
"Allow" = List and
"Source" =DropdownList (including the = mark.
You should now have a validation dropdown showing "Show All" in first position, "Show January" in second, and more "Show ..." depending upon the size of the named range DropdownList. Make sure that there is a single space between in "Show January" and "Show all", not more and not less, and every line consisting of 2 words, the second of which is relevant.
Now add the following procedure to the code sheet of the "GR_New ..." sheet.
Private Sub Worksheet_Change(ByVal Target As Range)
'17 Mar 2017
If Target.Address = Range("A4").Address Then
SetDisplay_GRA_NewGen Split(Target.Value)(1)
End If
End Sub
In this procedure, please change the reference to "A4" to the cell where you have the validation dropdown.
The next procedure goes into a normal code module. By default its name would be "Module1", but you can give it any name you like.
Sub SetDisplay_GRA_NewGen(ByVal Cmd As String)
' 17 Mar 2017
Dim Spike As String
Dim CountHidden As Integer
Dim FirstColumn As Long, LastColumn As Long
Dim CapRow As Long, Cap As String
Dim C As Long
CapRow = 4
FirstColumn = 3 ' = column C
With Worksheets("GRA_New_Gen_CI")
LastColumn = .UsedRange.Columns.Count
If StrComp(Cmd, "all", vbTextCompare) Then
With Range("DropdownList")
For C = 2 To .Rows.Count
Cap = Split(.Cells(C).Value)(1)
Spike = Spike & "|" & Cap
Next C
End With
For C = FirstColumn To LastColumn ' count hidden columns
Cap = .Cells(CapRow, C).Value
If .Columns(C).Hidden Then
If .Columns(C).Hidden Or InStr(1, Spike, Cap, vbTextCompare) = 0 Then
' if Cap can't be selected it is counted as not hidden
CountHidden = CountHidden + 1
End If
End If
Next C
Application.ScreenUpdating = False
If CountHidden = 0 Then
' hide all except the specified column
.Range(.Columns(FirstColumn), .Columns(LastColumn)).Hidden = True
End If
For C = FirstColumn To LastColumn
With .Columns(C)
If .Hidden Then
Cap = .Cells(CapRow).Value
If StrComp(Cap, Cmd, vbTextCompare) = 0 Then .Hidden = False
End If
End With
Next C
Else
.Range(.Columns(FirstColumn), .Columns(LastColumn)).Hidden = False
End If
End With
Application.ScreenUpdating = True
End Sub
Look for the two declarations in this procedure:
CapRow = 4
FirstColumn = 3
Row 4 is the row on your data sheet in which the program will look for the months names. Column 3 (= "C") will be the first column in which the program will expect to find a month's name. Columns A:B will never be touched.
Now your system is ready. You will need to know how to operate it.
1. When you select "Show All" from the dropdown all columns starting from FirstColumn will be shown. Call this a reset.
2. When you select any of the items from the dropdown columns with that name in CapRow will be shown.
3. When you select another month it will be added to the one already shown.
4. When all columns are shown already, only the selected one will be displayed.
You can modify the range DropdownList anytime, make it longer or shorter. The important thing is that the names in the dropdown are available in the CapRow. The program compares them as text, meaning "show all" is the same as "SHOW ALL".

Button Generates the columns from user input but not the cell lines?

I implemented a button that ask the user where to add a column, and the button takes the user input(A-Z) and generates the column until the end of the table NOT SPREADSHEET. The column ends based on how many rows there are in my table, meaning if there are 10 rows, after the user clicks the button an inputs where they want the column to be(They input a letter of the column A-Z), I should not see a column box on line 11 of the spreadsheet.
Now I've managed to do this my issue is below:
My issue is the cells the button generate does not include the lines or boxes around the cells so that you are aware that its an extension of the table?
here is what I mean: Picture of spreadsheet
notice the i column there are no lines around the cells?
Here is code, I think I am missing a copy function after the line
shift:=xlRight, but I don't know how to implement it?
I don't want to use macros because since the tables rows and column change due to the user's input I will have to constantly hard-code the range into the macro which i dont want.
Trust me I tried it an its annoying.
Private Sub CommandButton2_Click()
Dim x As Variant
Dim ColumnNum
x = InputBox("Enter a column that you want to add: ", "What column?")
If x = "" Then Exit Sub
ColumnNum = x
ThisWorkbook.Sheets("Sheet1").Columns(ColumnNum).Insert shift:=xlRight
ThisWorkbook.Sheets("Sheet1").Columns(ColumnNum).ClearContents
End Sub
you could try this:
Private Sub CommandButton2_Click()
Dim colIndex As Variant
colIndex = Application.InputBox("Enter a column that you want to add: ", "What column?", , , , , , 2) '<--| force a text
If colIndex = "" Then Exit Sub
With ThisWorkbook.Sheets("Sheet1").Columns(colIndex) '<--| reference column you want to insert
.Insert shift:=xlRight '<--| insert a new column , then the referenced one shifts one column to the right of the inserted one
.Offset(, -2).Copy '<--| copy the column two columns to the left of the referenced one (i.e. one column left of the new one)
.Offset(, -1).PasteSpecial xlPasteFormats '<--| paste formats to the new column
Application.CutCopyMode = False
End With
End Sub

sort two columns on excel while keeping blank cells

So I trying to sort a list of names that have favorite colors to each of those names. In other words I want to have the sort look like the following example: (A and B correspond columns while #'s correspond rows)
**A** **B** **A** **B**
1 Tim Red 1 Josh Black
2 Blue 2 Yellow
3 Purple 3 Maria Grey
4 Josh Yellow 4 Orange
5 Black 5 Pink
6 Maria Pink 6 Tim Blue
7 Orange 7 Purple
8 Grey 8 Red
I want it to sort the name first, and wherever that name goes, the colors follow its place and then sort the colors. Is there a way to do this without using VBA since I have no knowledge on how to use VBA. Any help would be very grateful and for the record, this is not for a class assignment.
I am currently using Microsoft Excel 2011 for Mac.
I'm not all that good with worksheet functions and stuff but I don't think you're going to be able to achieve what you want without using VBA.
Assuming Mac VBA is the same as on windows, the following code should get you started.
The Idea: Make a regular sort work by filling the blank 'name' cells and once the sort has completed remove the extra names. I haven't included the code to do the actual sorting but the two methods below should populate the empty cells and also empty them aferwards.
Public Sub InsertDuplicates()
Dim Sheet As Worksheet: Set Sheet = ThisWorkbook.Worksheets("Sheet1")
Dim Current As String: Current = ""
Dim Row As Integer: Row = 1
' Fill the blank cells in the names column
Do
If Sheet.Cells(Row, 2).Value2 = "" Then
' Break out of the loop
Exit Do
End If
If Sheet.Cells(Row, 1).Value2 = "" Then
' No name present, so populate cell with the current name
Sheet.Cells(Row, 1).Value2 = Current
Else
' A name has been found, set it as the current name
Current = Sheet.Cells(Row, 1).Value2
End If
' Goto the next row
Row = Row + 1
Loop
End Sub
Public Sub RemoveDuplicates()
Dim Sheet As Worksheet: Set Sheet = ThisWorkbook.Worksheets("Sheet1")
Dim Current As String: Current = ""
Dim Row As Integer: Row = 1
' Remove unwanted duplicate names in names column
Do
If Sheet.Cells(Row, 2).Value2 = "" Then
' Break out of the loop
Exit Do
End If
If Sheet.Cells(Row, 1).Value2 = Current Then
' Row is a duplicate so empty the value
Sheet.Cells(Row, 1).Value2 = ""
Else
' Row is different from the previous, store the value and continue
Current = Sheet.Cells(Row, 1).Value2
End If
' Goto the next row
Row = Row + 1
Loop
End Sub
Public Sub SortList()
' perform the sort (you can record a macro to get the code required)
End Sub
Public Sub DoIt()
' this is the main macro, call this sub to action a sort.
InsertDuplicates
SortList
RemoveDuplicates
End Sub
This can be done without VBA, using helper columns and a manual sort.
If you want to do this all the time instead of just once, you may want to consider changing your data architecture. Entering the name in a raw data sheet on every row, then build a pivot table that shows the sorted layout you are after.
Steps to do the sort manually:
Insert a header row in row 1 and put in labels for each column, i.e. Name, color.
Add another column and use this formula in cell C2 (copy down):
=IF(ISBLANK(A2),C1,A2)
Copy column C and paste over itself with Paste Special > Values. Now there are the names only in this column.
Use the sort dialog to sort by the new column first and by the color second. These are the settings before confirming the sort:
After the sort, you will see this:
Add another formula column with this formula starting in D2 and copied down:
=IF(C2<>C1,C2,"")
Copy column D, paste as values over column A. Delete the helper columns.

Data validation list, combo box, or active X combo box in Excel 2010?

Started with a data validation list and I like that it is in the cell where I want the data to appear. Tried combo box and active X combo box and don't like that they do not reside in the cell. This is very different than Access. This is what I am trying to accomplish:
My named range (Employee) is A4:C100, 3 columns, with headings Title, MI, and LN on a sheet named "Emp".
My form location is C6. I wanted to be able to show 3 columns and end up with data from the three columns. For example, Officer J. Doe.
Currently I am using data validation list entering data into one column as Doe, J., Officer and it works. The list can be long and I will need it to be in alphabetical order.
Is this the best way or am I confused with combo box and active X combo box?
The only way to show a combination of all 3 columns in a dropdown list is to concatenate the data in a 4th column e.g. use the following formula in Cell D4
=A4&" "&B4&" "&C4
...then you can name the range D4:D100. You may wish to hide this column for presentational reasons
Actually, you will probably want to avoid naming the whole range as the bottom cells may be blank/make scrolling more awkward than strictly necessary. I would recommend dynamic ranges
The next extension exercise might be to develop your formula to allow for e.g. a missing middlename e.g.
=A4&" "&IF(B4<>"",B4&" ","")&C4
The above assumes you can sort the data manually. If the data is not being sorted manually, you will need to use VBA e.g. ensure Column D gets completed and the named range created each time a user moves off Sheet("Emp"). You can embed the following code in the Emp sheet...
Private Sub Worksheet_Deactivate
For n = 4 to 100
If Cells(n, 1) <> "" Then
Cells(n, 4) = Cells(n, 1) & " " & Cells(n, 2) & " " & Cells(n, 3)
End If
Next n
Range(Cells(4,4),Cells(100,4)).Sort Key1:=Cells(4,4), Order1:=xlAscending, Header:=xlNo
LastRow = 4
Do Until Cells(LastRow + 1, 4) = ""
LastRow = LastRow + 1
Loop
ActiveWorkbook.Names.Add Name:="Employee", RefersTo:=Range(Cells(4,4),Cells(LastRow,4))
End Sub
The expressions for sorting/adding range names can be found by recording macros and eliminating code as in this expert Excel support video. Your data validation would refer to 'Employee' which is the range name created in the 4th column
There are a number of assumptions made above such as the idea that all employees have data in the first column and you would need to add logical tests if you do not always have data in all three columns
It may also be that you would prefer to create the Employee range when a user clicks in cell C6 of your form, as this may be more robust. My assumption in using Worksheet_Deactivate is that 'Employee' may be used elsewhere in your spreadsheet
Something like this. Put this code in your worksheet where the comboBox is
Private Sub Worksheet_Change(ByVal Target As Range)
Dim topY As Integer, leftX As Integer
topY = ComboBox1.top
leftX = ComboBox1.left
Dim c As Range
Set c = Cells(5, 5)
c.Left = topY
c.Top = leftX
c.Width = ComboBox1.Width
c.Height = ComboBox1.Height
End Sub
It should keep it locked in place if you move things around. Or you could try it in your Private Sub Worksheet_SelectionChange(ByVal Target As Range) event.

Resources