VBA in Excel - text box - vba

I am working on a spreadsheet and added a button w/ VBA to filter by status of line items in rows. There are 3 different status types available. I have it that the user selects the button and gets a input box to enter one of the statuses (with * = ALL status). The status entered in the input box is inserted in a field to show which filter is selected. My question is (maybe this is a 2-parter): If the user select * (for all), how can I get the text "ALL" inserted into the field? Or, instead of a input box, is there VBA could for a pull down/ option selection of all 3 status options plus "ALL"? Code used is below. Thank you! Kathleen
Private Sub CommandButton1_Click()
ActiveSheet.Unprotect Password:="1234"
Dim strInput As String
strInput = InputBox("Please enter the status to Filter - enter * for ALL rows")
Selection.AutoFilter
ActiveSheet.Range("A7:S1006").AutoFilter Field:=13, Criteria1:=strInput
Range("F3").Value = strInput
ActiveSheet.Protect Password:="1234"
End Sub

Use the Replace function
strInput = Replace(InputBox("Please enter the status to Filter - enter * for ALL rows"), "*", "ALL")

Related

Hide all Graphs and show them based on ComboBox Selection

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

Populating a dropdown box with already-existing options in VBA?

I'm making an add records form for a spreadsheet of mine, and let's say that I want one of the controls to be a dropdown that is populated by unique entries under a certain column "type". However, I want to also make it such that the dropbox always has a initial option to "add new type" and upon such selection, it becomes a regular text box. How would I do this in VBA?
You cannot change a control type at run time. The easiest thing to do is create a combo box and a text box. Set the text box visibility to false. Then in the onchange event of the combo box your code will unhide the text box and hide the combo box. You will also need a save button so that when it is clicked it will add the option to the drop down, clear the text box, hide the text box, hide the button and unhide the drop down.
Okay, so here's my idea of how to tackle this.
Create 2 hidden elements (Visibility = False), one a TextBox and one a CommandButton.
Populate your ComboBox with the values from the sheet under column "type"
Add one more entry AddItem with wording such as "Add new item..."
When the user selects "Add new item...", change the Visibility of the TextBox & CommandButtons to True
When the user clicks the CommandButton, add the phrase to the column and add a new element to the ComboBox
I have created a mockup UserForm and code that does a little more than just this; it also styles the user entry to sentence case (consistency purposes) and checks to make sure the value isn't already in the column.
Excel Sheet with "type" column
UserForm with name labels
UserForm Code
Private Sub bAdd_Click()
Dim str As String
Dim rng As Range
Dim ro As Integer
'Makes sure there is an entry, adds it to the Sheet and then updates the dropdown
If Len(Me.tbNew) > 0 Then
'Converts user entry to "Sentance Case" for better readability
str = StrConv(Me.tbNew, vbProperCase)
'Finds out if the entry already exists
Set rng = Sheets(1).Range(Sheets(1).Cells(2, 1), Sheets(1).Cells(Sheets(1).Cells(Sheets(1).Rows.Count, 1).End(xlUp).Row, 1))
On Error Resume Next
Err.Number = 0
'Searches for duplicate; if found, then ListIndex of cbColor is modified without inserting new value (prevents duplicates)
ro = rng.Find(str, LookIn:=xlValues, LookAt:=xlWhole).Row
Debug.Print Err.Number
'Ensures a user doesn't add the same value twice
If Err.Number > 0 Then
Sheets(1).Cells(Sheets(1).Cells(Sheets(1).Rows.Count, 1).End(xlUp).Row + 1, 1) = str
Me.cbColor.AddItem StrConv(Me.tbNew, vbProperCase), Me.cbColor.ListCount - 1
Me.cbColor.ListIndex = Me.cbColor.ListCount - 2
Else
Me.cbColor.ListIndex = ro - 2
End If
'Resets and hides user form entries
Me.tbNew = vbNullString
Me.tbNew.Visible = False
Me.bAdd.Visible = False
End If
End Sub
Private Sub bClose_Click()
Unload Me
End Sub
Private Sub cbColor_Change()
'Visibility is toggled based on if the user selected the last element in the dropdown
Me.bAdd.Visible = Me.cbColor.ListIndex = Me.cbColor.ListCount - 1
Me.tbNew.Visible = Me.cbColor.ListIndex = Me.cbColor.ListCount - 1
End Sub
Private Sub UserForm_Initialize()
'Populate from the sheet
For a = 2 To Sheets(1).Cells(Cells(Sheets(1).Rows.Count, 1).End(xlUp).Row, 1).Row
Me.cbColor.AddItem Sheets(1).Cells(a, 1)
Next
'Add option for new type
Me.cbColor.AddItem "Add new type..."
End Sub

Debugging "Search for Name" Code in VBA

