Userform and Selecting Cell values - excel

Just playing around with userform. Pretty new to using them . Created a pretty simple userform that find me the percentage change between two values that I enter. (see image below). I'm just interested in going one step further, I want to know how to enter the values based on excel cells that I select. So in this example, if I select K6, the value 15 enters in current year and if I subsequently click on K8, the value 10 enters in base year). Hope that makes sense, let me know if that is possible. Thanks.
current code....
TextBox3_Change()
TextBox3.Value = Format((Val(TextBox1.Value) - Val(TextBox2.Value)) / Val(TextBox2.Value), "#0.00%")

Rather than clicking, why not load the values in those textboxes in the UserForm_Initialize() event? Something like
Private Sub UserForm_Initialize()
TextBox1.Value = Sheets("Sheet1").Range("K6").Value2
End Sub
If you really want to select a cell (after the userform is shown) and then populate the textboxes, then yes that is also possible. Here is an example
For demonstration purpose, I am going to populate 1 textbox. Let's say our userform looks like this. Note, I added a CommandButton1 next to the textbox. I changed the caption to .... We will use Application.InputBox with Type:=8 so that user can select a range.
Next paste this code in the userform code area
Option Explicit
Private Sub CommandButton1_Click()
Dim rng As Range
On Error Resume Next
'~~> Prompt user to select range
Set rng = Application.InputBox(Prompt:="Select the range", Type:=8)
On Error GoTo 0
'~~> Check if the user selected a range
If Not rng Is Nothing Then
'~~> Check if the range is single cell or not
If rng.Columns.Count > 1 Or rng.Rows.Count > 1 Then
MsgBox "Select single cell"
Exit Sub
End If
TextBox1.Text = rng.Value2
End If
End Sub
Demonstration

This isn't a built-in functionality for userforms (unlock the create a graph dialogue box).
One option would be to have the user start by selecting two cells, and then your code runs and calculates the percentage change in those two cells selected and returns the answer in a pop-up. Another option would be to have the userform automatically populate from preselected cells in your workbook (example below).
Private Sub UserForm_Activate()
Dim ActiveR As Long
ActiveR = ActiveCell.Row
TextBox1.Value = Cells(ActiveR, 1).Value
TextBox2.Value = Cells(ActiveR, 2).Value
TextBox3.Value = Cells(ActiveR, 3).Value
TextBox4.Value = Cells(ActiveR, 4).Value
End Sub

Related

vba- Userform with three comboboxes

I need some guidance how to create a selection for a excel userform that when the user selects CB1 which may be in A2, that it will also select the next cell B2 for the populated field and also select the next cell in C2 for the next populated field. I am not sure if my first selection should be a CB and the next fields maybe a list box or text field.
current code is:
Private Sub Userform_Initialize()
ComboBox_Combox5.List=Sheets("Sheet1").Range("A1:A650").Value
End Sub
Add two text boxes next to your combo-box.
I've left them with the default names of TextBox1 and TextBox2.
Code behind the form is:
Option Explicit
Private Sub Userform_Initialize()
ComboBox_Combox5.List = Sheets("Sheet1").Range("A1:A650").Value
End Sub
Private Sub ComboBox_Combox5_Change()
'Reference to selected value in column A.
Dim rng As Range
Set rng = ThisWorkbook.Worksheets("Sheet1").Cells(Me.ComboBox_Combox5.ListIndex + 1, 1)
Me.TextBox1 = rng.Offset(, 1)
Me.TextBox2 = rng.Offset(, 2)
End Sub
Always have Option Explicit at the top of your module.

3 comboboxes have same items (aa, bb, cc, and dd). If I select “aa” in one of the comboxes, how to make it not appear again in the other 2 comboxes?

