The following code looks for certain text strings in a column and gives a msg box whenever something matches. The code looks for more than one text string, so if I have "X" AND "y" in one column and the code looks for both text strings, then two msg boxes will appear. I only want the first msg box to show and hide the rest. Is there a way to do this?
In other words, code looks for multiple text strings, pops up msg boxes if text strings match. More than one text string will definitely match, but I want only the first box to appear, and hide the rest.
Thanks
Private Sub Worksheet_Change(ByVal Target As Range)
Dim icounter As Long
Dim icounter1 As Long
Dim lastrow As Long
Dim MSG As String, ans As Variant
For icounter = 2 To 31
If Cells(icounter, 2) = "Job Code Name" Then
MsgBox ("Please note you may need to add in additional attributes under this field" & vbNewLine & vbNewLine & "1. PS Group" & vbNewLine & "2. Level" & vbNewLine & "3. Box Level" & vbNewLine & vbNewLine & "Please add in these additional fields as needed")
ElseIf Cells(icounter, 2) = "Personnel Area" Then
MsgBox ("Please note you may need to add in additional attributes under this field" & vbNewLine & vbNewLine & "1. Personnel Subarea" & vbNewLine & vbNewLine & "Please add in these additional fields as needed")
ElseIf Cells(icounter, 2) = "Line of Sight" Then
MsgBox ("Please note you may need to add in additional attributes under this field" & vbNewLine & vbNewLine & "1. 1 Up Line of Sight" & vbNewLine & vbNewLine & "Please add in these additional fields as needed")
ElseIf Cells(icounter, 2) = "Title of Position" Then
MsgBox ("Please note you may need to add in additional attributes under this field" & vbNewLine & vbNewLine & "1. Job Code Name" & vbNewLine & "2. PS Group" & vbNewLine & "3. PS Level" & vbNewLine & "4. Box Level" & vbNewLine & vbNewLine & "Please add in these additional fields as needed")
ElseIf Cells(icounter, 2) = "Company Code Name" Then
MsgBox ("Please note you may need to add in additional attributes under this field" & vbNewLine & vbNewLine & "1. Cost Center" & vbNewLine & "2. Line of Sight" & vbNewLine & "3. 1 Up Line of Sight" & vbNewLine & "4. Personnel Area" & vbNewLine & "5. Location" & vbNewLine & vbNewLine & "Please add in these additional fields as needed")
ElseIf Cells(icounter, 2) = "Function" Then
MsgBox ("Please note you may need to add in additional attributes under this field" & vbNewLine & vbNewLine & "1. Sub Function" & vbNewLine & vbNewLine & "Please add in these additional fields as needed")
Else
End If
Next icounter
End Sub
If I understand your question, you could use Select Case instead of all the If...ElseIf stuff. Just reading the comments. Apparently you want to exit the For loop as well so add Exit For.
Private Sub Worksheet_Change(ByVal Target As Range)
Dim icounter As Long
Dim icounter1 As Long
Dim lastrow As Long
Dim MSG As String, ans As Variant
For icounter = 2 To 31
Select Case Cells(icounter, 2)
Case "Job Code Name"
MsgBox ("Please note you may need to add in additional attributes under this field" & vbNewLine & vbNewLine & "1. PS Group" & vbNewLine & "2. Level" & vbNewLine & "3. Box Level" & vbNewLine & vbNewLine & "Please add in these additional fields as needed")
Exit For
Case "Personnel Area"
MsgBox ("Please note you may need to add in additional attributes under this field" & vbNewLine & vbNewLine & "1. Personnel Subarea" & vbNewLine & vbNewLine & "Please add in these additional fields as needed")
Exit For
Case "Line of Sight"
MsgBox ("Please note you may need to add in additional attributes under this field" & vbNewLine & vbNewLine & "1. 1 Up Line of Sight" & vbNewLine & vbNewLine & "Please add in these additional fields as needed")
Exit For
Case "Title of Position"
MsgBox ("Please note you may need to add in additional attributes under this field" & vbNewLine & vbNewLine & "1. Job Code Name" & vbNewLine & "2. PS Group" & vbNewLine & "3. PS Level" & vbNewLine & "4. Box Level" & vbNewLine & vbNewLine & "Please add in these additional fields as needed")
Exit For
Case "Company Code Name"
MsgBox ("Please note you may need to add in additional attributes under this field" & vbNewLine & vbNewLine & "1. Cost Center" & vbNewLine & "2. Line of Sight" & vbNewLine & "3. 1 Up Line of Sight" & vbNewLine & "4. Personnel Area" & vbNewLine & "5. Location" & vbNewLine & vbNewLine & "Please add in these additional fields as needed")
Exit For
Case "Function"
MsgBox ("Please note you may need to add in additional attributes under this field" & vbNewLine & vbNewLine & "1. Sub Function" & vbNewLine & vbNewLine & "Please add in these additional fields as needed")
Exit For
End Select
Next icounter
End Sub
I would recommend you another refactoring in your code. Since you are using a Worksheet event, every time you change a cell content it will fire that event, so I believe you add Application.EnableEvents = False before your For loop and Application.EnableEvents = True after it.
Private Sub Worksheet_Change(ByVal Target As Range)
Dim icounter As Long
Dim icounter1 As Long
Dim lastrow As Long
Dim MSG As String, ans As Variant
Application.EnableEvents = False
For icounter = 2 To 31
Select Case Cells(icounter, 2)
Case "Job Code Name"
MsgBox ("Please note you may need to add in additional attributes under this field" & vbNewLine & vbNewLine & "1. PS Group" & vbNewLine & "2. Level" & vbNewLine & "3. Box Level" & vbNewLine & vbNewLine & "Please add in these additional fields as needed")
Exit For
Case "Personnel Area"
MsgBox ("Please note you may need to add in additional attributes under this field" & vbNewLine & vbNewLine & "1. Personnel Subarea" & vbNewLine & vbNewLine & "Please add in these additional fields as needed")
Exit For
Case "Line of Sight"
MsgBox ("Please note you may need to add in additional attributes under this field" & vbNewLine & vbNewLine & "1. 1 Up Line of Sight" & vbNewLine & vbNewLine & "Please add in these additional fields as needed")
Exit For
Case "Title of Position"
MsgBox ("Please note you may need to add in additional attributes under this field" & vbNewLine & vbNewLine & "1. Job Code Name" & vbNewLine & "2. PS Group" & vbNewLine & "3. PS Level" & vbNewLine & "4. Box Level" & vbNewLine & vbNewLine & "Please add in these additional fields as needed")
Exit For
Case "Company Code Name"
MsgBox ("Please note you may need to add in additional attributes under this field" & vbNewLine & vbNewLine & "1. Cost Center" & vbNewLine & "2. Line of Sight" & vbNewLine & "3. 1 Up Line of Sight" & vbNewLine & "4. Personnel Area" & vbNewLine & "5. Location" & vbNewLine & vbNewLine & "Please add in these additional fields as needed")
Exit For
Case "Function"
MsgBox ("Please note you may need to add in additional attributes under this field" & vbNewLine & vbNewLine & "1. Sub Function" & vbNewLine & vbNewLine & "Please add in these additional fields as needed")
Exit For
End Select
Next icounter
Application.EnableEvents = False
End Sub
I used Tim's answer but it fired whenever I made an edit to any cell in the sheet. To avoid this, I added this line of code before Select Case Cells(icounter, 2):
If Not Intersect(Target, Cells(icounter, 2)) Is Nothing Then
This edit has made the msg box pop up only when cells change to the string text I inputted, i.e. company code name
Related
I am trying to make sure data is entered into the InputBox at each stage otherwise I want the message to pop-up and the entry to end.
I have a MsgBox that pops up if nothing is entered but I want to add an option to have the Sub remain at the InputBox so the user can still enter the data.
I've tried vbRetryCancel and either the Retry will loop and then never cancel (and I have to force Outlook to close to end the macro) or the Cancel will loop and it will never retry.
If I could make it so the InputBox would not advance with "Ok" until data is entered or "cancel" was clicked that would work too but I would still need a MsgBox if nothing is entered to alert the user.
Sub Request_Tracking()
Dim objMsg As MailItem
Dim tasksource As String
Set objMsg = Application.CreateItem(olMailItem)
'requests input of the user with various options
tasksource = InputBox("Please select task receipt method:" & _
vbCrLf & vbTab & "1 - Phone call" & _
vbCrLf & vbTab & "2 - Email" & _
vbCrLf & vbTab & "3 - Instant Message" & _
vbCrLf & vbTab & "4 - Desk walk-up" & _
vbCrLf & vbTab & "5 - Miscellaneous", "Option Chooser")
'if response is blank then ends the task
If tasksource = vbNullString Then
MsgBox "Nothing was entered. Please re-enter your request.", vbOKOnly, "Error!"
Exit Sub
End If
Select Case tasksource
Case "1"
tasksource = "Phone call"
Case "2"
tasksource = "Email"
Case "3"
tasksource = "Instant Message"
Case "4"
tasksource = "Desk walk-up"
Case "5"
tasksource = "Miscellaneous"
Case Else
tasksource = tasksource
End Select
'various input boxes for data needed, if response is blank then ends the task without send
strName = InputBox("Requestor Name:")
If strName = vbNullString Then
MsgBox "Nothing was entered. Please reattempt your request.", vbOKOnly, "Error"
Exit Sub
End If
strCoreEvent = InputBox("Task Description:")
If strCoreEvent = vbNullString Then
MsgBox "Nothing was entered. Please reattempt your request.", vbOKOnly, "Error"
Exit Sub
End If
strUnitNum = InputBox("Number of units:")
If strUnitNum = vbNullString Then
MsgBox "Nothing was entered. Please reattempt your request.", vbOKOnly, "Error"
Exit Sub
End If
strTime = InputBox("Processing Time:")
If strTime = vbNullString Then
MsgBox "Nothing was entered. Please reattempt your request.", vbOKOnly, "Error"
Exit Sub
End If
'configures message being sent, sents to WFM box and formats the body font and various line breaks and then sends
With objMsg
.To = "tracking#test.com"
.Subject = "Request Received"
.HTMLBody = "<body style=font-size:11pt;font-family:Arial><b>For request tracking; please assign to me.</b>" & "<p>" & tasksource & " request from " & strName & ": " & strCoreEvent & "<br />Number of units: " & strUnitNum & "<br />Processing time: " & strTime & "</p></body>"
.Recipients.ResolveAll
.Send
End With
End Sub
Your can try something like this:
While tasksource = " "
tasksource = InputBox("Please select task receipt method:" & _
vbCrLf & vbTab & "1 - Phone call" & _
vbCrLf & vbTab & "2 - Email" & _
vbCrLf & vbTab & "3 - Instant Message" & _
vbCrLf & vbTab & "4 - Desk walk-up" & _
vbCrLf & vbTab & "5 - Miscellaneous", "Option Chooser", " ")
If tasksource = vbNullString Then
Exit Sub
ElseIf (tasksource = " ") Then
tasksourceCancel = MsgBox("Nothing was entered. Please re-enter your request. Or press Cancel to leave!", vbOKCancel, "Error!")
If tasksourceCancel = 2 Then
Exit Sub
End If
End If
Wend
It forces the user to have Input, if user clicks OK the return/default value will be " " (space) will loop, if the user clicks cancel the value will be vbNullString and it will exit.
Try something like this
Do
tasksource = InputBox("Please select task receipt method:" & _
vbCrLf & vbTab & "1 - Phone call" & _
vbCrLf & vbTab & "2 - Email" & _
vbCrLf & vbTab & "3 - Instant Message" & _
vbCrLf & vbTab & "4 - Desk walk-up" & _
vbCrLf & vbTab & "5 - Miscellaneous", "Option Chooser", 1)
If Not (tasksource >=1 And tasksource <=5) Then
optionvar = MsgBox("Incorrect selection. Do you want to re-enter your request?", vbYesNo, "Input required")
If optionvar = 6 Then 'if yes is pressed
tasksource = vbNullString
Else
tasksource = optionvar
End If
End If
Loop While tasksource = vbNullString
My code works perfectly if the selected cell you tell it to delete is filled out with one of the part #'s from the drop down list.
But if you type in a new or custom # then want to delete that part # later the macro returns a
Type mismatch (Error 13)
Sub DeleteRows()
Application.ScreenUpdating = False
On Error GoTo whoa
If ActiveCell.Row <= 8 Then
MsgBox "Ooops!" & vbNewLine & _
vbNewLine & "Please select a Part Number"
ElseIf MsgBox("Are you sure you want to delete this part?" & vbNewLine & _
vbNewLine & _
ActiveCell.EntireRow.Cells(1, "A").Value & vbNewLine & _
ActiveCell.EntireRow.Cells(1, "B").Value & vbNewLine & _
"QTY: " & ActiveCell.EntireRow.Cells(1, "M").Value, _
vbYesNo) = vbYes Then
ActiveCell.Resize(3, 1).EntireRow.Delete
End If
Application.ScreenUpdating = True
Exit Sub
whoa:
MsgBox "Please select a number from the drop down list" & vbNewLine & _
"then run the delete command again.", vbInformation, Err.Description
End Sub
How can I get rid of that error and still have it delete the 3 rows (each part is 3 rows) if I enter a custom #.
Thanks
In my Application i need to display some short notes in label.
So i tried like this
lable1.Text="HELP : " & vbNewLine & vbNewLine & "1. You can Remove or
Replace existing Main Icons" & vbNewLine & vbNewLine _
& "2. If you want to remove any buttons, Right click on Icon and
select UNPIN or select the " _
& " Icon and press delete button from key borad" & vbNewLine &
vbNewLine & "3. Drag the needed button from top menu and drop it on
empty space in Icons." & vbNewLine & vbNewLine & "4. If Reports
having sub reports, You can drag and drop the sub reports only."
If the points in came to second line its start from 2 but i need all letter start same level like this
1 . aaaa
2. bbbbb
vvvvv.
and Help must have underline. How to do it. Can say which control good for this can you give some example also.
am using VB.Net 2008
I dont think you can set tabs in a label, so you might need to manually space the text using a monospaced font. But in a richtextbox, you can do this.
RichTextBox1.Text = "HELP : " & vbNewLine & vbNewLine &
"1." & vbTab & "You can Remove or Replace existing Main Icons" & vbNewLine & vbNewLine &
"2." & vbTab & "If you want to remove any buttons, Right click on Icon and" & vbNewLine _
& vbTab & "select UNPIN or select the Icon and press delete button from keyboard" & vbNewLine & vbNewLine &
"3." & vbTab & "Drag the needed button from top menu and drop it on" & vbNewLine _
& vbTab & "empty space in Icons." & vbNewLine & vbNewLine &
"4." & vbTab & "If Reports having sub reports, You can drag and drop the sub reports only."
RichTextBox1.SelectAll()
RichTextBox1.SelectionTabs = {20}
RichTextBox1.DeselectAll
I have a string that is read from a cell in Excel, which is something like this:
""Horace Lai" & vbNewLine & Date"
or this
"Chr(149) & "level 1" & vbNewLine & Chr(9) & Chr(149) & "level 2" & Chr(149) & "level 1" & vbNewLine & Chr(9) & Chr(149) & "level 2" & vbNewLine & Chr(9) & Chr(9) & Chr(149) & "level 3""
I would like to be able to evaluate these strings so that ""Horace Lai" & vbNewLine & Date" becomes:
Horace Lai
2014-06-20
So far I have tried ScriptControl without success. It doesn't seem to like it when I have double quotes for the string.
Sub testing()
Dim sc As ScriptControl
Set sc = CreateObject("ScriptControl")
sc.Language = "JScript"
MsgBox sc.Eval(""Horace Lai" & vbNewLine & Date")
End Sub
The MsgBox line becomes red and I cannot run it.
Any ideas? Thanks
-Horace
You specify JScript, instead:
Dim test As String, result As String
test = "Chr(149) & ""level 1"" & vbNewLine & Chr(9) & Chr(149) & ""level 2"" & Chr(149) & ""level 1"" & vbNewLine & Chr(9) & Chr(149) & ""level 2"" & vbNewLine & Chr(9) & Chr(9) & Chr(149) & ""level 3"""
Dim sc As Object
Set sc = CreateObject("ScriptControl")
sc.Language = "VBScript"
result = sc.Eval(test)
Debug.Print result
•level 1
•level 2•level 1
•level 2
•level 3
Firstly load the name into a variable i.e. name = """Horace Lai""" (this will contain the quote marks)
Then use the replace function to search for quotes in name name = Replace(name,"""","")
Then run the msgbox of MsgBox (name & vbNewLine & Date)
This should remove the extra " marks that you face
I've been trying to adapt the method shown here: http://support.microsoft.com/kb/246299 so that I can create a command button in word which will save the document and remove itself when clicked. I've been unable to figure out how to change the position of the button from the default of the top left of the first page however. Ideally I'd like the button to be generated at the end of the document and be centre aligned, or otherwise placed at the cursor position.
Any advice would be very much appreciated :)
Thank You.
My VB.NET project code so far:
Dim shp As Word.InlineShape
shp = wrdDoc.Content.InlineShapes.AddOLEControl(ClassType:="Forms.CommandButton.1")
shp.OLEFormat.Object.Caption = "Save To Disk"
shp.Width = "100"
'Add a procedure for the click event of the inlineshape
Dim sCode As String
sCode = "Private Sub " & shp.OLEFormat.Object.Name & "_Click()" & vbCrLf & _
"ActiveDocument.SaveAs(""" & sOutFile & """)" & vbCrLf & _
"On Error GoTo NoSave" & vbCrLf & _
"MsgBox ""Document Saved Successfully""" & vbCrLf & _
"Dim o As Object" & vbCrLf & _
"For Each o In ActiveDocument.InlineShapes" & vbCrLf & _
"If o.OLEFormat.Object.Name = ""CommandButton1"" Then" & vbCrLf & _
"o.Delete" & vbCrLf & _
"End If" & vbCrLf & _
"Next" & vbCrLf & _
"Exit Sub" & vbCrLf & _
"NoSave:" & vbCrLf & _
"MsgBox ""Document Failed To Save""" & vbCrLf & _
"End Sub"
wrdDoc.VBProject.VBComponents("ThisDocument").CodeModule.AddFromString(sCode)
wrdApp.Visible = True
As long as everything else is working just set the shp.left = 250 and shp.top = 1200
etc etc.
Just like in VB when you place a button. For more details on the exact calls you should reference this page: http://msdn.microsoft.com/en-us/library/office/hh965406%28v=office.14%29.aspx
But say to center a button you can set left to be the (doc.width - shape.width)
But word buttons allow for much more complex styling and setup.