Excel button that writes down the time of clicking - excel

I want to make list of times in Excel Spreadsheets.
The idea is to have 4 buttons in the spreadsheet that will write down the time of clicking of each button.
Each button will represent a different group, and I wish to have a list of the times I pressed each button so I could check and research about the groups activity.

Create a new module in your VBE and stick this in there:
Sub capturetime()
Dim timeWS As Worksheet
Dim timeRange As Range
'Change "Sheet3" to whatever worksheet is your click log
Set timeWS = ThisWorkbook.Sheets("Sheet3")
'find the last cell in column A of your log
Set timeRange = timeWS.Range("A" & timeWS.Rows.Count).End(xlUp).Offset(1)
'Write which button was clicked in column A
timeRange.Value = Application.Caller
'Write the time in column B
timeRange.Offset(, 1).Value = Now()
End Sub
Edit the values as necessary in that code.
Create a button using Developer>>(Controls)Insert. Find the button in there. The "Form" button is fine.
Draw the button on the screen somewhere. And, when it prompts you to assign a macro, choose "capturetime"
This bit of VBA will grab the name of the button that was pressed and the time and write it to whatever worksheet you select as the your log. It's pretty simple, so it shouldn't be a bother to mess with it to fit your needs.

Related

How to find last empty row among many named ranges in worksheet and push the textbox value for selected named range without showing worksheet

off late I got developed interest in excel VB applications and practicing it since last 2 years. ( I use Excel 2016 version )
I have a problem with my recent Vb program. I have designed a main Userform with two ComboBoxes and out of it, ComboBox2 list is depending on ComboBox1 value.
For ComboBox1 row source, I have linked it to one of the named ranges in worksheet Sheet4("Settings") ColumnB which is actually the list of process names and in same sheet # different columns I have many named ranges (task list) bearing the name of all the items listed for ComboBox1 as headings.
So when I choose list in ComboBox1 dropdown , (Process names, which is also named range) only the relevant options will appear in ComboBox2 (task item) and upto this file is working fine.
Now to add more task line items to named ranges which will appear in combobox2 , I have designed another Userform (2nd one)so that user can add more task options himself under any selected process and he don't have to approach me.
So what I did, in a 2nd Userform I added combobox3 and TextBox1 with submit button. The Combobox3 will show same list as ComboBox1 does -Process names.
Intention of 2nd Userform is when user select process name from Combo3 followed by new task list entered in textbox1 and after submitting, Vb will look for named range selected in combo3, should go to worksheet "Settings" and find empty row in selected process range and will push the value of textbox1-task item. So that user will be able to get newly added value under his selected process in a main Userform.
But the problem is here, while operating the file the excel file is to set to show only Userform, to feel like App and will not display any worksheets during Userform operation. (No excel environment)
However while using 2nd Userform the code is forcing to display " Settings" worksheet (excel environment), since it is forbidden in code for not to navigate to excelworksheet, UF2 action failing in completing task.
However, If I keep Worksheets displaying in background (& 2nd Userform in front) then no issue, I was able to append the textbox1 value to desired named ranges in " Settings" ws.
The Error i get while using 2nd Userform without having excel sheets Opened is : " Run-time Error '1004' , Select method of Range class failed" . I tried using " application.screenUpdate=False/True", but no use, code stops working, unless excel worksheet is displayed...
any help in this regard will be appreciated
Code written :
Private Sub UserForm2_Initialize() ' UF name "addCat"
Dim i As Long
For i = 2 To Sheet4.Range("B50").End(xlUp).Offset(1, 0).Row
Me.SelFnCB.AddItem Sheet4.Cells(i, 2).Value
Next i
End Sub
'---------------------------------------------------------------
Private Sub CatUpdtCmd_Click() 'this is to add new items under selected namedRanges in worksheet
Dim lastRow As Long
Dim ws As Worksheet
i = Me.SelFnCB.Value 'Combobox 3 value with process names as named Range
Set ws = Worksheets("Settings")
Worksheets("Settings").Unprotect "Bnm0000"
Range(i).Select ' at this stage of code execution , screen tries physically go to worksheet and tries to display it.which i dont want.
Range(i).Find("").Activate
ActiveCell.Value = Me.NwCat.Value '( "NwCat" is TextBox1 with new task to be added to selected named range)
Worksheets("Settings").Range("A1").Select
Worksheets("Settings").Protect "Bnm0000"
Me.NwCat.Value = ""
ActiveWorkbook.Save
MsgBox " Done, Select the process again to view your update ", vbInformation, Title:=" Category added "
Unload Me
addCat.Show
End Sub

