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
Related
This question already has answers here:
Why MS Excel crashes and closes during Worksheet_Change Sub procedure?
(3 answers)
Closed 1 year ago.
I haven't worked with VBA for a while, so my skills are a bit rusty. I've managed to put together a small script that will update the Date Contacted and Date Modified column I have in my excel sheet If I were to change the respective cell in column A. For all cells in column A, I set up a List by using the Data Validation tool. List = A, B, C, D.
The script should input the current date in Date Contacted (column H) when one of the list items is selected. If the cell is changed to a new item a message box will ask if the user wants to commit the changes. If yes, the Date Modified (column I) will be updated. If no, Column A should revert back to it's original self and neither one of the Date columns should be updated.
There are no bugs in the code, but when prompted to answer yes or no, clicking "no" continues to loop back to the message box. I am stuck in the loop and unsure how to escape.
Private Sub Worksheet_Change(ByVal Target As Range)
' Auto Date
Dim Cell As Range
Dim Answer As Integer
For Each Cell In Target
If Cell.Column = Range("A:A").Column Then
If Cells(Cell.Row, "H").Value <> "" And Cell.Value <> "" Then
Answer = MsgBox("Are you sure you want to make these changes?", vbYesNo)
If Answer = vbYes Then
Cells(Cell.Row, "I").Value = Int(Now)
Else
Application.Undo
End If
Else
Cells(Cell.Row, "H").Value = Int(Now)
End If
End If
Next Cell
End Sub
You need to turn off events before making any changes to the worksheet via the macro. Changes trigger the event and when your event makes changes, it re-triggers the event resulting in a loop, or in your case, a conditional loop.
Private Sub Worksheet_Change(ByVal Target As Range)
Application.EnableEvents = False
'Any code that makes changes to worksheet nested inside
Application.EnableEvents = True
End Sub
I have a UserForm where I have 6 check boxes. The role of check box is to select a unique range in Excel. Each checkbox corresponds to a different range in the Excel sheet.
I want to know what methodology can you use to ensure that when a user select a combination of checkboxes max of 6, Excel selects the corresponding ranges of the selected checkbox.
For example:
Checkbox1 programmed to select range A1
Checkbox2 programmed to select range H3
Checkbox3 programmed to select range F6
If User ticks Checkbox1 and Checkbox2 then how can you tell Excel to select A1 and H3 without using If statements since the combination of 6 check boxes would mean a lot of If statements.
Is there anyway when Checkbox1 is selected it keeps that selection in memory and adds it to the next selection.
Thanks
You would loop over the checkboxes, and build a range using Application.Union() (plenty of examples of that here on SO). When you're done looping then select the built-up range.
Or you can build a string like "H3,F6" and use Range(rangeString).Select
For example:
Sub CheckSelection()
Dim s As String, i As Long, sep
For i = 1 To 6
With Me.Controls("Checkbox" & i)
If .Value = True Then
s = s & sep & .Tag 'ranges are stored in checkboxes' Tag property
sep = ","
End If
End With
Next i
If Len(s) = 0 Then s = "A1" 'default selection if none chosen...
ActiveSheet.Range(s).Select
Debug.Print s
End Sub
Private Sub CheckBox1_Click()
CheckSelection
End Sub
'...
' etc
'...
Private Sub CheckBox6_Click()
CheckSelection
End Sub
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
I'm 15 and I'm doing a Internship as a Developer and I've got a kinda hard exercise.
I have a Table with 3 columns, A is "Number" B is "percent" and C is "Value". The column "value" is blank and I Need to calculate the value with a macro button. I've tried this, but it was wrong because I didnĀ“t calculate it in VBA:
Public Sub PushButton ()
Range("C2:C11").Formula = "=A2*B2/100"
Range("C2:C11").Value = Range("C1:C6).Value
End Sub
How do I solve this?
You are using a defined range, you could do it with a dynamic range like this:
Option Explicit
Sub PushButton()
Dim i As Long, LastRow As Long
With ThisWorkbook.ActiveSheet
LastRow = .Cells(.Rows.Count, 1).End(xlUp).Row 'first you need to find the last row on the active sheet
For i = 2 To LastRow 'then iterate through all the rows starting from 2, if row 1 has headers
.Cells(i, 3) = .Cells(i, 1) * .Cells(i, 2) / 100
Next i
End With
End Sub
If you need help understanding this code, let me know.
Edit: Explanation
Well, the first thing you must do is Dimension all your variables, and to help that you can use the Option Explicitright above all your code.
I've dimensioned 1 variable for the loop and another one to find the last row with text.
To find the last row what you are actually doing is going to excel, select the last row (1048576) and the column where it will have text, in this case 1 or column "A" and then pushing ctrl+Up excel and vba will get you to the last cell with text.
To do that you use Cells(Row, column) instead of manually inserting row 1048576 you can just use rows.count and it will be the same.
Once you get the last row you just iterate with a For iloop meaning For a variable called i which equals 2 (For i = 2) To LastRow (to the last row you calculated) VBA will repeat the code in between the ForAnd Next adding 1 number to i everytime the loop restarts.
In this case is just adding a number to the rows on Cells(i, 3) so you can modify that cell depending of its i value.
I think you need to question why Excel needs to calculate on demand rather than automatically like normal. Failing that there are a few options
You could change your calculation method to Manual using the following in the ThisWorkbook object
Option Explicit
Dim xlCalcMethod As XlCalculation
Private Sub Workbook_Open()
With Application
' Store users current method for when closing the workbook
xlCalcMethod = .Calculation
.Calculation = xlCalculationManual
End With
End Sub
Private Sub Workbook_BeforeClose(Cancel As Boolean)
' Reset calculation
Application.Calculation = xlCalcMethod
End Sub
and then when the button is pressed use the following code to calculate placed in a Module
Option Explicit
Public Sub Button_Click()
Application.Calculate
End Sub
Another option to do this without looping would be:
Sub CalculateRange()
Dim rng As Range
' Update for your Range
With ActiveSheet
Set rng = .Range("C2:C" & .Cells(.Rows.Count, "A").End(xlUp).Row)
End With
rng.Value2 = Evaluate(rng.Offset(0, -2).Address & "*" & rng.Offset(0, -1).Address & "/100")
End Sub
Finally, the way you've come up with is perfectly acceptable as VBA
VBA beginner here.
I have project where I have specified input cells for the user. When one of these input cells is changed, it needs to run a few lines of code that are specific to only that one cell. If the user clears the contents of the cell, I want the code to replace the blank cell with the value "0".
The code below simulates what I am trying to achieve. It is written in the same form as my project but is more succinct.
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Column = 1 And Target.Row = 1 Then
Range("B1").Value = "Changed 1" 'Just something specific to this cell. Not important
If IsEmpty(Sheet1.Range("A1")) Then Sheet1.Range("A1").Value = 0
End If
If Target.Column = 1 And Target.Row = 2 Then
Range("B2").Value = "Changed 2" 'Just something specific to this cell. Not important
If IsEmpty(Sheet1.Range("A2")) Then Sheet1.Range("A2").Value = 0
End If
If Target.Column = 1 And Target.Row = 3 Then
Range("B3").Value = "Changed 3" 'Just something specific to this cell. Not important
If IsEmpty(Sheet1.Range("A3")) Then Sheet1.Range("A3").Value = 0
End If
End Sub
Everything above works fine when the changes are performed on single cells. If the user selects all the cells and presses the delete key, it only runs the code for the first cell. I want it to run for all the selected (deleted) cells.
Any advice on how to simultaneously run the Worksheet_Change across multiple cells?
Thanks
When you have a change event that works just fine on a single cell, you can make a few adjustments to ensure that it also works when you change a range of cells in one go, like when you paste three cells into A1 to A3
You may want to apply an approach similar to this:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim ws As Worksheet
Dim cel As Range
Dim myRow As Long
Set ws = ThisWorkbook.Sheets("Sheet1")
If Not Intersect(Target, Range("A1:A3")) Is Nothing Then ' watch all the cells in this range
For Each cel In Target ' do the next steps for each cell that was changed
myRow = cel.Row
Range("B" & myRow).Value = "Changed " & Target.Row 'Just something specific to this cell. Not important
Application.EnableEvents = False
If IsEmpty(ws.Range("A" & myRow)) Then Sheet1.Range("A" & myRow).Value = 0
Application.EnableEvents = True
Next cel
End If
End Sub
Explanation:
If Not Intersect(Target, Range("A1:A3")) Is Nothing Then -- only act on changes to cells A1 to A3
For Each cel In Target - do the same thing for all cells that have been changed
Range("B" & myRow).Value = "Changed " & Target.Row - enter some value into column B of the current row
In the next step of the macro we will possibly enter some data into the cells we are monitoring for a change, i.e. A1 to A3. A change in these cells will trigger this macro. This macro will write into the cells. A change in the cells will trigger this macro ....
You see where this is going. In order to avoid an endless loop, we turn off any event triggered macros, e.g. macros that fire when a cell is changed. So we turn off event monitoring with this statement.
Application.EnableEvents = False - Now any events like "a cell has been changed" will be ignored.
We can now write a value into column A and that will not trigger the macro again. Great. We do whatever we need to do to cells A1 to A3 and then we turn event monitoring back on.
Application.EnableEvents = True
Then we go to the next cell (if any) in the range that triggered this macro.
Let me know if that helps or if you need a bit more detail. These things take a little learning curve.
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Application.Intersect(Target, Cells(1, 1)) Is Nothing Then
Range("B1").Value = "Changed 1" 'Just something specific to this cell. Not important
If IsEmpty(Sheet1.Range("A1")) Then Sheet1.Range("A1").Value = 0
End If
If Not Application.Intersect(Target, Cells(1, 2)) Is Nothing Then
Range("B2").Value = "Changed 2" 'Just something specific to this cell. Not important
If IsEmpty(Sheet1.Range("A2")) Then Sheet1.Range("A2").Value = 0
End If
If Not Application.Intersect(Target, Cells(1, 3)) Then
Range("B3").Value = "Changed 3" 'Just something specific to this cell. Not important
If IsEmpty(Sheet1.Range("A3")) Then Sheet1.Range("A3").Value = 0
End If
End Sub