Multiple command buttons in sequence with one another - excel

I am new to programming and I want to create an application for basketball coaches to be able to keep stats of their players by using VBA in Excel with command buttons.
Instead of manually inputting stats for each player in a game, I want to be able to click a command button with a players name on it to select that player's row. Then click a button that will have an action on it and input a number.
I will have 12 buttons for the players and 40 buttons for the actions (e.g. pts, reb,stl,etc.)
For example: When I click the player's name button, it will select the row that the player's attribute are in. Then when I select the action (e.g. points), it will add the number 2 to the column labeled points.
I want to use the action buttons for all 12 players so only put the number in when i click the player's name button. So the "pts" button will work for all 12 player buttons. Overall, I want to make a stat sheet for the coaches with command buttons instead of moving the cursor and inputting the information in manually.
Any suggestions on how to do so? Thank you in advance.
Clancy

Some example code, using a module-scoped variable to store which player is being processed, might be:
Option Explicit
Private CurrentPlayerRow As Long
Sub PlayerA_Click()
CurrentPlayerRow = 3
End Sub
Sub PlayerB_Click()
CurrentPlayerRow = 4
End Sub
Sub PlayerC_Click()
CurrentPlayerRow = 5
End Sub
Sub Action1_Click()
'Update column D by adding 2 to the cell value
Cells(CurrentPlayerRow, "D").Value = Cells(CurrentPlayerRow, "D").Value + 2
End Sub
Sub Action2_Click()
'Update column G by adding an inputted number to the cell value
Cells(CurrentPlayerRow, "G").Value = Cells(CurrentPlayerRow, "G").Value + CLng(InputBox("Enter a number:"))
End Sub
(Not knowing anything about basketball scoring and/or statistics, I wasn't sure what sort of actions you wanted to process.)

Related

Repopulate Excel data to report specific information whenever a validation list option is switched

I'm working on an Excel financial template that calculates Standard Costs of Manufacturing, and on one tab I need to select from a list (Current, Increase/Decrease, NPI) to tell the calculator to use either a "standard value" or "manual entry", but coming from the same cell.
The Docs saved so that a new WB its set to 'Current' and displays the current 'List Price', 'Bulk Price', and 'Materials Cost' that are called from the 'ROI - Current' to the 'ROI - Target' tab where gross margin is calculated.
However, when you changes through the options, you need to rewrite the called values to whatever it needs to be. When you're doing process improvement, you doing this this a lot just to see how the numbers are moving. So I'm looking for a way to repopulate data a specific way every time a list option is switched.
What I'd like to accomplish is when 'Current' gets selected, the *List Price $(75), Bulk Price $(60), Materials Costs $(18), are called back from the ROI - C tab. (Preferable, nothing would be modifiable unless you changed options, like when using the Validation's error message; but not essential).
Then if Increase/Decrease is selected, List Price and Material Costs populates with the Current $ (but are modifiable fields). Here, Bulk Price is calculated as (CurTabBulk/CurTabList)xIncDecList then used to find the gross margin.
Then when 'NPI' is selected, if only List Price is modified, it calculates bulk price as 0.7 x List Price to assume Material Costs to get gross margin. So as an example of all this:
$75 List x 0.8 = $60 Wholesale X 0.3 = $18 Materials / $60 Wholesale * 100 = 70% Gross Margin (which is the NPI Market Forecast from the ROI - Current Tab building the ROI - Target Tab.)
I can code all this into the cells directly, but whenever you enter your own value, the code that exists in that cell is overwritten. So, I'm looking for a fast 'n dirty way to repopulate it a specific way every time the option is switched.
Thanks all!
UPDATE: So I made it this far at work today using Alt-F11:
I found this code that I put into the VBA:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$G$4" Then
Range("H4:I7").Value = "Select"
End If
End Sub
That will set all validation boxes H4:I7 to "Select" after choosing the Category, but I want each cell to update with the value rather than having to keep selecting them since it's the only value in the list.
I then found this VBA that will reset as I need, but have no idea how to make it work at all...
Sub ResetDropDowns()
Dim rngLists As Range
Dim ListCell As Range
On Error Resume Next
Set rngLists = Sheets("Entry Sheet").UsedRange.SpecialCells(xlCellTypeAllValidation)
On Error GoTo 0
If Not rngLists Is Nothing Then
For Each ListCell In rngLists.Cells
ListCell.Value = Range(Trim(Mid(Replace(ListCell.Validation.Formula1, ":", String(99, " ")), 2, 99))).Value
Next ListCell
End If
End Sub
Also, After the independent Category is chosen, I will need to enter values into the white boxes under Product 1 and 2. These boxes move around depending on what ROI category is selected: Current = No white; Adj/ Price =- List & Costs, NPI = List & GM. So, I'm hoping I can just reassign the code to accomplish calling the 'dependent recalculated Values' to the correct cells as changes are made.
Follow the URL for pictures of the spread sheet. This is the dependent list code that I put into the Validation Source (the result numbers in the image) for Product 1 List Price: =INDEX($E$43:$G$43,,MATCH($G$4,$E$42:$G$42,0))
Thanks all for the help.
https://www.excelforum.com/excel-programming-vba-macros/1387101-auto-populate-dependent-validation-list-with-fist-values-from-list.html
This solves it. Right click the tab to open the Code editor. Copy and past below to the window. Change Sheet1 and the Cells to what ever you need. Create a Macro button using an inserted Icon and right click it and select make macro.
Sub ResetDropDowns()
Dim rngLists As Range
Dim ListCell As Range
On Error Resume Next
Set rngLists = Sheets("Sheet1").Range("G21,H21")
On Error GoTo 0
If Not rngLists Is Nothing Then
For Each ListCell In rngLists.Cells
ListCell.Value = Range(Trim(Mid(Replace(ListCell.Validation.Formula1, ":", String(99, " ")), 2, 99))).Value
Next ListCell
End If
End Sub