How to generate a copy of a page from a drop down menu in Excel?

I am developing an electronic audit sheet for work, where the desire is to generate a new work sheet each day, copied from a blank master sheet. This part is easy, I have a simple macro attached to a button that generates a copy of my master sheet, places it last in the tab, and automatically names it the current day's date.
Sub NewDay()
Sheets("Master").Copy After:=Worksheets(Worksheets.Count)
NewPageName = Format(Date, "dd-mm-yyyy")
ActiveSheet.Name = NewPageName
End Sub
What I would now like to do is to give the ability to create a new sheet from a selection of 'master' sheets, while retaining the date-as-sheet-name part.
Ideally, the user experience would be to select from a drop down menu the sheet, and click a button to create the new sheet, or to click the button, be presented with the list of options to select, and then generate.
I am relatively inexperienced at VBA, and this is beginning to go out of what little realm of mastery I have. Any and all help would be greatly appreciated.
For selecting items from a list in a User form, I strongly recommend ComboBox. In Project Explorer, insert a userform, and add a ComboBox and a Button. To create the list for the user to select from. We need to have our macro do ComboBox.Add(SheetName,Index) for each of the Master Sheets. To keep track of everything, it's best to have them declared in an array.
We then pop-up the form for the user to interact with using UserForm.Show. Once the user has made their selection, the select button does UserForm.Hide to close the form.
Since we created the combobox list from an array, the list index of the selected item is equivalent to the array index of the sheet we want to copy.
Sub Start()
Dim MasterSheets() As Variant, ShtSelect As Integer
MasterSheets = Array(Sheet1, Sheet2, Sheet3, Sheet4)
'This is your list of Master Sheets.
'Change Sheet1 and Sheet2 to Sheets("Master1") and Sheets("Master2")
With UserForm1
.ComboBox1.Clear
For i = LBound(MasterSheets) To UBound(MasterSheets)
.ComboBox1.AddItem MasterSheets(i).Name, i
Next i
.Show
ShtSelect = .ComboBox1.ListIndex
'This is how to get the user selected item from the ComboBox
End With
'The Selected Sheet is referenced by MasterSheets(ShtSelect)
'Your code from before
MasterSheets(ShtSelect).Copy After:=Worksheets(Worksheets.Count)
NewPageName = Format(Date, "dd-mm-yyyy")
ActiveSheet.Name = NewPageName
End Sub
These are the only lines inside UserForm1
Private Sub CommandButton1_Click()
If UserForm1.ComboBox1.ListIndex <> -1 Then Me.Hide
'ListIndex = -1 means that the user has not yet selected anything.
End Sub
You may also want to check to see if a sheet with that name already exists before attempting to create it. Otherwise you may run into errors if someone runs the macro twice in one day.
I would suggest something like:
NewPageName = Format(Date, "dd-mm-yyyy")
If Sheets(NewPageName) Is Nothing Then 'Only copy if it doesn't already exist
MasterSheets(ShtSelect).Copy After:=Worksheets(Worksheets.Count)
ActiveSheet.Name = NewPageName
End If

ActiveX Command Button combined with ActiveX List Boxes – VBA Code Needed

