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.
Related
Sorry for the long one, and it's probably something simple that I'm overlooking after so much time.
I'm writing a small program that in just a few clicks, pulls data from an actor's "deal memo" pdf and put's that info into one of four possible excel templates (in different sheets) to export as a new "contract" pdf.
The application identifies certain key words/values from other cells that determines which sheet/template is used.
The issue comes at the following step:
-Based on the keywords, I need the exported document to identify if a series of folders are created, and if not, create them, and step in to create more relevant folders before finally saving the file.
The structure example is as follows, relevant created folders in Bold:
C:\Work Folder\ Deal Memo to Contract\Exported Contracts\Episode Number\Actor Name\Contract Type\final.pdf
Each folder name is created based on variables pulled from cell values. It works just fine when I put the string variables in quotes for testing, and even when the variables are stated by themselves, it pastes the proper path in the admin cells as shown in the following picture - Range A14:A21
Screenshot of Dashboard Page, admin column to be hidden
But even though it looks like a proper path address in the cells, VBA throws a
Runtime error 52:Bad file name or number on line 56, "PlayerExFolder = Dir(PlayerExPath, vbDirectory)"
Like I said, it's probably something simple. Any help would be great as I'm still pretty new to this. Oh, and I'm working on the Daily_Direct section of the if statements, the others will be identical once this starts working. Thanks!
UPDATE - It turns out that I had narrowed it down to what I thought were extra spaces that were ruining the path/folder creation. They were invisible "Ghost" characters. Ended up using the Clean function on the cells that were being used to name the folders. Hope this helps someone in the future.
Sub export_pdf()
Application.ScreenUpdating = False
Dim MainExPath As String
Dim MainExFolder As String
MainExPath = Worksheets("Deal2Contract").Range("C3").Value & "\Exported Contracts"
MainExFolder = Dir(MainExPath, vbDirectory)
If MainExFolder = vbNullString Then
Answer = MsgBox("An export folder for the generated contracts does not exist, I will create one for you", vbOKCancel, "Create Contract Export Folder?")
Select Case Answer
Case vbOK
VBA.FileSystem.MkDir (MainExPath)
Case Else
End Select
End If
Worksheets("Deal2Contract").Range("A15").Value = MainExPath
Dim EpiExPath As String
Dim EpiExFolder As String
Dim currEp As Integer
currEp = Worksheets("Data").Range("F14").Value
EpiExPath = Worksheets("Deal2Contract").Range("A15").Value & "\" & currEp
Worksheets("Deal2Contract").Range("A17").Value = EpiExPath
Dim PlayerExPath As String
Dim PlayerExFolder As String
Dim CurrPlayer As String
CurrPlayer = Worksheets("Data").Range("F8").Value
PlayerExPath = Worksheets("Deal2Contract").Range("A17").Value & "\" & CurrPlayer
Worksheets("Deal2Contract").Range("A19").Value = PlayerExPath
Dim TypeExPath As String
Dim TypeExFolder As String
Dim CurrType As String
CurrType = Worksheets("Deal2Contract").Range("A7").Value
TypeExPath = Worksheets("Deal2Contract").Range("A19").Value & "\" & CurrType
Worksheets("Deal2Contract").Range("A21").Value = TypeExPath
If Worksheets("Deal2Contract").Range("A7").Value = "Weekly_Direct" Then
ElseIf Worksheets("Deal2Contract").Range("A7").Value = "Weekly_Loan" Then
ElseIf Worksheets("Deal2Contract").Range("A7").Value = "Daily_Direct" Then
EpiExFolder = Dir(EpiExPath, vbDirectory)
If EpiExFolder = vbNullString Then
VBA.FileSystem.MkDir (EpiExPath)
Else
End If
PlayerExFolder = Dir(PlayerExPath, vbDirectory)
If PlayerExFolder = vbNullString Then
VBA.FileSystem.MkDir (PlayerExPath)
Else
End If
TypeExFolder = Dir(TypeExPath, vbDirectory)
If TypeExFolder = vbNullString Then
VBA.FileSystem.MkDir (TypeExPath)
Else
End If
'dateExFolder = Dir(dateExPath, vbDirectory)
'If typeExFolder = vbNullString Then
' VBA.FileSystem.MkDir (currType)
'Else
'End If
ElseIf Worksheets("Deal2Contract").Range("A7").Value = "Daily_Loan" Then
End If
Worksheets("Deal2Contract").Range("A15").WrapText = False
Worksheets("Deal2Contract").Range("A17").WrapText = False
Worksheets("Deal2Contract").Range("A19").WrapText = False
Worksheets("Deal2Contract").Range("A21").WrapText = False
Worksheets("Deal2Contract").Range("A23").WrapText = False
Application.ScreenUpdating = True
End Sub
I am working on a spreadsheet that has to be completed quarterly so am looking to automate a lot of the process. I have a master tab - "#" and 16 Team tabs. On the # is a table which includes all 16 team names, I would like this table to update depending on what Teams have signed off.
Quick runthrough:
Spreadsheet is emailed out to multiple teams, each log in and review Products. After all products have been reviewed, they press the "Sign Off" button. This button does 3 things;
Inputs username and date to right of button
Sends email to spreadsheet owner
Updates table on "#" tab.
Number 3 is where I am having the issue. I have tried find, if, functions - lots of different options but just can't get it to work. Functions didn't work as the spreadsheet is reset every quarter so the cell values are cleared, so it needs to be VBA.
Some previous options I tried:
Sub If_Team 1()
'Set variables
Set sht1 = Sheets("#")
Set sht2 = Sheets("Team 1")
'Team1
If sht2.Range("M2:N2") <> "" Then
sht1.Range("C4:D4") = sht2.Range("M2:N2")
sht1.Range("B4") = "P"
Else
sht1.Range("C4:D4") = ""
sht1.Range("B4") = "O"
End If
Unfortunately this worked until I put in more If functions, where it then pasted the data in the whole table rather than just Team 1. The below also worked, until again adding more values where it pasted the data in every field where the criteria was met (which was them all).
If pfID = "Team 1" Then GoTo 1 Else
If pfID = "Team 2" Then GoTo 2 Else
1 sht2.Cells(3, 2).Value = "P"
sht2.Cells(3, 3).Value = Date
sht2.Cells(3, 4).Value = Environ("username")
On each team tab is the team name, so lets say "Team 1". Team 1 is found in Cell "F1" on the Active Team Sheet. On the # tab in the table, Team 1 is Cell "A3".
What I would like to happen is ActiveSheet.Range("F1") to find the same name on the # (sht2) tab, and then do the following if the names match (so as we know Team 1 on the # tab is "A3"):
sht2.Range("A4").Value = "P"
sht2.Cells("A5").Value = Date
sht2.Cells("A6").Value = Environ("username")
This way the spreadsheet owner will only need to review the # tab to see who has signed off, rather than go through each tab. However I don't want it to point to an invdividual cell like above as I would like it to find and match the names.
Here is the full code so far:
Sub Button2_Click() 'SIGN OFF BUTTON
Dim cellAddr As String
Dim aCol As Long
' Declare variables
Dim c As Integer ' Column
Dim emBody As String ' Body text of email
Dim emCnt As Integer ' Count of email addressees
Dim emTitl As String ' Subject line of email
Dim emTxt As String ' List of email addressees
Dim myOutlook As Object ' MS Outlook application
Dim mySendmail As Object ' The email to be sent
Dim pfID As String ' Platform ID
Dim r As Integer ' Row
'Set variables
Set sht1 = ActiveSheet
Set sht2 = Sheets("#")
'Cell Address
cellAddr = ActiveSheet.Shapes(Application.Caller).TopLeftCell.Address
'Column Number
aCol = ActiveSheet.Shapes(Application.Caller).TopLeftCell.Column
'Input Date and Username
If aCol <> 1 Then _
sht1.Range(cellAddr).Offset(, 2).Value = Date
sht1.Range(cellAddr).Offset(, 1).Value = Environ("username")
' Obtain Platform details
pfID = ActiveSheet.Range("F1").Value
'Version ID
vID = sht2.Range("D1").Value
**'Input Sign Off on "#" Tab**
' Email subject line
emTitl = pfID & " - Out of Support Software Review " & vID & " Completed"
' Email body text
emBody = "<BODY style=font-size:12pt;font-family:Calibri>" & "Hi," & "<br>" & "<br>" & "Out of Support Software Review " & "<b>" & vID & "</b>" & " Completed for " & "<b>" & pfID & "</b>" & "."
Set myOutlook = CreateObject("Outlook.Application")
Set mySendmail = myOutlook.CreateItem(olMailItem)
With mySendmail
.to = ""
.Subject = emTitl
.HTMLBody = emBody
.Display
End With
' CLEAR.
Set objEmail = Nothing: Set objOutlook = Nothing
ErrHandler:
'
End Sub
Any help is appreciated, any questions let me know! Sorry if this is slightly confusing.
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 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?
I have created a simple spreadsheet in Excel 2010 containing a form that loads as the spreadsheet opens. The employee fills out the required form data and presses a "Save" button macro utilizing the SaveAs method.
My question is if it possible to disable the form in the saved copy? The reason is that I would like to avoid the form to load when our bookkeeping department opens the copy to review the data.
To clarify this is how my vba code is
Sub SaveAsButton()
Dim applicationUserName As String
Dim saveLocation As String
Dim fileName As String
Dim weekNo As String
Dim year As String
weekNo = ThisWorkbook.Sheets("Service").Range("M4").Value
Debug.Print (weekNo)
year = ThisWorkbook.Sheets("Service").Range("R4").Value
Debug.Print (year)
applicationUserName = Application.UserName
Debug.Print (applicationUserName)
saveLocation = "w:\Service Users\" & applicationUserName & "\Service\"
Debug.Print (saveLocation)
fileName = "Service" & "-" & weekNo & "-" & year & "-" & applicationUserName
Debug.Print (fileName)
fileName = Replace(fileName, " ", "")
Debug.Print (fileName)
ThisWorkbook.SaveAs (saveLocation & fileName)
End Sub
Thank you.
If you don't need any code in the final workbook you could simply save as .xlsx and thereby remove all the vb components
You may want to use CustomProperties for that.
First, create a CustomProperty:
ActiveWorkbook.CustomDocumentProperties.Add "Saved", False, msoPropertyTypeNumber, 0
Change the value of the CustomProperty to 1 when user saves the form (add the following code to SaveAsButton()):
ActiveWorkbook.CustomDocumentProperties("Saved").Value = 1
Add a check if ActiveWorkbook.CustomDocumentProperties("Saved").Value = 0 to the method which opens the form.