Hide all Graphs and show them based on ComboBox Selection - vba

I am fairly new to VBA, and have been searching the forum for one particular code.
I have a workbook with multiple different graphs. I also have a UserForm with a popbox box where the User can select the city and click enter. What I would like to do is Hide all the Worksheets with the graphs, and when the user selects one particular city, I would like Excel to show that particular Graph. For Example, The user Selects Vancouver, and Excel will show the worksheet Pertaining to Vancouver.
I would also Like to Have a done button, where if the user clicks that, it will go back to the sheet where the UserForm is.
Private Sub ENT_Click()
'Error if you do not select the City
If Me.City.Value = "" Then
MsgBox "Please Select a City", vbExclamation, "Select a City"
Me.City.SetFocus
Exit Sub
End If
If Me.City.Value = "Mississauga" Then
Sheets("Sheet4").Visible = True
Sheets("Sheet4").Activate
End If
End Sub
Every time I run the code, with the Worksheets hidden, Or UnHidden, I keep getting a Subscript out of Range error.
Thank you again for the help

It was an error with the way I was labeling my Worksheet. The Code should have actually been:
Private Sub ENT_Click()
'Error if you do not select the City
If Me.City.Value = "" Then
MsgBox "Please Select a City", vbExclamation, "Select a City"
Me.City.SetFocus
Exit Sub
End If
'Change "Mississauga" to the item in your ComboBox
If Me.City.Value = "Mississauga" Then
Worksheets(4).Visible = True
Worksheets(4).Activate
End If
End Sub

Related

Transfer multiple selections from ListBox to Word bookmark

I am a bit of a novice when it comes VBA (and coding generally) and apologize in advance if this isn't possible. I have created a userform for users to input different pieces of information (name, address, etc.). When the user clicks the "OK" command button, the values of the differing text boxes appear at various bookmarks within the Word document. For instance, TextBox 1 is dedicated to name, TextBox2 is for the address, etc.
Additionally on the userform, I have added a ListBox that lists various types of data. Ideally, I'd like to have the multiple selections from that ListBox appear at the bookmark in Word on the same line together after clicking the "OK" command button on the userform. It transfers the values to the document (where the cursor is blinking), but I can't figure out how to assign it to the bookmark.
You can see below that I'm assigning the values of the textboxes to the bookmarks and essentially want to do the same with the ListBox.
The ListBox is filled at initialization with the following code.
With ListBox1
.AddItem "name"
.AddItem "address"
.AddItem "secondary address"
.AddItem "Social Security Number"
.AddItem "financial account number"
.AddItem "date of birth"
.AddItem "email address"
End With
End Sub
Private Sub CommandButton1_Click()
Dim DataSub1 As Range
Set DataSub1 = ActiveDocument.Bookmarks("DataSub1").Range
DataSub1 = Me.DataSub1.Value
Dim dAddress As Range
Set dAddress = ActiveDocument.Bookmarks("dAddress").Range
dAddress.Text = Me.dAddress.Value
Dim ii As Integer
For ii = 0 To ListBox1.ListCount - 1
If ListBox1.Selected(ii) Then
Selection.Text = ListBox1.List(ii)
Selection.MoveRight
Selection.TypeParagraph
End If
Next ii
Application.ScreenRefresh
Me.Repaint
Me.Hide
End Sub
For example:
Dim ii As Integer, listText As String
For ii = 0 To ListBox1.ListCount - 1
If ListBox1.Selected(ii) Then
listText = listText & ListBox1.List(ii) & vbCr
End If
Next ii
ActiveDocument.Bookmarks(bmkName).Range.Text = listText

Form closes but the written conditions therein not implemented