I would like to create an ActiveX Command Button that will populate cells on one worksheet based off of data from a separate, hidden sheet (still in the same workbook though). However, I want the button to know to pull specific data depending on selections that were chosen in list boxes.
For example: Let’s say someone is working in “Worksheet 1,” where 2 ActiveX List Boxes and 1 ActiveX Command button are located. If this individual chooses “Animal” in the first list box, then the second list box will display a following list of animals. Then, let’s say that person chooses “dog” in the second list box. I then want the ActiveX Command Button to recognize that someone chose both “animal” and “dog” in the two previous list boxes, and then when clicked, be able to pull data from a separate hidden sheet (“Worksheet 2”) to display on “Worksheet 1.” So, in other words the data for the selection “dog” would be hidden away from the individual, but when they choose “dog” in the second list box, and then click the ActiveX command button, then the data would appear.
I already know how to write the code in VBA for dependent list boxes, but I am unsure on how to make an ActiveX Command Button dependent upon selections in a list box. Is this possible, and if so, can anyone provide me with a VBA code that I can use to fulfill my specifications? Furthermore, does anyone know a VBA code for an ActiveX Command Button that will pull data from one sheet, and display it on another? Any input is appreciated. Thanks!
Hard to be specific with the info given, but hopefully this can point you in the right direction. This assumes that you only have one column in your 2nd ListBox:
Sub cmdActiveX_Click()
Dim strBox1 as String
Dim strBox2 as String
strBox1 = Worksheets("SheetWithListBoxes").ListBox1.List(Worksheets("SheetWithListBoxes").ListBox1.ListIndex))
End Sub
A cleaner way might be:
Sub cmdActiveX_Click()
Dim lbo as ListBox
Dim strBox1 as String
Set lbo = Worksheets("SheetWithListBoxes").ListBox1
strBox1 = lbo.List(lbo.ListIndex)
End Sub
So, you get the selected item's index from ListIndex, and then you pass that to the List method. This also assumes that MulitSelect is off (single).
Have fun!
Well, I believe I actually found a way to have the Command Button pull data from one sheet and place it on another sheet by creating this module:
Sub PopulateMain(ByVal lRowNumber As Long)
Dim shReport As Worksheet
Dim shData As Worksheet
' Assign sheets to the variable.
Set shReport = ThisWorkbook.Worksheets("Report")
Set shData = ThisWorkbook.Worksheets("Data")
' Paste the value in sheet Data Range H2 into sheet Report Range C12.
shReport.Range("C14").Value = shData.Range("H2").Value
shReport.Range("C16").Value = shData.Range("I2").Value
shReport.Range("C18").Value = shData.Range("J2").Value
shReport.Range("C20").Value = shData.Range("K2").Value
shReport.Range("K14").Value = shData.Range("L2").Value
shReport.Range("K16").Value = shData.Range("M2").Value
shReport.Range("K18").Value = shData.Range("N2").Value
End Sub
And then using this code:
Private Sub CommandButton1_Click()
Call PopulateMain(2)
End Sub
However, I still don't know how to link the Command Button to the List Boxes so that it knows a particular choice has been made. So, again, if someone chose "Animals" in the first box and "Dog" in the second box, I need the Command Button to recognize those two choices in order to pull the correct data from the hidden sheet.
Ok, try something like this (by the way, are these list boxes or combo boxes? Confused by your naming if they are list boxes or not...if combo, change the As ListBox to As ComboBox):
Sub cmdActiveX_Click()
Dim lboCategory as ListBox
Dim lboDependent as ListBox
Dim strBox1 as String
Set lboCategory = Worksheets("Report").cboCategoryList
strCategory = lboCategory.List(lboCategory.ListIndex)
Set lboDependent = Worksheets("Report").cboDependentList
strCategory = lboCategory.List(lboCategory.ListIndex)
End Sub
Then use the 2 string values to find your data...make sense?

Pull data from specific cell to load as text in an email