I have 3 comboboxes namely combobox1, combobox2, and combobox3 with the same items each.
The items are, “aa”, “bb”, “cc”, and “dd”.
ALSO, these comboboxes are in the userform.
So, i have 3 comboboxes in USERFORM,
if I select “aa” in combobox1, how can I make “aa” not appear in the other 2 comboboxes, so that it
won’t be reselected again.
It’s like, if I select “aa” in combobox1, and “bb” in combobox2, the remaining items that is available in
combobox3 must be “cc” and “dd”.
But I don’t know how to do it. Can you help me with the codes? Thank you a lot.
Ps. I am using Microsoft VBA in excel
Pps. I am not pro.
Here is a basic sample of how you can achieve what you are looking for. You will need to change the code to work in your own project.
This is assuming the user will make a selection from the ComboBoxes in a specific order every time. E.g. this will work for selecting a value from ComboBox1 first and then provides the list for ComboBox2 but won't work (in its current form) for removing ComboBox2's selection from ComboBox1 list. It can be modified to do so, so feel free to do that if need be.
You can write your list to a worksheet anywhere you want - generally it's useful to do so in a hidden column or hidden helper sheet. This example has the values "AA" to "GG" in cells P6 to P12 on Sheet2 of the workbook.
The code loops through the range of your to to add items to the ComboBox 1 by 1, if the value is = the previous ComboBox selection then skip it otherwise add the value to the ComboBox.
Private Sub UserForm_Initialize()
'This Sub populates the first combobox with all values in the list
Dim TargetCell As Range
Dim ListRange As Range
With ThisWorkbook.Sheets("Sheet2")
Set ListRange = .Range("P6:P12")
For Each TargetCell In ListRange
Me.ComboBox1.AddItem TargetCell.Value
Next TargetCell
End With
End Sub
'__________________________________________________________________________________
Private Sub ComboBox1_Change()
'This Sub is similar to above, but has an If statement to check if the value on the sheet_
'is the same as the selected value in the previous combobox1. If it is it's ignored_
'otherwise Combobox2 has the value added.
Dim TargetCell As Range
Dim ListRange As Range
With ThisWorkbook.Sheets("Sheet2")
Set ListRange = .Range("P6:P12")
For Each TargetCell In ListRange
If TargetCell.Value = Me.ComboBox1.Value Then
'Skip
Else
Me.ComboBox2.AddItem TargetCell.Value
End If
Next TargetCell
End With
End Sub

Populate textboxes based on Combobox value

I am trying to populate a textbox based on the selection i made on the combobox. It would basically look up the combobox value and return the assigned value from the column (the 3rd) I chose. I have some code done but it returns a type mismatch error
Private Sub UserForm_Initialize()
ComboBox1.List = Sheets(1).Range("C15:C39").Value
End Sub
Private Sub ComboBox1_Change()
TextBox1.Text = Application.VLookup(ComboBox1.Value, Worksheets("sheet1").Range("A15:K39"), 3, False)
End Sub
Can anyone let me know what's wrong and if there is a better way to go about this?
Assuming your combo is not multiselect then load it with all of your range and just show the columns you are interested in. Make the number of columns in the ComboBox the same number of columns in your range.
ComboBox1.List = Sheets(1).Range("A15:K39").Value
ComboBox1.ColumnCount=11
ComboBox.ColumnWidths="0,0,100,0,0,0,0,0,0,0,0"
TextBox1.Value=ComboBox1.List(ComboBox.ListIndex,2) 'the columns are zero-based
Try maybe this:
TextBox1.Text = Application.WorksheetFunction.VLookup(ComboBox1.List(ComboBox1.ListIndex, 0), Range("C15:K39"), 1, False)
Thank you guys,
I ended up going with this
Private Sub ComboBox1_Change()
Dim myRange As Range, f As Range
Set myRange = Worksheets("Sheet2").Range("A2:B26")
Set f = myRange.Find(What:=ComboBox1.Value, LookIn:=xlValues, Lookat:=xlWhole, MatchCase:=False) '<--| try and find combobox selected value
If f Is Nothing Then
TextBox1.Value = ""
Else '<--| ... otherwise...
TextBox1.Value = f.Offset(, 1)
TextBox2.Value = f.Offset(, 2)
TextBox3.Value = f.Offset(, 3)
End If
End Sub
I had 3 textboxes that i populated with three different columns once an option is chosen from the Combobox.
What i am trying to do is have two last textboxes that I would eventually write numbers in and that would automatically update the corresponding cells.
The Problem I face is updating the cell that relates to the value entered on the combobox.
The code below is very rudimentary and i am wondering if i should do the same manually. First part of the code populates three first textboxes, second part is supposed to populate corresponding cells with value I enter depending on the combobox selection. Please let me know if i am not clear enough.
Private Sub OK_Click()
Sheets("Sheet1").Range("C9") = TextBox4.value
Sheets("Sheet1").Range("D9") = TextBox5.value
End Sub