I have the following two codes on a button: The (first code) aims to submit value of option button into worksheet cell:
For Each FormControl In Me.Controls
'Check only OptionButtons
If TypeName(FormControl) = "OptionButton" Then
'Check the status of the OptionButton
If FormControl.Value = True Then
'Set a variable equal to the Caption of the selected OptionButton
OptionButtonValue = FormControl.Caption
'We found the selected OptionButton so exit the loop.
Exit For
End If
End If
Next
'Store input in the worksheet
Sheets("Answer Sheet").Range("E80").Value = OptionButtonValue
To ensure an option button is selected before proceeding to next form, i have
the 'following code (second code):
Dim cnt As Integer
For Each ctl In Me.Controls
If TypeName(ctl) = "OptionButton" Then
If ctl.Value = True Then cnt = cnt + 1
End If
Next ctl
If cnt = 0 Then MsgBox "Hello " & CStr(ThisWorkbook.Sheets("AccessReg").Range("D630").Value) & ", you
have not selected an answer! Please select an answer to proceed to next question. Thank you.",
vbInformation, "Please select an answer!" Else ScoreBoards.Show
Unload Me
MY CHALLENGES
Both codes above exists in my forms i.e. questions 1, 2, 3,...respectively, but can't seem to get the second code (that, which ensures an option button is selected before next form can be opened) to work by adding 'unload me' to the end of it, yet i want the form closed before proceeding to next. Adding 'unload me', pop-up the msgbox (which tells me to select an answer) but when i clicked okay on the msgbox, it closes the form (Question1) instead of returning me to same form to ensure an answer is clicked, then proceed to next form (Question2). However, when i remove the 'unload me', things work fine i.e. the msgbox popup when selection not made, returns to same form when okay on msgbox is clicked, and opens next form when selection made.
What i really want is: i want the second code above (which ensures an option button is selected before next form can be opened) to work as programmed and each form closed before proceeding to the next form.
Thank you in advance
PS:
The concept summary is:
On a userform (Question1), select an option button and submit the value to worksheet
Ensure an option button is selected:
if selected and button clicked, the next form(being Question2) should open
if not selected and button clicked, the msgbox (which tells me to select an answer), should popup
clicking okay on the msgbox, should return me to same form (Question1) so that i can select an option and proceed.
Try adapting your second code in the next way, please:
If cnt = 0 Then MsgBox "Hello " & _
CStr(ThisWorkbook.Sheets("AccessReg").Range("D630").Value) & ", you have not selected an answer! Please select an answer to proceed to next question. Thank you.", vbInformation, "Please select an answer!"
Exit For
Else
ScoreBoards.Show
Unload Me
End if

How to make to click in button and open form releated in value in combox in Ms Access?

I open a form as shown here: How to use combobox to open specific form in Ms Access
But I still have a problem. When I click the button, it opens two forms immediately. What I want to do, when the value in the combobox is 1, is to open only form 1, and when the value in the combobox is 2, open form 2.
Should I correct something in my macro? How do I do it?
I personally don't like this macro builder.
Here's a proper way to do it with VBA.
Select your combobox and go in the events. In the After Update event, remove your macro code and add VBA code instead, using the option "Code Builder"
Your code should be this :
Private Sub ComboBoxColor_AfterUpdate()
On Error GoTo Err_Handler
Dim strForm As String
Select Case ComboBoxColor.Value
Case "Color1"
strForm = "Form1"
Case "Color2"
strForm = "Form2"
Case "Color3"
strForm = "Form3"
Case Else
MsgBox "I don't know what to do with this combobox value"
GoTo Exit_Sub
End Select
DoCmd.OpenForm strForm, acNormal
Exit_Sub:
Exit Sub
Err_Handler:
MsgBox Err.Description, vbExclamation, "Error " & Err.Number
Resume Exit_Sub
End Sub
Adapt ComboBoxColor with the proper name, and Form1/Form2/Form3 with the names of your forms of course (and the Color1/Color2/color3 if needed)

Select combobox if wrong item selected

