My coworkers often have to send out invites for interviews (up to dozens at a time). I am writing a macro to help them autogenerate Outlook meeting invites based on all of the interviewees' information, which we store in an Excel data file.
E.g., "Dear [INSERT NAME], We would like to invite you to an interview about [INSERT SUBJECT] on [INSERT DATE]. Please call into the interview using [INSERT PHONE NUMBER] and be prepared to talk about patients with [INSERT CONDITION]."
Because my coworkers do not know VBA I'm trying to have them write it in an input sheet and have the program read it and the formatting and store it in a variable called MeetingInviteBody. So I need to take a cell's value and read it as a variable definition. The problem is the entire thing is entered as a string, even if the cell's contents are part string and part reference to another variable. Is there a way to fix this?
Thank you in advance!
Addendum: I'm hoping this will clarify what I was trying to do. This was the macro I wrote as a test:
Sub MultipleDataTypeInput()
Dim FirstLineofText As Variant
Dim PhysicianName As String
PhysicianName="Dr. Smith"
FirstLineofText=Sheets("Sheet1").Cells(1,1).Value
MsgBox(Prompt:=FirstLineofText)
End Sub
I put "Dear" & PhysicianName & "," in cell A1, What I was hoping was for Excel to then read the macro as
FirstLineofText="Dear" & PhysicianName & ","
If that happened the MsgBox would say "Dear Dr. Smith,"
Instead the prompt was ""Dear " & PhysicianName & ",""
Does this make sense?
One way to do this would be to use the replace function in VBA.
However, in cell A1, write "Dear PhysicianName," and get rid of your & concatenation. (The way you wrote it would work if you were writing it strictly in VBA. The code would concatenate the values together. For example: FirstLineofText = "Dear " & PhysicianName & ",")
Sub MultipleDataTypeInput()
Dim FirstLineofText As Variant
Dim PhysicianName As String
PhysicianName="Dr. Smith"
FirstLineofText=Sheets("Sheet1").Cells(1,1).Value
FirstLineofText = Replace(FirstLineOfText,"PhysicianName",PhysicianName)
MsgBox(Prompt:=FirstLineofText)
End Sub
I suggest this way because you said the coworkers are writing their own scripts and i didn't want to suggest an entire new methodology as it may confuse you. That said, I think there are way more efficient ways to design this.
i think that you have your logic backward
you probably want this
Sub MultipleDataTypeInput()
Dim PhysicianName As String
Dim PatientName As String
PhysicianName = Sheets("Sheet1").Range("A1").Value ' this cell should hold "Dr. Smith"
PatientName = Sheets("Sheet1").Range("B1").Value
MsgBox "Dear" & PhysicianName & ", We would like to invite you to an interview about " & PatientName
End Sub
Related
I came across a problem, I try to export a date by using IF-function in excel als followed,
=WENN(C2="Y";TEXT(HEUTE();"JJJJMMTT");"")
it is in German, which in English is,
=IF(C2='Y'; TEXT(TODAY();"YYYYMMDD");'')
The language of My computer is in German, so it works well, but my colleagues in USA had problem, which they can't export the date with correct date, it shows today as example "JJJJ16TT", it can't show the year and day...
I don't know how to set it right. So I try to ask help from you.
Thanks and best regards.
Two possible methods:
Don't use the TEXT function.
Change your formula to:
=WENN(C2="Y";HEUTE();"")
- and apply a number format to the cell without the LCID.
Especially if your code is part of a longer function, you can
Use a Defined Name for the date string
Create a Workbooks_Open event to change that Name depending on the language of the user
Change your formula to: =WENN(C2="Y";TEXT(HEUTE();dtFormat);"")
eg:
To enter this Macro (Sub), <alt-F11> opens the Visual Basic Editor.
In the Project Explorer window, select ThisWorkbook under the relevant VBA Project.
Paste the code below into the window that opens.
Option Explicit
'change text function date code
Private Sub Workbook_Open()
Dim yrCode As String, mnthCode As String, dyCode As String
Dim dtCode As String
Dim nM As Name
With Application
yrCode = WorksheetFunction.Rept(.International(xlYearCode), 4)
mnthCode = WorksheetFunction.Rept(.International(xlMonthCode), 2)
dyCode = WorksheetFunction.Rept(.International(xlDayCode), 2)
End With
'Can only add a name if it is absent
For Each nM In ThisWorkbook.Names
If nM.Name = "dtFormat" Then
nM.Delete
Exit For
End If
Next nM
dtCode = yrCode & mnthCode & dyCode
ThisWorkbook.Names.Add _
Name:="dtFormat", _
RefersTo:="=""" & dtCode & """", _
Visible:=False
End Sub
I am trying to change an excel workbook link, but by constructing a new workbook link using dates in cells (the naming convention is always the same, except that the date changes)
I know that to change an excel workbook link (using VBA), all I have to do is:
ActiveWorkbook.ChangeLink "c:\excel\book1.xls", _
"c:\excel\book2.xls", xlExcelLinks
But what if I want to construct a new workbook link using different figures in cells?
This is what I have been trying:
ActiveWorkbook.ChangeLink "part of file name" & Range("N2") & ".xlsx"", _
"part of file name" & Range("N4") & ".xlsx"", xlExcelLinks
Range N2 is the previous week's date, and Range N4 is current week's dates.
But whenever I do this I get the following message "Compile error: Syntax error"
Any ideas on what I can do? Really want to be able to construct file name as it'll mean I can automate a LOT of what I do...lol
Thank you in advance for any guidance on this!
First, if you want to include " in string you must double it, so:
ActiveWorkbook.ChangeLink """part of file name" & Range("N2") & ".xlsx""", _
"""part of file name" & Range("N4") & ".xlsx""", xlExcelLinks
this was your error reason
Also you should refer to .Value property od Range object, and if cells you are using contains dates the safest way would be using Format(Range("N4").Value, "yyyymmdd")
The code below (given to me) works but I don’t understand the Values (line line 9). I keep getting error messages when I try to add more cells. Im new to VBA could someone add more fields so I can better understand, such as:
C5 into Ingredient2
C6 into Ingredient3
C7 into Ingredient4
L5 into Lot2
L6 into Lot3
L7 into Lot4
and a whole bunch more.
The values below are arbitrary just for testing. Please tack a look at my spreadsheet
The spreadsheet I am using is here: https://mega.co.nz/#!2cZ3kKAZ!BL-HJM...1tZewrw-CH-1Dc
Sub Button10_Click()
Dim strSQL As String
Set appAccess = CreateObject("Access.Application")
appAccess.Visible = False
strSQL = "INSERT INTO [Finished Batches] ([Production Date],[Lot_Number],[Ingredient1],[Amount1]) " & _
"VALUES (#" & Range("C5") & "#,'" & Range("D5") & "','" & Range("F23") & _
"'," & Range("G23") & ")"
With appAccess
.OpenCurrentDatabase ("C:\users\jay neuh\desktop\database\ss database.mdb")
.DoCmd.RunSQL strSQL, dbFailOnError
.CloseCurrentDatabase
End With
Set appAccess = Nothing
End Sub
Thanks for any help
Access is using a SQL language to handle data, in this case an INSERT command.
An INSERT commands looks like this:
INSERT INTO [my_table_name] ([my_string_column],[my_number_column],[my_date_column])
VALUES ('my text value', 42, #2015-01-31#)
Notice a few things:
the number of columns must be the same as number of values
if you are passing text, it needes to be wrapped in single quotes
if you are passing a date, it needs to be wrapped in hash signs
if you are passing a number, it must not be wrapped
You didn't mention what kind of error you were getting, but if in your Access database the field [Lot_Number] is actually of a numeric type, wrapping it in single quotas like in your example could be the problem.
You should check Introduction to Access SQL
EDIT: You should always use
Debug.Print strSQL
to preview your SQL statement, you'll be more likely to spot if you have a syntax error somewhere in the code, like a missing comma, single quote or a bracket.
I was wondering if it would be possible to call out several items from a single cell? I am very limited in space so having one cell with all the information would be a great benefit. for example if i had a single cell that had "Dog,2,5,8" the program could run the program could tell what it meant and run my calculation? Thank you for your help in advance.
As #Tim Williams said, Split() will do what you want.
Sub splitcell()
Dim TestString As String
TestString = CStr(Range("A3").Value)
Dim TestArray() As String
TestArray() = Split(TestString, ", ")
MsgBox (TestArray(0) & vbNewLine & TestArray(1) & vbNewLine & TestArray(2) & vbNewLine & TestArray(3))
End Sub
Macro will give you this message box:
Now you can just iterate through the TestArray() with a For loop and do your logic.
See MSDN documentation here
And here is another post
I have an .xltm template spreadsheet that I'm wondering if I can get a macro to populate the "save as" file name based on cell data, is this possible?
There are over 50 people who will have this spreadsheet, it's more of a form, and we are trying to figure out a way to keep the filenames uniform. I know there is the ThisWorkbook.BeforeSave, but I'm not really having any luck there. I just need it to make a file named something like $A$1 R $B$1 T $B$3.xlsx
Any ideas on how to do this?
Sure.
Sub SaveMyWorkbook()
Dim strPath As String
Dim strFolderPath as String
strFolderPath = "C:\"
strPath = strFolderPath & _
Sheet1.Range("A1").Value & "R" & _
Sheet1.Range("B1").Value & "T" & _
Sheet1.Range("B3").Value & ".xlsx"
ActiveWorkbook.SaveAs Filename:=strPath
End Sub
EDIT: After you clarified your question in your comment below, I can now safely say that the answer is: No, what you are asking is not possible.
What is possible is to put a big, fat command button on your sheet that says "Press me to save", and have that button call the above Sub. You can set a fixed folder, as in the example above, or have the user pick a folder using the FileDialog object (or the GetSaveAsFilename function, but then the user will be able to change the suggested filename, so less safe).