Excel VBA Userform - How do I add a value to a combo box from a list in the userform? - vba

Is there a way to do the following:?
I would like a manual text box that when filled in and the user clicks "Add" and it adds the value to the combo box. However, I will also have this list stored somewhere so that the next time the user loads the form they can select "Previously Used" items from a list and these will then also be added to the combo box.
I will be adding a button to "Add all" of the previous items but it must still have the option to add items manually each time.
Even more useful would be to have an option to check items in and out of the combo box as well as the manually text input option.
I tried to add an image I found to better explain this but I need 10 rep points. Therefore, I hope this makes sense but please feel free to comment for more details and a better explanation if needed.
Link to an example of what I am thinking of but without the manual addition field:
http://kb.blackboard.com/download/attachments/14057766/bbui_multi_select.gif?version=1&modificationDate=1202823953747

Use this
combobox1.additem txtValue.Text
Sheets("SheetName").Cells(lastRow+1,col).value=txtValue.Text
Also add below code in Form_activate
For i=1 to lastrow
combobox1.additem Sheets("SheetName").Cells(i,col).value
next i

Related

Can't add items to a combobox

I have tried these sites for help:
excel-easy.com
sitestory.dk
ozgrid.com
stackoverflow.com
contextures.com
microsoft.com
Would appreciate the help!
Here is my code:
Private Sub FusegearPerformanceUserForm1()
With FailureComboBox
.AddItem "japp"
End With
End Sub
Manipulation of controls in Excel confused me for a while until I realized that there are two kinds of them, and this might be part of your confusion. For example, there is:
a Form Control Combo Box, and,
an ActiveX Control Combo Box.
The two look, behave, and are controlled similarly, but not identically.
Click image to enlarge:
I realize that wasn't your question, but I figured I should make sure you can identify which control you are using, and therefore make sure that you're using (and Googling) the correct information - especially since the terms "Combo Box" and "Drop Down Box" are often used interchangeably. A Google Search for Vcode related to BA Combo Box Control will be wrong 50% of the time, so you need to be more specific.
AS for the code difference:
FORM CONTROL Combo Box
'add item
ActiveWorkbook.Sheets("Sheet1").Shapes("Drop Down 1").ControlFormat.AddItem "abcd"
'remove all items
ActiveWorkbook.Sheets("Sheet1").Shapes("Drop Down 1").ControlFormat.RemoveAllItems
'https://analysistabs.com/vba-code/activex-controls/combobox/
ACTIVEX CONTROL Combo Box
'add item
ActiveWorkbook.Sheets("Sheet1").ComboBox1.AddItem "abcd"
'remove all items
ActiveWorkbook.Sheets("Sheet1").ComboBox1.Clear
More Information:
Different Types of Combo Boxes Explained
VBA ComboBox
MSDN : VBA Shape Members
So, just for completion:
The solution to this question was, that the file was saved in the XLSX format which cannot contain VBA code. After saving the file in the XLSM format everything worked as expected.

Adding multiple textboxes to a form in ms-access?

I would like to have a line on an Access user-form. Call it Item #1. I would like Item #1 to be a text box where the user can enter any type of information. So for example, say the user entered "Tutoring" and then next to this was an additional Textbox that allowed the user to enter the hours spent tutoring and the date in which the hours were logged.
What I would like is to have a button to allow the user to add a second line (or set of textboxes) when needed, or a third line, etc. Is this something that can be set up on the Access form? Does this need to be coded in VBA? Just looking for some tips to provide me some direction on the best approach.
"If the data should not be stored in a table, it' a bit more complicated. "
You would have to make the text boxes not visible when the form loads, and then make each text box visible on the After Update event of the previous box.

Access 2010 Me.Refresh/Me.Requery

