I am selecting an email address from a combobox using a form in Excel and want to split the email address and display only first name from the selected email address. Please check the code as below:
Private Sub UserForm_Initialize()
Me.CboBugSts.List = Array("Open-Crtical", "Open-ShowStopper")
Me.CboCC.List = Array("Nandesh.kalyankar#wipro.com")
Me.CboToEmail.List = Array("firstName.LastName#Email.com")
Me.CboGrtngs.List = Array("Hi", "Dear")
TxtSubj.Value = "Bug notification logged by " & Environ("Username") & " - " & Now
End Sub
Private Sub TxtMsg_Change()
arTemp = Split(Me.CboToEmail.Text, "#")
Fname = arTemp(LBound(arTemp))
TxtMsg.Text = Me.CboGrtngs.Text & " " & Fname
End Sub
Here Me.CboToEmail.text is a value that user selects from a combobox text, and then I split the email address and concatenate it with another combobox text Me.CboGrtngs.text, but it shows the whole email, not just the first name from email.
Not sure (not a vba programmer), but since CboToEmail is defined as a list or array (with one item):
Me.CboToEmail.List = Array("firstName.LastName#Email.com")
Don't you need to extract the first element here, instead of calling .Text on the whole list?
arTemp = Split(Me.CboToEmail.Text, "#")
If not, what's the value of Me.CboToEmail.Text?
Related
So currently under specific conditions I have this InputBox poping up for the user to enter notes into and then that box fill those notes to a cell. What I would like to try to do is if notes are already in the cell that the InputBox is filling notes to, those notes will already appear in the input field so that the user can add to them. I have no idea if this can be done and could not find anything on google. If this cannot be done ill just use a user form insted.
The current code that brings up the input box is below, "notes" is the feild that the InputBox fills to:
If InStr(OPs, "Incomplete") > 0 Or InStr(OPs, "Miss") Then
notes.Interior.Color = RGB(255, 200, 0)
If notes = "" Then
Do While notes = ""
notes = notes & InputBox("You must imput notes for " & Desc & " !", "Notes")
Loop
End If
End If
Use the Default parameter of InputBox:
Something like:
notes = notes & InputBox("You must input notes for " & Desc & " !", "Notes", notes)
However, if you use this, you don't want to use the notes = notes & ... as the string field will quickly overfill.. so perhaps just:
notes = InputBox("You must input notes for " & Desc & " !", "Notes", notes)
What I am trying to do is basically you click a button, it brings up the Excel MailEnvelope to send an email, and you can then send it off to the relevant button email address'.
However one of the email addresses needs to be modifiable by the end user.
So I want a drop down where you select said email, and it then inputs that into the VBA code.
I know basically nothing about VBA and I could not find a way of doing this by searching around the web.
I figured I need some way of setting a variable to read a cell (the drop down cell), and then input that into the MailEnvelope Item.CC but I was struggling.
Any help would be appreciated.
This is what I have so far;
Sub Send_Range_Email()
' Select the range of cells on the active worksheet.
ActiveSheet.Range("B6:D302").Select
' Show the envelope on the ActiveWorkbook.
ActiveWorkbook.EnvelopeVisible = True
' Set the optional introduction field thats adds
' some header text to the email body. It also sets
' the To, CC and Subject lines.
With ActiveSheet.MailEnvelope
.Introduction = ""
.Item.To = "Email 0"
.Item.Subject = "Email Tracker Results"
.Item.CC = "Email 1" & text input here & "Email 2"
End With
End Sub
When using formulas, if you want to put a variable in there, just break it apart and add in the variable. As commented,
.Item.CC = "email 1" & "," & Range("A1").Value & ", " & "Email 2"
So to make super clear, say we want to add A1's value in this string: str = The man lives in STATE all the time by doing str = "The man lives in " & Range("A1").Value & " all the time"
I'm trying to send an email to recipients when a specific value of a cell is selected. If the value is 'new' then it gets sent to a predefined email. if the value is 'pending' or otherwise then it gets sent to a user entered email in another cell. I'm using this currently but i'm not sure how to modify it to look at the other cell.
Private Sub Worksheet_Change(ByVal Target As Range)
Dim myToAdd As String
If Target.Column = 6 Then
If Target.Value = "New " Then
myToAdd = "email#hotmail.com;"
ElseIf Target.Value = "Pending review" Then
myToAdd = 'I want this to look at another cell for the email
End If
With CreateObject("Outlook.Application").createitem(0) '0 will create a new email item
.To = myToAdd
.Subject = "A change request/project support has been request"
.Body = "Dear User," & vbNewLine & vbNewLine & "This is my email body "
.Display
End With
End If
You can use Cells([row],[col]) to reference other cells in the active worksheet.
On the other hand, if the worksheet is not active you can use Sheets([sheetname]).Cells([row],[col]).
#Cullen gave a good answer, also you can use:
SomeSheet.Range(<<cellreference)
e.g.
Sheets("CoolSheet").Range("A6")
I want to program a UDF to query a access db. I'm wondering if it's possible to have 2 different Workflows, depending whether it's a first execution or only a recalculation of the UDF.
Ideally I would have a UDF that you can feed the primary key of the db and the UDF presents an overview of possible values of the access db table. If it's a recalculation I don't want to have a userform popup again. Is this possible at all? Can somebody point me in the right direction?
Thx!
Edit
An attempt to show some (dummy) code:
public function key_from_table(primarykey as string) as string
' Read-out column names from Access table for userform
' Trigger userform with possible column names and let user choose
' readout Chosen column names
key_from_table = Call get_from_db(Primary_key, column_names)
end function
Function get_from_db(Primarykey as string, column_names as string) as string
'call Access db and readout result
end Function
If a recalculation is triggered the userform popup comes up again
I'm still new to Excel vba - pls tell me if this is rather stupid :)
Declares a global dictionary variable. Just before you trigger the form, check if dictionary already has the column name. If it does, don't trigger the form. If it doesn't, trigger the form and add the column name to dictionary once form is closed. You can clear the variable in Workbook_BeforeClose just to be clean
Something like this should work for you:
Public pub_sRecalcCheck As String
Public Function MyTest() As Boolean
Dim bReCalc As Boolean
If InStr(1, " " & pub_sRecalcCheck & " ", " " & Application.Caller.Address(External:=True) & " ", vbTextCompare) = 0 Then
'This is a brand new calculation
'Add this cell to the public variable storing where calculations for this UDF have occurred
bReCalc = False
pub_sRecalcCheck = WorksheetFunction.Trim(Replace(pub_sRecalcCheck, " " & rCell.Address(External:=True) & " ", " "))
''''''''''''''''''''''''''''''''''''''''''''
' '
' Your code here for new calculation '
' '
''''''''''''''''''''''''''''''''''''''''''''
Else
'This is a recalculation
bReCalc = True
''''''''''''''''''''''''''''''''''''''''''
' '
' Your code here for recalcuations '
' '
''''''''''''''''''''''''''''''''''''''''''
End If
MyTest = bReCalc
End Function
EDIT: And just in case the formula gets deleted from the cell, use this in the ThisWorkbook module to clear that cell's address from the RecalcCheck public string variable so that if a new formula is put there it is treated as a new calculation:
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
Dim rCell As Range
For Each rCell In Target.Cells
'Check if cell has been deleted/cleared
If IsEmpty(rCell) Then
'Found deleted cell, check if it is stored for the recalc checks
If InStr(1, " " & pub_sRecalcCheck & " ", " " & rCell.Address(External:=True) & " ", vbTextCompare) > 0 Then
'It is stored, remove it so that if formula is put back it is treated as a new calculation
pub_sRecalcCheck = WorksheetFunction.Trim(Replace(pub_sRecalcCheck, " " & rCell.Address(External:=True) & " ", " "))
End If
End If
Next rCell
End Sub
I'm currently trying to add a name from a form onto an email on Outlook. However I can only capture from one worksheet which works successfully:
.Subject = "New Form Name: - " & Worksheets("Form1").Range("C50")
However I want the code to be able to check if another worksheet in a different field has been filled out and to use the one which had data in it.
I've tried this (using Or), but with no luck:
.Subject = "New Form Name - " & Worksheets("Form1").Range("C50") Or Worksheets("Form2").Range("A25") Or Worksheets("Form3").Range("B19")
The above goes not generate anything in the Subject line of the email.
Is there a way for it to show which has data in one of the worksheets and to capture this.
Any help would be great!
Assuming you want to check in the order you wrote use this:
Dim sName as String
sName = Worksheets("Form1").Range("C50")
If sName = "" Then sName = Worksheets("Form2").Range("A25")
If sName = "" Then sName = Worksheets("Form3").Range("B19")
.Subject = "New Form Name - " & sName
This will show a blank for sName if none of the three have a name and will look in the certain order, but it's enough to play with to meet your exact needs.