I have a sheet (sheet A) with an activeX combobox, this combobox is filled with dates from another sheet (sheet B) on the format "dd/mm/yyyy" in text.
Now what i want is that when i select some date in the combobox on sheet A, i get the same date in another cell but 2 years add to it. So if i select "01/01/2018" i get "01/01/2020". Any help?
Use
Option Explicit
Private Sub ComboBox1_Change()
Range("A2") = DateAdd("yyyy", 2, Me.ComboBox1.Value)
End Sub
Code goes in code pane of sheet containing the activex object. Change A2 to the target output cell and ComboBox1 to the appropriate combobox.
Related
I am having an excel file with a bunch of UserForms, they are all working well. but I got stuck on one part.
I have a userform with a combobox1 and a textbox1.
in the combobox you can select 17,19,21,23,25,25+
in the textbox a numeric value should be typed, for e.g. 80
based on the selected value in the Combobox1 (17,19,21,23,25,25+) I want the data from the textbox (80) to be inserted in respectively column H,I,J,K,L or M of the active row. yet I cannot figure out how...
That's rather easy:
The items in the Combobox are indexed, the first item has index 0, the second 1 and so on. You can get the selected item with the property ListIndex. If nothing is selected, it returns -1, else the index of the selected item.
Now all you need is to use this as offset to the cell in the current row and column "H". Put the following sub into the form code and call it from whatever event routine you want (button, change-event of the textbox, form close...) Just change the names TextBox1 and ComboBox1 to the names of your controls.
Sub PutValueToSheet()
If Me.TextBox1 = "" Or Me.ComboBox1.ListIndex < 0 Then Exit Sub
Dim cell As Range
Set cell = ActiveSheet.Cells(ActiveCell.Row, "H").Offset(0, Me.ComboBox1.ListIndex)
cell.Value = Me.TextBox1
End Sub
So i have a Userform,
On this UserForm I have a ComboBox (comboDepartment), a TextBox (txtCode), and another TextBox (textDescription). Each ComboBox selection has a sheet. (e.g. Robo1, Robo2, etc...). The worksheets have 2 columns ( Column A = is the Error Code and Column B = is the Error Description)
Now my question, Depending on (comboDepartment) comboBox selection have it select the proper worksheet (Maybe set it as a varible?).
Next user would input an Error Code into (txtCode) TextBox (Just Numbers e.g. 1,2,3, etc...), after inputing the code I need it to fill in the (textDescription) TextBox from column B from the worksheet depending on the error code the user inputed.
Was wondering if this is at all possible?
Thank you in advance!
Collect all sheets by getting their names and add them on your combobox. On your Private sub Userform_Initialize method:
For each deptSheet in Thisworkbook.Worksheets
comboDepartment.AddItem deptSheet.Name
Next
Create a private variable for your worksheet (dim deptSheet as Worksheet) and set the selected value on the combobox. On your Private sub comboDepartment_Change method:
set deptSheet = Thisworkbook.Sheets(comboDepartment.Value)
You may now reference the sheet on the selected dropdown by: deptSheet.Range("...
based on the selected value in the Activex combobox how to use vlook up in the particular sheet and show that data in the another sheet.
use the LinkedCell property and type the address in there. If you want that value in the other spreadsheet you can use a simple worksheet formula = to get that done.
Add a script in VBA for the combobox when it changes like this:
Private Sub ComboBox1_Change()
Range("YOURCELL").Value2 = ComboBox1.Value
End Sub
Then cell "YOURCELL" will contain the value of the combobox whenever its value changes. If you reference that "YOURCELL" cell in your vlookup you are there!
I am trying the folowing since a few days but due to my lack of VBA skills don't get it working.
Scenario:
User: Selects a value from dropdown list (cells allow only a list
defined in another sheet).
Code: Copy the value left to the appropriate list value. (This is a list of names.)
Code: Paste the value into a specific field in sheet one.
Example:
The user is picking the value "Team One" from a dropdownlist in A1 in sheet one. This list is defined on sheet two. Next to each item of the list on sheet two is a cell with a comma separated list of names.
After the user has picked a team from the dropdown list, the corresponding list of names is copied into the field B1 in sheet one.
This procedure should only be fired when A1 is changed.
Hope I could make myself clear. If I finally find the solution myself, I will post it here.
Thank you for reading this.
You can do this without VBA. In the field you want the list of names pasted into enter this formula:
=IF(ISBLANK(<address of dropdown on Sheet1>),"",INDEX(<address of list to left of values on Sheet2>,MATCH(<address of dropdown on Sheet1>,<address of dropdown values on Sheet2>,0)))
This will be blank when nothing is selected from the dropdown and will display the appropriate list of names when a value is selected.
For example, if the dropdown is in B1 on Sheet1, the dropdown values are in B1:B9 on Sheet2, and the corresponding list of names are in A1:A9 on Sheet2, you would use this formula:
=IF(ISBLANK(Sheet1!B1),"",INDEX(Sheet2!A1:A9,MATCH(Sheet1!B1,Sheet2!B1:B9,0)))
EDIT (per comment):
To use this in VBA, you'll need to do something similar to what #chris neilsen suggested. In the Worksheet module, you'll need to create a sub for a change event:
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("B1")) Is Nothing Then
Range("A1").Formula = "=INDEX(Sheet2!A1:A9,MATCH(Sheet1!B1,Sheet2!B1:B9,0))"
If IsError(Range("A1").Value) Then
Range("A1") = ""
Else
Range("A1").Value = Range("A1")
End If
End If
End Sub
To remove any confusion, A1 is the cell that will display the output.
To do this as VBA, you would do something like the following. Per your original question, there is a list on Sheet2 that corresponds to the selection from the dropdown box and populates on Sheet1.
In the VBA editor:
In Sheet1, add the following methods
Private Sub ComboBox1_Change()
If ComboBox1.Text <> "Select" Then
Dim selVal As String
selVal = ComboBox1.Text
Range("B1").Value = GetList(selVal)
End If
End Sub
Public Function GetList(ByVal Value As String) As Variant
Dim result As Variant
result = Application.VLookup(Value, Worksheets("Sheet2").Range("A1:B100"), 2, False)
GetList = result
End Function
In the workbook object code, enter the following method:
Private Sub Workbook_Open()
With ThisWorkbook.Worksheets("Sheet1").ComboBox1
.AddItem "Team One"
.AddItem "Team Two"
.AddItem "Team Three"
.AddItem "Team Four"
.AddItem "Team Five"
.Text = IIf(.Text = "", "Select", .Text)
End With
Worksheets("Sheet1").Activate
End Sub
I should note that you could do this without any vba by simply using a a list control found in the Data Validation option in Excel. When you make a selection change in that, you would then use a standard VLookup in cell B1 to grab the corresponding value(s).
To implement this in VBA, use a Change event to monitor the data entry cell.
Assuming you have named your validation data range as ListData, put his in the module for Sheet1
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$A$1" Then
[B1] = Application.WorksheetFunction.VLookup(Target.Value, [ListData].Resize(, 2), 2, 0)
End If
End Sub
I would advocate an approach similar to the one that Excellll described, but with VLOOKUP rather than MATCH. To do this, you'd need to have your lists of names to the right of each team's name. For example:
| A | B
1 |Team 1 |Albert, Beth
2 |Team 2 |Carlo, Delia
3 |Team 3 |Egbert, Frederika
Now, if the team's name is at cell B7, you could use this formula to get the associated list of names:
=VLOOKUP(B7, Sheet2!$A$1:$B$3, 2, FALSE)
EDIT
See Doug Glancy's comment below explaining why Excellll's solution is better than using VLOOKUP.
I have a checkbox on sheet1 (there are about 7 sheets total). If the checkbox is selected (true), I want it to say "Approved" in cell M9. If the checkbox is not selected (false), I want it to say "Denied" in the textbox.
Do I need to create a macro for that?
If I want it to display the same text in cell M5 of sheet2, how would I put it all together?
You can link the status of a checkbox to a cell (right-click, format control, cell link on the control tab), this means you can then refer to that cell in any other sheet or cell.
Private Sub CheckBox1_Click()
If CheckBox1.Value = True Then
Range("M9").Value = "Approved"
Else
Range("M9").Value = "Denied"
Sheets("Sheet2").Range("M5").Value = "Denied"
End If
End Sub