I am trying to generate an email from a button click and have it load data( as the subject line for the email) from the first cell in the row that the button is located in.
Example: In row 22 I place a button in column X and when it is clicked it creates an email using cell A22 as the subject for my email.
I know how to do this by inputting the subject line data directly into the script but, I want to avoid creating hundreds of scripts for the large amount of rows in my spreadsheet.
Here is the manual entry method I currently have:
Private Sub CommandButton1_Click()
Dim outobj, mailobj
Set outobj = CreateObject("Outlook.Application")
Set mailobj = outobj.CreateItem(0)
With mailobj
.To = "xxxxxxxxx#xxxxxxxx.com"
.Subject = "XXXXXXXXXXXX"
.Body = "XXXXXXXXXXXXXXXXXXXXXXX"
.Display
End With
'Clear the memory
Set outobj = Nothing
Set mailobj = Nothing
End Sub
I just need the subject to populate from column A in the corresponding button row.
I think I need to create another object but I am not sure what the correct code would be and I am not sure how to have the script pull it for ".subject =".
Here is basic data of what I have tried:
► ActiveSheet.Buttons(Application.Caller).TopLeftCell
► "Row of pressed button: " & ActiveSheet.ShapesApplication.Caller).TopLeftCell.Row
► Dim SubjectTo As String
SubjectTo = Cells(XXX,XXX).Value
.subject = SubjectTo
And quite a few other suggestions around the net.
All have worked in their original scripts but I cannot figure out how to incorporate them into what I am trying to do.
I even tried using a Form Shape instead of an ActiveX Button.
From your code sample, it seems that you are using an ActiveX style button. I know of no way to determine the row number from an ActiveX button but if you switch to a Form Control type button, retrieving the row is easy.
Sub Button2_Click()
MsgBox ActiveSheet.Shapes(Application.Caller).TopLeftCell.Row
End Sub
Having one button per row for a large number of rows is impractical. You could have one button that relies on either the selected cells or the activecell to identify the row. Another possibility is a Worksheet.BeforeDoubleClick event macro that opens an email whenever one of the rows is double-clicked (thereby having no buttons at all).

how to add item to a combobox on button click?

I want to add item to a combobox found in excel worksheet from text box which is located in the user form When button is clicked.i see the value added to combobox but it will become empty when I close and reopens the workbook.can any one help me handling this?
Thank u for you're fast response first
thank you both for your feedback and correction.let me make more clear my concern
Create a workbook and save as xlsm.
On the first worksheet define user name as follows:
Name: dn_cmb_items
Range: =""
Using the developer ribbon add an Excel (not ActiveX) combobox onto the Worksheet1 and set its list-by-range to dn_cmb_items
Open VBA editor and add a user form to the workbook, name it as frm_add_cmb_item and set ShowModal to False.
Drop a text box to the form and name it tb_item_text.
Drop a button to the form, name it cmb_add and from its context menu choose View code. This creates the click event handler.
Implement the handler as follows:
Private Sub cmb_add_Click()
Dim v_r As Range, v_n As Name
Set v_n = Names("dn_cmb_items")
If v_n.Value = "=""""" Then
v_n.Value = "=" & Worksheets(1).Name & "!$A$1:$A$1"
v_n.RefersToRange.Value = tb_item_text.Text
Else
Set v_r = v_n.RefersToRange
Set v_r = v_r.Cells(v_r.Rows.Count + 1, 1)
v_r.Value = tb_item_text.Text
v_n.Value = "=" & Worksheets(1).Name & "!$A$1:" & v_r.Address(True, True)
End If
End Sub
Drop onto the workshet a button, then create/set a macros in the workbook. Implement the created macros with the code frm_add_cmb_item.Show.
In the VBA Editor from the Debug menu choose Compile. Then save the VBAProject as well as the workbook. That's all for the coding.
Switch to the worksheet, show the form.
Now when you enter some to the textbox, then click the cmb_add button, a new item will be added to the A column at the end thus changing the value of dn_cmb_items assigned to the combobox on the worksheet.
See the screenshorts attached:
Initial state:
1 added:
2 added:
PS
I have the ready workbook with all the code. Where should I upload it?
Be specific with your question and always post the relevant code, so that it will become easy to solve it for others.
If you want to see the data while executing the userform, just write the required data in userform_activate or Initialize. before executing it will take the values and shows up in the combobox.
the input which you are taking from the worksheet just write those values in another worksheet so that whenever you open the workbook the values will not get erased.

Resources