Multiple Listboxes showing ranges from active sheets - Excel VBA

This is my First Ever Post:
I am creating a user form that will function similarly to a school management system.
One of the elements that are part of it is a Pupil Profile, which shows all information relating to the specific child.
Currently, each pupil has its own sheet. When searching for the child, the sheet matching the name of the child will be set as the active sheet.
This sheet contains sets of columns that store data about Parents meetings, rewards and sanctions, concerns and meetings with key staff members.
Example Pupil Sheet
I have a multipage setup with 4 tabs, each tab has a list box that I want to show the data from the set of columns on the active sheet.
I don't know a great deal about VBA, so I followed some tutorials and managed to make one of the sets of columns work perfectly. But to be honest, I don't understand how the code works, and I can't make it work for the other rows:
Private Sub cmdSearch_Click()
Dim X As Long
Dim Y As Long
Dim i As Long, G As Integer
Dim C As Integer, M As Integer
For G = 2 To Sheets.Count
If Sheets(G).Name = txtSearch Then
For i = 2 To Sheets(G).Range("A2000").End(xlUp).Row
Me.ListBox1.AddItem
For C = 0 To 6
Me.ListBox1.List(ListBox1.ListCount - 1, C) = Sheets(G).Cells(i, C + 1)
Next C
Next i
End If
Next G
Private Sub AddParentMeeting_Click()
TgtSheet = txtSearch.Value
If TgtSheet = "" Then
Exit Sub
End If
Worksheets(TgtSheet).Activate
LastRow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row
ActiveSheet.Cells(LastRow + 1, 1).Value = PM1.Value
ActiveSheet.Cells(LastRow + 1, 2).Value = PM2.Value
ActiveSheet.Cells(LastRow + 1, 3).Value = PM3.Value
ActiveSheet.Cells(LastRow + 1, 4).Value = PM4.Value
ActiveSheet.Cells(LastRow + 1, 5).Value = PM5.Value
ActiveSheet.Cells(LastRow + 1, 6).Value = PM6.Value
End Sub
Basically, my two key issues are:
How do I populate list boxes from a range of data on the active sheet so that it changes with each new profile search? E.g
ListBox1 shows A2:F2000 on active sheet
ListBox2 shows H2:K2000 on active sheet
ListBox3 shows M2:Q2000 on active sheet
ListBox4 shows S2:X2000 on active sheet
How do I add information to just those columns without it adding the information to the next fully clear row? e.g)
I don't want this to happen:
Picture
I want it to look like this:
Picture
Sorry for the lengthy post, and any questions or help in the right direction would be really appreciated
You may not like this reply and for that I apologize but your approach ignores all database basics. It will give you more work than need be and fail before the project is finished. The first mistake is to have a separate sheet for each pupil.
You need one sheet with a row for each pupil, columns like name, first name, middle name, birthday (not age because you can calculate the age from the birthday any time you need it), address, parent's phone etc. The first column is the most important. In db parlance it's call the "Key". It contains a unique identifier for everything in the row, a pupil number, if you like or an ID code. The point of the "key" is to be unique. By this ID you can retrieve the entire row and all information in it.
Then you need 4 more similar data bases: Parent Meetings, Staff Meetings, Concerns, Rewards/Consequences. Each of these has the columns you already designed plus one more which is called the "Foreign Key". In each row of these tables you enter the "Key" from the Pupils sheet, linking each entry - Meeting, Concern or Reward - to one particular pupil. Perhaps you have a validation drop-down with the pupils' names to avoid typos in the Keys. But that is already step 2.
You would be able to work with this setup immediately. Simply filter any of the lists on a pupil's ID to get a list of all meetings, concerns or rewards for any one pupil. With the help of the "Key" you would be able to easily and quickly create a sheet with the pupil's details at the top, meetings, concerns, rewards listed - much like your existing pupil sheets but with changing data as you select a pupil from a validation drop-down. All done with VLOOKUP and worksheet functions. No user form yet. That would be the de-luxe version but you would have 80% of the functionality while you work on it.