I have solid experience in C++ but am still getting used to the syntax of VBA and I think that's what's tripping me up in my code.
What I'm trying to do is have a button that asks the user for a name. If the name entered is in column B, then tell the user the name was found and select where it is (no problem with this). If the name is not found, then ask if the user wants to try another name (no problem with this, either).
Where I'm having trouble is with the "Cancel" buttons. At any time, I want the user to be able to hit "Cancel" and immediately stop the loop, but stay in the sub because I'll be adding to this later.
Here's the code:
Dim inputName As String
Dim row As Integer
Dim i As Integer
Dim tryAgainResponse As Integer
tryAgainResponse = vbOK
'Ask user for name they would like to replace'
inputName = InputBox("What is the name of the person you would like to find? (First Last)")
'Find the row that the name is located and tell the user where it is'
Do While tryAgainResponse = vbOK
For i = 1 To 1000
If Cells(i, 2).Value = inputName Then
MsgBox ("Found the name! It's located at cell B" & i & ".")
ActiveSheet.Cells(i, 2).Select
tryAgainResponse = 0
Exit Do
End If
Next i
tryAgainResponse = MsgBox("We didn't find the name you were looking for. Please try again.", vbOKCancel)
If tryAgainResponse = vbCancel Then
Exit Do
End If
inputName = InputBox("What is the name of the person you would like to find? (First Last)")
Loop
I've tried plenty of things, but the main error is when you hit cancel for the first MsgBox, it tells you the name was found in the first blank square.
Any help or suggestions would be greatly appreciated! This is my first VBA program, so it's not the prettiest, but it's definitely a lot of fun. Thanks!
I'm not sure if I'm understanding what you're asking for, and I can't comment for clarification, but I think your hang up is that when you click cancel on the INPUT box, your input box is returning a blank string, and the rest of your code is then finding a blank cell.
Use the Application.Input Method, declare your input string as a variant, and test if it is false. If it is, use an Exit Sub to exit the macro. You could also test if your input string = "" and then exit the macro if true with the code you have.
From MrExcel
There are 2 versions of InputBox in VBA.
The InputBox Function is called without an object qualifiier and returns the contents of the text box or a zero-length string ("") if the user clicks Cancel.
The InputBox Method is a member of the Application object, so it is called by using Application.InputBox. It returns the contents of the text box or False if the user clicks Cancel. It is more versatile than the InputBox Function because it has a Type argument which specifies the return data type.
The function InputBox() will return an empty string if cancelled. The empty string will compare equal to the first cell that is empty.
Here's the doc of the function: http://msdn.microsoft.com/en-us/library/6z0ak68w(v=vs.90).aspx

What VBA coding in a Userform can I use to find a cell based upon row & column content, and then update that cell?

I am a complete beginner to VBA, truely.
I am trying to create a user form that will update the number of tasks a person completes on a given date as listed on a spreadsheet. I envision the Userform having two bottons (which are hidden and appear as the conditions of the subroutine). I am working on a Mac, by the way, and I know that will have VBA coding implications for use on a PC och vice versa.
The example sheet is this:
The example Userform (a) is this:
For argument's sake, let say I want to update or input the number of tasks that Greg completed on the 7th of May (2013/05/07).
I would like for the Userform to proceed something like this:
Entering person and date:
Then, retrieving number of tasks for Greg on the 7th after button click:
Now, I want to input that I know Greg completed 6 tasks on the 7th and I click the second button (now visible and the first button hidden):
And the result in the spreadsheet:
I ought to input some code here, but my skills and the completeness of the code are wanting. But I will put in what I have:
Option Explicit
'Subroutine when clicking the first ("find") button
Private Sub btnfind_Click()
lbltasks.Vissible = True
txttasks.Visible = True
btnupdate.Visible = True
btnfind.Visible = False
'Defining variables
Dim pr01 As String
Dim dt01 As Date
Dim tsk01 As Integer
'Assigning variables to inputs
pr01 = txtperson.Text
dt01 = txtdate.Text
tsk01 = txttask.Text
'Looking for Name in column "A"
' ? ? ?
'Looking for inut Date in row "1"
' ? ? ?
'Retrieving the existing number of tasks according to name and date
'and showing number in the 'tasks' text input box in the user form
' ? ? ?
End Sub
'Subroutine when clicking the Second ("update") button
Private Sub btnupdate_Click()
'Paste updated Number of tasks in appropriate cells according to name and date
'The new number of tasks should over write what was there previously
' ? ? ?
End Sub
Thanks in advance for any and all help!
This should work. Please study it and use the macro record function in Excel to grasp a lot more:
Option Explicit
Public frmName As Variant 'store row of name
Public frmDate As Variant 'store column of date
'Subroutine when clicking the first ("find") button
Private Sub btnfind_Click()
'Defining variables
Dim pr01 As String
Dim dt01 As Date
Dim tsk01 As Integer
'Assigning variables to inputs
pr01 = UserForm1.TextBox1.Text
dt01 = UserForm1.TextBox2.Text
tsk01 = UserForm1.TextBox3.Text
'Looking for Name in column "A"
With ThisWorkbook.Worksheets("Sheet4")
frmName = .Columns("A").Find(pr01).Row
End With
'Looking for inut Date in row "1"
With ThisWorkbook.Worksheets("Sheet4")
frmDate = .Rows(1).Find(CDate(dt01)).Column
End With
If frmName Is Nothing Or frmDate Is Nothing Then
'not found
Exit Sub
End If
'Retrieving the existing number of tasks according to name and date
'and showing number in the 'tasks' text input box in the user form
With ThisWorkbook.Worksheets("Sheet4")
UserForm1.TextBox3.Text = .Cells(frmName, frmDate)
End With
End Sub
'Subroutine when clicking the Second ("update") button
Private Sub btnupdate_Click()
'Paste updated Number of tasks in appropriate cells according to name and date
'The new number of tasks should over write what was there previously
With ThisWorkbook.Worksheets("Sheet4")
.Cells(frmName, frmDate) = UserForm1.TextBox3.Text
End With
End Sub

