I can click on my ComboBox and see the values of Column1 and Column2, but after I click on off the ComboBox, the value in Column1 is always displayed and I want the value of Column2 displayed.
I tried this:
With ComboBox2
.Value = "None"
.ColumnHeads = True
.ColumnCount = 2
.ColumnWidths = "50;100"
.RowSource = "SetupQuestions!A42:B48"
.BoundColumn = 2
End With
That didn't set the value as I thought it would.
I tried this:
Private Sub ComboBox2_AfterUpdate()
ComboBox2.Value = ComboBox2.Column(2)
End Sub
That didn't set the value as I thought it would.
How can I force the ComboBox to display the value in Column2 after a selection is made?
The DropList of a ComboBox can show multiples columns, but after selecting a row, it can show only one column as Text. To show the second column use the property TexColumn.
Me.ComboBox1.TextColumn = 2
If you are only concerned with appearances, there is a workaround
Private Sub ComboBox2_Click()
With ComboBox2
.Text = .List(.ListIndex, 0) & " | " & .List(.ListIndex, 1)
End With
End Sub
Argh! It's not .Value
It's .Text
Private Sub ComboBox2_AfterUpdate()
Me.ComboBox2.text = Me.ComboBox2.Column(1)
End Sub
I just came across here because I was looking to solve this too. but other people's response helped me find the answer.
If you haven't gotten it yet, here is my solution.
Private Sub ComboBox_AfterUpdate()
ComboboxList.Text = ComboboxList.Column(0) & " " & ComboboxList.Column(1)
End Sub
An alternative you can use without VBA.
Combo row source (adjust for your situation):
SELECT Adults.aID, Trim([Adults].[LastName]) & ", " & Trim([Adults].[FirstName]) AS Expr1
FROM Adults WHERE ((Not (Adults.LastName)=("isNull")))
ORDER BY Adults.LastName, Adults.FirstName;
Basically, make your second column a composite single field via SQL.
Bound column: 1, Column count: 2, Column widths: 0cm;4cm
You can use this technique to display whatever you want by building the string representation as a single field.
The reason is in your setting of ColumnWidth. Your combobox shows two columns. The second one can't be displayed because the total width of your box is insufficient. Therefore you see the first column only. Set the ColumnWidth to "0;100" and you will see the second column. Make sure that there is a working relationship between the width of the box and that of the columns to be displayed within it.
Use a text field ond the right side of the combo box. Set the number of columns in the combo box to 2.
Set the list width the size of both combo and text together (a+b)
Set the columns width for the size of the combo and the size of the text (a;b)
Since the columns property is an 0 based array of columns, set the source of the text field to "=[MyCombo].Columns(1)" to display the second field.
When you drop down, the first column should be exactly under the combo box and the second column under the text box.
When you update the combo, the text box should update its value accordingly.
Additionally you may want to set the text box properties locked to true and enabled to false.
Open Design View
Open Property Sheet
Under the "Data" tab click on the three dots to the right of the "Row Source" item
In the Query Builder that opens, move the desired column you want to view in the combo box field where in the place where the currently viewed column is
Change any VBA code accordingly. For Example: If Column(1) was showing in the combo box field and you moved Column(2) to the place of Column(1), then Column(2) becomes Column(1) and Column(1) becomes Column(2). This will affect any VBA Code referring to Column numbers and also may affect the default value assessed for that Combo Box when a column number is not specified in the VBA code.
Related
I want to format a single column of a ListBox as Centered.
Is that possible?
Perhaps you can work around it. If you make a listbox containing 3 columns each with the same column width and you fill column 1 and 3 with nothing or one space and you fill column 2 with data, than it is centered in a way:
Private Sub UserForm_Initialize()
With Me.lstBox
.ColumnCount = 3
.ColumnWidths = 10
.BoundColumn = 2
.AddItem
.List(0, 1) = "Example"
End With
End Sub
Above code results in:
I don't think dat VBA has a property to center align data in a listbox (I read that VB6 does have it by the way). But this is not a single column I'll give you that ;)
A Userform listbox actually does dispose of a TextAlign property to center align data.
Side note: generally this would apply the same format to all list columns,
which might not be wanted in other occasions (e.g. for numeric data in additional columns).
Possible example assignment
Me.ListBox1.TextAlign = fmTextAlignCenter
TIA for the help.
I am having a problem with the row for shapes returning an incorrect value. Here is my setup:
Each of the buttons on the right is named after the corresponding column header, the value in column A of the same row, and a number combining the row and column number (ex. Edit_555_10 would be the name of the first shape in cell G3). This ensures there are no duplicate names for when Application.Caller is used in the code.
All of the "Import Data" buttons run the same macro to import data. The Batch IDs have a corresponding sheet which the data is pasted to by using this code:
variable = Shapes(Application.Caller).TopLeftCell.Row
Worksheets(Range("A" & variable).Value).Range("A1").PasteSpecial xlPasteAll
However, during runtime, any and all "Import Data" buttons will return 3 when assigning a value to the variable. I have tested it with 3 different "Import Data" buttons in I3, I4, and I5 at the same time, each returning 3. During runtime I have moved the buttons to all areas of the sheet, and they all return row 3 every time. I have even moved all shapes out of row 3 and 4 so it wouldn't be possible to return 3, and it still returns 3.
All the "Edit" and "Create Tabs" buttons behave normally and have no problems. But for some reason, even without duplicate names, and setting the "Import Data" buttons down as far as row 8, they always return row 3 during runtime, which is obviously causing data to be put in the incorrect place.
Any ideas?
You have not posted the full code for the miscreant Shapes, but if you substitute your code for the Import Data Shapes with:
Dim s As Shape, r As Long, sname As String
sname = Application.Caller
Set s = ActiveSheet.Shapes(sname)
r = s.TopLeftCell.Row
MsgBox r
do you get a different value for each Shape ??
So I actually figured it out. But I am still confused as to why it is happening.
When pressing the "Edit" button, an option is to delete the entry. This grabs the row from Shapes(Application.Caller).TopLeftCell.Row and sets it to a variable. It then deletes the entry with Range("A" & variable & ":I" & variable).Delete xlShiftUp When doing this, the "Edit" and "Create Tabs" buttons are deleted no problem. However, the "Import Data" buttons are just scrunched up to 0 height and end up between I2 and I3. So every time I had deleted the entries in the past, it just kept adding to the stack of invisible buttons; meaning when I was running a test with the same names, there were 10 or so duplicate buttons for each unique name, and the row of one of the invisible buttons was returned.
My workaround for this is to cycle through the shapes, compare each Shapes(name).TopLeftCell.Row with Shapes(Application.Caller).TopLeftCell.Row and delete the shapes in the same row.
So while the problem is fixed, I am not sure why the "Edit" and "Create Tabs" buttons were deleted correctly each time while the "Import Data" buttons remained and were scrunched to be invisible. If anyone has thoughts I would be interested.
i have a matrix (6*6) in sheet1 like below, and Inside its cells, I've written the countifs() formula for the number of correct items, Now i want a Listbox for Returning the name of items from sheet2
for example: by right-click in the cell (K10) Listbox show the name (USER 1)
sheet2.database;
matrix:
Create an ActiveX listbox on the page keeping the data (your matrix);
Access its Properties and set the ColumnCount to 6;
Set its ListFilRange as your 'matrix' address (A2:F7, for instance);
Now access its code (View Code in Developer Tab) and select your list box name from the upper left corner;
Then select ListBox_Change event from the top left corner;
Fill the next code inside it (supposing that your listbox name would be ListBox1):
Private Sub ListBox1_Change()
MsgBox ListBox1.Value
End Sub
Take care to exit Design Mode (from Developer Tab).
Now, changing the line in the listbox, a message with the first column content, for the selected range will appear...
i'm wondering if someone could help me with a little issue.
I have a textbox in an userform where people can add numbers for example 3. I need an macro when a checkbox is on then a few rows needs to be hidden. For example total range is B3 to B50 and if people typ 3 in the textbox B3 + 3rows needs to be visibele and the rest needs to be hidden.
Sub CommandButton1_Click()
If UserForm12.CheckBox2.Value = True Then
Rows("[B4+Karel]:B55").Hidden = True
End Sub
Could some one help me to fix this?
if people typ 3 in the textbox B3 + 3rows needs to be visibele and the rest needs to be hidden.
First hide all rows in the range and then show only the relevant rows. I am assuming that the name of the textbox is TextBox1. Change as applicable.
Is this what you are trying? (Untested). Also I have not done any error handling. I am sure you can take care of that?
Rows("3:50").Hidden = True
Rows("3:" & 3 + Val(TextBox1.Text)).Hidden = False
Note: When you are hiding/showing rows, you do not need the column names. You can directly work with row numbers as shown above.
Looking for advice on the best way to pass an Id of a value into a parameter in excel VBA.
Essentially I'm trying to replicate getting the value rather than the text itself like for example in html:
<option value="1">Option one</option>
Would return 1. I could concatenate the Id to the start or end of the string with something like:
.additem varList(0, 1) & " | " & varList(1, 1)
But I'm looking for a 'cleaner' option if that makes sense?
Cheers
Create your combobox with at least 2 columns. This can be set using the ColumnCount property, via the VBE or through VBA code.
You can then adjust the ColumnWidths property to make one of the columns a width of 0 so it will not be displayed/visible to users.
WHen you populate the combobox, simply put the ID in one column of the ComboBox, and put the value in the other visible column. The interface will look like this, unless you adjust the columnwidths
Use the BoundColumn of the ComboBox to return the appropriate value, or you can do some iteration over the selected item(s) and refer to the indexed position:
Debug.Print Me.ComboBox.List(0, 0) '# Display the first row item in the first column
Debug.Print Me.ComboBox1.List(0, 1) '# Display the first row item in the SECOND column