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

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).

Related

I'm trying to add a hyperlink to a cell based on a user's copy-paste of the hyperlink into a userform. It keeps only linking back to my sheet,?

Like the title suggests, I've created a userform with input fields for requests to buy things. Name, Item, Justification, Price, and Link are all text fields on this form. Then, when the user submits the info, it flows into the table on the sheet.
Sub Submit()
Dim ws As Worksheet
Dim result As VbMsgBoxResult
With Sheets("Unfunded Requests - OSA")
.Rows(10).EntireRow.Insert
.Range("I10").Value = UFRForm.txtName
.Range("J10").Value = UFRForm.txtItem
.Range("K10").Value = UFRForm.txtJust
.Range("L10").Value = UFRForm.txtCost
.Range("L10") = Format(Range("L10"), "Currency")
.Hyperlinks.Add Anchor:=.Range("M10"), Address:=UFRForm.txtLink.Value, SubAddress:="", TextToDisplay:=UFRForm.txtItem.Value
.Range("H:N").WrapText = True
.Range("H:N").HorizontalAlignment = xlCenter
.Columns.AutoFit
.Rows.AutoFit
End With
result = MsgBox("Information Submitted. Submit another request?", vbYesNo, "Confirmation")
If result = vbYes Then
Call Reset
Else
Call ExitForm
End If
End Sub
The link goes into the cell, but it only links back to the workbook that I'm on, and doesn't show at all the URL that the user copies and pastes into the userform. I've tried to select the cell and insert the hyperlink, I've tried to separately insert the hyperlink outside of the 'with' group, etc.
Update - I tried to change the procedure to only place the value from the userform field into the cell, and it won't even do that. Nothing I place in the userform enters that cell. I can type in the field if I want, but from the userform, it won't make it. I tried .range("M10").Value = UFRForm.txtLink. I've checked the name of the field on the userform and that's it.

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?

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.

Excel button that writes down the time of clicking

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.

Resources