copy row data from one sheet to one or more sheets based on values in other cells

I have a sheet with user details in columns A - C.
Columns D-H is distribution lists the users are subscribed too. (currently check box linked to cell to indicate which list(s) user is subscribed to)
A user can be subscribed to more than on list.
At the moment I can use filters which works ok to filter users for list x, copy the user info to another sheet, then next list filterd (used text for the list selection)
This does cause some problems between some of the users of this sheet.(those that don't know how to use filters)
I would like to create a new sheet for each list which gets populated automatically.
As a user is added/removed from a list, his details is automatically added/removed from the corresponding 'list sheet'.
This way no one can complain about the filters
At the same time they can then export the 'list sheet' they require to another xls doc or csv as required.
I have found various options on how to do this, but all of then had only one selection column. I thought I could alter some of the ranges etc etc in the sample code but all failed with the limited knowledge I have with VB.
Any suggestions on how to do this?
Thanks!
Please do not try and create two copies of your data. Keeping two versions of the same data in step is very, very difficult.
I believe your best option is to create a macro with which your users can select the filter they require.
You do not describe your data in much detail so I have imagined something like the following:
Name Addr Tele List1 List2 List3 List4 List5
John London 1234 x
Jane Paris 2345 x
Kevin Stockholm 3456 x
Mary Brussels 4567 x
Nigel Dublin 5678 x
Abby Athens 6789 x x x
Brian Rome 7890 x
Given the above layout, the following macro shows the sort of thing I would offer.
When the macro is executed, it displays an InputBox like this:
from which the user can select the filter required.
I hope this gives you some ideas.
Option Explicit
Sub SelectFilter()
Dim ColNum() As Variant
Dim InxList As Long
Dim ListName() As Variant
Dim Prompt As String
Dim ReturnValue As Variant
' Load ListName with user-friendly names for the lists
ListName = Array("Name list 1", "Name list 2", "Name list 3", _
"Name list 4", "Name list 5")
' Load ColNum with the column number for each list. The entries in ColNum
' must be in the same sequence as the entries in ListName. Column "A" is
' column 1, column "B" is column 2 and so on.
ColNum = Array(4, 5, 6, 7, 8)
' Combine the user-friendly list names to create a menu
Prompt = ""
For InxList = 0 To UBound(ListName)
Prompt = Prompt & InxList + 1 & ". " & ListName(InxList) & vbLf
Next
Prompt = Prompt & "Please enter the number against the list you require." _
& vbLf & "Leave box empty to cancel selection."
' Loop until user cancels or enters a permitted value
Do While True
ReturnValue = InputBox(Prompt, "Select filter")
If VarType(ReturnValue) = vbBoolean Then
If Not ReturnValue Then
' The documentation for InputBox claims it returns False if
' the user clicks Cancel. In my experience it return a
' null string but check to be on the safe side.
Exit Sub
Else
' True is not a documented return value from InputBox.
' This code should never be executed but if something
' goes wrong there is a message for the user.
Call MsgBox("Please report there has been a InputBox " & _
"error type 1 to Chaka", vbCritical)
Exit Sub
End If
End If
If VarType(ReturnValue) <> vbString Then
' False or a string are the only documented return values
' This code should never be executed but if something
' goes wrong there is a message for the user.
Call MsgBox("Please report there has been a InputBox " & _
"error type 2 to Chaka", vbCritical)
Exit Sub
End If
If ReturnValue = "" Then
' User has clicked cancel or left the text box empty.
Exit Sub
End If
If IsNumeric(ReturnValue) Then
InxList = ReturnValue - 1
If InxList >= 0 And InxList <= UBound(ListName) Then
' Good selection
Exit Do
End If
End If
Loop
With Sheets("Sheet2")
If .AutoFilterMode Then
' AutoFilter is on. Cancel current selection before applying
' new one because criteria are additive.
.AutoFilterMode = False
End If
.Cells.AutoFilter Field:=ColNum(InxList), Criteria1:="x"
End With
End Sub
A simple solution, hope this helps
Here's a simple solution I figured:
Copy the top row with the heading from the main sheet to all the
sheets
Paste this formula in field A2 of each sheet: ='MainSheet'!A2:I555 (number
555 can be increased as per requirement)
Drag this first row-wise, then column-wise till the end
In the top row with the heading, filter the data as per your requirement, for e.g. in the MainSheet, data can be filtered on any column on any value needed
Do this for all the sheets
When you update the data in the MainSheet, just re-run the filter to refresh the data
HTH,