I have an MS-Word 2013 document with several (legacy) formfields; some text boxes, some comboboxes. The first list item on all of the comboboxes is "(select one)" to let the end user know they need to make a selection (I did not draft or design this document, I've just been asked to write the VBA code for it). So I coded each to give a simple VBA message box, ran on exit, if that first selection was not changed, for example:
Public factor1 As Integer
Sub MyFormFieldFactor1()
If ActiveDocument.FormFields("cbofactor1").Result = "(select one)" Then
MsgBox "You must select either Yes or No."
Exit Sub
End If
If ActiveDocument.FormFields("cbofactor1").Result = "Yes" Then
factor1 = 1
Else
factor1 = 0
End If
End Sub
The word document automatically goes to the next formfield when you click ok on the message box. Through VBA, I want it to stay on the current formfield when "(select one)" is chosen. Bonus points if it stays on the current field and pulls up the list selection automatically.
Will this work:
If ActiveDocument.FormFields("cbofactor1").Result = "(select one)" Then
MsgBox "You must select either Yes or No."
ActiveDocument.FormFields("cbofactor1").SetFocus()
Exit Sub
End If
You can auto drop the list with something like:
SendKeys "%{down}", True
DoEvents
Full code:
If ActiveDocument.FormFields("cbofactor1").Result = "(select one)" Then
MsgBox "You must select either Yes or No."
ActiveDocument.FormFields("cbofactor1").SetFocus()
SendKeys "%{down}", True
DoEvents
Exit Sub
End If

Combo box to list Macros

I want when you hit 'send' you are presented with a form. This I am developing in Outlook 2010.
Is there a way to populate a combo-box with a list of Macros?
Public Sub Confidential()
Application.ActiveInspector.CurrentItem.Sensitivity = olConfidential
Application.ActiveInspector.CurrentItem.Save
Set MsgSub = Outlook.Application.ActiveInspector.CurrentItem
Set objMail = Outlook.Application.ActiveInspector.CurrentItem
Subject = MsgSub.Subject
MsgSub.Subject = Subject + " - [CONFIDENTIAL]"
Email = objMail.HTMLBody
info = " <html> <body> <FONT color=#666666> <font-size: 11px> <p></p> AUTO TEXT: This message has been marked as 'CONFIDENTIAL' please treat it as such </body> </font> </html>"
objMail.HTMLBody = Email + info
End Sub
Private Sub Sens_DropButtonClick()
Sens.AddItem "Confidential()"
Sens.AddItem "Normal()"
End Sub
Public Sub Send_Click()
Set objMail = Outlook.Application.ActiveInspector.CurrentItem
objMail.Send
End Sub
Would I be right in thinking that this is a public sub?
My goal is when you hit the 'send' button a form will appear with a dropdown box, this has 4 options which are the sensitivity options you can use with the emails, except I have created them as macros and added code on them (to add to subject and footer of message) but I wont it so a user is forced to make a selection, hence why I am creating this form instead of having the 4 buttons.
My goal is when you hit the 'send' button a form will appear with a dropdown box, this has 4 options which are the sensitivity options you can use with the emails, except I have created them as macros and added code on them (to add to subject and footer of message) but I wont it so a user is forced to make a selection, hence why I am creating this form instead of having the 4 buttons. – Rsmithy 36 mins ago
If I understand you correctly, Yes it is possible to do what you want. See this Example
Let's say you have a userform with 4 options A,B,C and D and the userform code is
Private Sub UserForm_Initialize()
ComboBox1.AddItem "A"
ComboBox1.AddItem "B"
ComboBox1.AddItem "C"
ComboBox1.AddItem "D"
End Sub
Private Sub CommandButton1_Click()
lstNo = ComboBox1.ListIndex
Unload Me
End Sub
Next Paste this in a module
Public lstNo As Long
and this in the ThisOutlookSession
Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
UserForm1.Show
MsgBox "user chose " & lstNo & "from combo"
Select Case lstNo
Case -1
'User didn't select anything in the combo
Case 0
'User selected option 1 in the combo
Case 1
'User selected option 2 in the combo
Case 2
'User selected option 3 in the combo
Case 3
'User selected option 4 in the combo
End Select
End Sub
Replace the above comments in the Select Statement` with the Macro Names that you want executed depending on the user choice.
SNAPSHOTS IN ACTION
And this is what you get when you select the Option D (ListIndex 3)
FOLLOWUP
Dim email As String, info As String
Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
UserForm1.Show
Select Case lstNo
Case -1
'User didn't select anything, default will be used
Case 0
With Item
.Sensitivity = olNormal
.Save
End With
Case 1
With Item
.Sensitivity = olPersonal
.Save
End With
Case 2
With Item
.Sensitivity = olPrivate
.Save
End With
Case 3
With Item
.Sensitivity = olConfidential
.Subject = .Subject & " - [CONFIDENTIAL]"
Email = .HTMLBody
info = " <html> <body> <FONT color=#666666> <font-size: 11px> <p></p> AUTO TEXT: " & _
"This message has been marked as 'CONFIDENTIAL' please treat it as such </body> </font> </html>"
.HTMLBody = Email & info
.Save
End With
End Select
End Sub
Is there a way to populate a combo-box with a list of Macros?
Not in Outlook, I'm afraid. Programmatic access to the VBA IDE isn't supported in Outlook due to the possibility of spreading viruses or conducting malicious activity via email.
See VBA Extensibility in Outlook for reference.
The closest you can come is by programmatically displaying the "Run Macro" dialog, like this:
ActiveExplorer.CommandBars.FindControl(,186).Execute
However I'm not sure if this is available in Outlook 2010.
But showing a dialog box can't possibly be your goal. Maybe if you explain what your goal is, someone can suggest a better way of reaching it that doesn't require populating a combo box with a list of macro names.