I have a form that I want refreshed when the submit button is clicked. Preferably those that have default values will be restored and those without will be blank.
The submit button has an attached OnClick Macro that, checks to make sure all fields are filled, if so an action query runs that inserts a new row into a table.
So its after this action query that I want the refresh to occur. I have tried researching and come across suggestions that suggest using VBA code Me.Requery or Me.Refresh. I'm not 100% on how to incorporate this into the macro. The RunCode command doesn't recognize the function I put the code in, and The convert macro to VBA option in the top left is grey'd out.
I'm new to Access and just not seeing where the communications are for code and macros if someone could please elaborate for me I would be greatly appreciated.
Please consider my solution as a brief introduction to VBA. If you care to learn the language, you will find there is very little you cannot do.
Within your submit button properties, there should be an 'Event' tab. In this tab, the On Click should be set to [Event Procedure]. Click the three dot button just to the right of that, and it will launch the VBA editor.
All you need to have here between the Private Sub and End Sub lines are these lines of code:
DoCmd.RunMacro ("Mac_1")
Me.TextBox1.Value = "Null"
Me.CombBox1.Value = "Null"
Me.Refresh
MsgBox "Your Save Was Successful.", vbOKOnly, "Saved"
"Mac_1" is the name of the macro you want to execute. the ME.Refresh executes as soon as mac_1 finishes, and will refresh the page. Be sure to enclose it in a proper quote (") and not a double tick ('').
The Requery command can be used from both within VBA code or a macro. It sounds like you are using the macro designer so you can use the Requery macro action instead of VBA.
Just add this to your macro after you have inserted your new data.
This macro action lets you specify a control to requery. The parameter can be left blank to requery the source of the active object. More information can be found here.
Edit
In response to your comments, I think you should try experimenting with the SetProperty macro action (more details here).
I have attached this macro to a button click event and the value from the TextBox called txtInputValue is cleared. The Value field is left blank as you want to fully remove the value from the text box.

VBA Excel comboBox dropdown is empty after obj.addItem

I have an Excel2010 VBA userform that has one comboBox, from which the user should be able to select a currently-open Excel Workbook. The USERFORM_Initialize subroutine contains (among other things) :
cbWorkbook.Clear
cbWorkbook.Visible = True
For Each wb In Workbooks
cbWorkbook.AddItem wb.name
Next wb
I have set a breakpoint at this code, and am able to step through it; in the present situation there are four open workbooks, and the "for each" is iterated four times, as appropriate. And I can see that wb.name contains the values that I want.
However, when the form displays and the dropbox arrow is clicked, the "list" is empty. It looks like there is room for one item, and that item is blank. (I believe this is typical of an empty dropdown box.)
Select attributes for the combobox are:
Autosize=False; AutoTab=false; Enabled=True; DropButtonStyle=1-fmDropButtonStyleArrow;
Height=18; ListRows=8; ListStyle=0; Locked=False; ShowOptionWhen=2; SpecialEffect=2; Style=0; Visible=True. At the time of execution, cbWorkbook.listCount = 4
This is in development, and it did appear to work as expected yesterday, but now seems to never work. Any ideas where I might be going wrong?
EDIT: I found the solution to this: I had inadvertantly duplicated another combo box over the top of cbWorksheet, effectively hiding it. The control I was seeing was empty, while the control I wanted was overlaid. Deletion of the rogue control box solved the issues.
My apologies; this should have been the first thing I sought.
I found the solution to this: I had inadvertantly duplicated another combo box over the top of cbWorksheet, effectively hiding it. The control I was seeing was empty, while the control I wanted was overlaid. Deletion of the rogue control box solved the issues.
My apologies; this should have been the first thing I sought.
With ComboBox1
.AddItem "This"
.AddItem "Is"
.AddItem "A"
.AddItem "Test"
End With
or if you want to fill it with Range data:
ActiveSheet.Shapes("ComboBox1").Select
Selection.ListFillRange = "k1:k10"
PS - submit your file for review; it should be easier to look at!

How to wrap comment.text in vba

I have a situation in which text in one of the cells is massive.When I change the contents of this cell the previous value becomes a comment to that cell.Now the problem is, this text is so big that I cannot see complete text.If I use .Shape.Textframe.Autosize=true then I have to go on browsing till god knows when to see the text.
What I need to do is whatever and however big the text might be in Commnet.text,I want to show it in one and one place only.i.e when I hover on comment.
Place a TextBox where you will on your worksheet adjusted to the size you want, this still reads the comment although this is not on a hover, but perhaps will help
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Not ActiveCell.Comment Is Nothing Then
ActiveSheet.Shapes("Text Box 1").TextFrame.Characters.Text = ActiveCell.Comment.Text
Else: ActiveSheet.Shapes("Text Box 1").TextFrame.Characters.Text = ""
End If
End Sub
Actually after considering this further you really are trying to get comment to act as you want. So I recommend Resize all comments in the selected area and on the same link further down the page Show Excel Comments in Centre of Active Window.
These get you closer to what i think you want.
How about a pop-up text box? I.e., the user clicks on the cell and a pop-up form with the text appears. The best part is you can make the pop-up as big as you want.
Personally, I would make the pop-up resizable, but with only the Close button at top. I can't think of a way to trigger this form with just a mouse rollover, but I'm only using 2003. Later versions allow you more tricks.
xlSheet.Range(xlSheet.Cells(rowCount,1), xlSheet.Cells(rowCount,8)).Merge
xlSheet.Range(xlSheet.Cells(rowCount,1), xlSheet.Cells(rowCount,8)).HorizontalAlignment = 1
xlSheet.Range(xlSheet.Cells(rowCount,1), xlSheet.Cells(rowCount,8)).WrapText = True
xlSheet.Range(xlSheet.Cells(rowCount,1), xlSheet.Cells(rowCount,8)).RowHeight=45