How to Start Cursor After 3rd character of text box in UserForm

I am fairly new to VBA so any help would be greatly appreciated.
I have a UserForm that contains 3 labels and 3 text boxes. 2 of the text boxes (including the first one that is activated upon the form's activation) are empty. The third has 3 characters pre-filled into it. When the userform starts, I would like to fill in the first text box as normal then when I tab into the second text box, I would like the cursor to automatically go to the end of that 3 letters instead of highlighting the entire text box. I have looked around but cannot seem to find anything that can do this easily. Could you please let me know where exactly (which sub) to enter this code into as well?
My UserForm is called 'GetBIInfo2' and my text box is called 'NBIDText'
Thanks!
Select the textbox.
Go to property (F4).
Scroll till you find EnterFieldBehavior property.
Change it to 1 - FmEnterFieldBehaviorRecallSelection.
The direct approach is via the textbox'es .SelStart property; possibly you might call it via a sub procedure like this:
Sub fillAnyTextBox(myTextBox As MSForms.TextBox, ByVal firstCharacters As String)
With myTextBox
.Value = firstCharacters
.SetFocus
.SelStart = Len(firstCharacters)
.SelLength = Len(.Text) - Len(firstCharacters)
End With
End Sub
A simple alternative would be to make another text box to hold the first three characters and have the user enter the following characters into a separate textbox. Then you can write code that will take the contents of both and append them into a single string like this:
Dim finalString As String
finalString = TextBox1.Value & TextBox2.Value

How do I call upon checkboxes with groupname or tag?

Am creating a form in which the user will be able to select 4 checkboxes. Blue, Green, Yellow, Red. They have to do this atleast twice, up to 5 times in total.
The Form Example
The first checkboxes are on the form, the other 4 grouped checkboxes are on multipages.
I've tried giving them a group name "Reeks" to "Reeks4", to then print their values into a single cell separated by comma.
The code I've found so far does print the values neatly, however, it grabs every available checkbox on my form instead of just from the group "Reeks".
The ways I've tried to add the groupname (or tag, named them both the same) to the code, only gives me errors.
For Each Control In Me.Controls
If TypeName(Control) = "CheckBox" Then
'If Control.GroupName = "Reeks" Then
If Control.Value Then
Reeks = IIf(Reeks <> "", Reeks & ", ", "") & Control.Caption
End If
End If
Next
With ws
.Cells(iRow, 5).Value = Reeks
End With
Any help is welcome, but if at all possible please add comments as I only started on VBA last week.
(Side note. I also have framed checkboxes on the multipages in which the user can select the location of said colour. Side + Top view. Which will need to work the same as with the colours, but printed in different columns. )

Focus Rectangle Appears on All Listbox Items in VBA

I've created a Userform in Excel VBA, on which there is an unbound Listbox that has its MultiSelect property set to Extended. When that listbox receives focus by any means other than clicking a list item, all items in that list appear with the dotted focus rectangle around them.
Here is some code that shows the phenomenon beside another listbox with MultiSelect set to Single for comparison. Create a Userform, put two Listboxes on it, and add the code to the form. When you launch the form, tab between listboxes to see what I've described.
Private Sub UserForm_Activate()
ListBox1.MultiSelect = fmMultiSelectSingle
ListBox2.MultiSelect = fmMultiSelectExtended
Dim i As Integer
For i = 1 To 15
ListBox1.AddItem String(i, Chr(i + 64))
ListBox2.AddItem String(i, Chr(i + 64))
Next
End Sub
Is there a way to remove the focus rectangles or prevent their appearing?
Thanks,
I have experimented with your code in Excel 2010 and confirm your observation. If I create two list boxes, enter the code provided, start the form and press tab to focus on ListBox2, the dotted lines appear around all rows.
If I create the two list boxes as before, manually set ListBox2/Properties/Multiselect to 2 - fmMultiSelectExtended, run and tab to ListBox2 the nasty lines disapperar.
For me this is rather stable, the form now survives multiple window activation changes, jumpng back/forth etc.
don't ask me why ...

Resources