Automatically Fetch Current Time in Excel

I am trying to build out a task tracker for my job that aggregates the amount of time I spend on various assignments.
Example rows of task tracker
Currently I am entering the start and finish times for each task manually with Ctrl + ;, Space, Ctrl + Shift +;. What I would like to do, if possible, is to have the start time column populate with the current time at whatever moment I set the drop down option to "Work in Progress," and then the end time column to do the same for whatever moment I change it to "Closed."
Drop Down Menu
Any ideas?
If the Drop Down is a Form Control (as opposed to an ActiveX control) then you could right-click it and choose Assign Macro.
It should default to something like DropDown1_Change, and you can click New.
Then paste in code "something like" this:
Sub DropDown1_Change()
If Range("A4") = 1 Then Range("A1") = Now()
End Sub
The first & last lines should already be populated for you.
You'd need to adjust the code to your needs. In this example:
The Drop Down is named Drop Down 1.
Cell A4 is the Cell Link for my Drop Down.
Cell A1 is the cell I want the date in.
I want to run the code when item # 1 is selected from the Drop Down's Input Range.
If your control happens to be an ActiveX DropDown/ComboBox, the steps are similar:
Right click the control and choose View Code.
You'll be taken immediately to the VBA Editor.
Paste the code.
The only way to do this, is using macro's.
In VBA (Excel -> Alt+F11) you can handle create events to handle things like cell changes.
The double click on the sheet you're entering your task tracker data and add the following code:
Private Sub Worksheet_Change(ByVal Target As Range)
End Sub
Now you can use the Target parameter to detect the cell which has been changed and then modify some other cell based on the changes cell.
So let's assume you have the dropdown in colum '4' and the begin and end time in column '5' and '6'. Then using the code below will update the begin and endtime field based on what state the taks has:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Column = 4 Then
If Target.Value = "Work in Progress" Then
Me.Cells(Target.Row, 5).Value = Time
ElseIf Target.Value = "Closed" Then
Me.Cells(Target.Row, 6).Value = Time
End If
End If
End Sub
NOTE
You need to create an Excel workbook with macro's which require you to enable them - by default Excel disables macro's - so take note on enabling them when asked.
Try this code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim FirstRow, StatusColumn, CurrentRow, CurrentColumn As Double
FirstRow = 5 'Define here the first row number of data
StatusColumn = 5 'Define here the column number of Status
CurrentRow = Target.Row
CurrentColumn = Target.Column
If CurrentColumn = StatusColumn And CurrentRow >= FirstRow Then
Select Case CStr(Target.Value)
Case "Work In Progress":
Cells(CurrentRow, CurrentColumn + 2) = Format(Now, "MM.dd.yy hh:mm")
Cells(CurrentRow, CurrentColumn + 3) = ""
Case "Closed":
Cells(CurrentRow, CurrentColumn + 3) = Format(Now, "MM.dd.yy hh:mm")
Case Else:
'Some code here...
End Select
End If
End Sub

Excel VBA: Userfom - textbox value depends on combobox value

I am creating userform which I will be using to insert data and then make some other stuff.
I have a Userform with ComboBox and few TextBoxes. ComboBox is filled with data from range. I want to change values of TextBoxes depending on ComboBox value. Values of TextBoxes should be filled with specific values from worksheet. I thought about creating For Each loop to determine Row of chosen ComboBox value and then change TextBoxes using Row number and setting proper offset.
Worksheet is table with headers and filled with data such as name, city etc.
However my code does not work within Userform.
Any ideas what is wrong or maybe a different approach to a problem?
klient = ComboBox name
Private Sub klient_Change()
Dim MyCell As Range, MyRange As Range
Dim wiersz As Long
Set MyRange = Range("klienci")
For Each MyCell In MyRange
If klient.Value = MyCell.Value Then
wiersz = MyCell.Value
Exit For:
End If
Next
MsgBox (wiersz)
End Sub
As follow up from comments to the questions, this code works:
Private Sub klient_Change()
MsgBox Range("klienci").Cells(1 + Klient.ListIndex, 1).Address
'do something else, e.g. get element one to the right:
MsgBox Range("klienci").Cells(1 + Klient.ListIndex, 1).Offset(,1).Address
End Sub

Resources