Moving between cells based on checkbox status - vba

I am trying to generate a unique output when a button is clicked on one sheet that will depend on the state of a group of check boxes.
There is a group of ten boxes, any of which can be selected for a maximum of ten. When the button is clicked, the name of each check box will appear sequentially. So for example:
Check box 1 and 3 are checked, the button is clicked, then the output to the cells will look like: [Check Box1] [Check Box3]
However if boxes 1 2 3 9 are checked, it will display:
[Check Box1] [Check Box2] [Check Box3] etc....
My main issue is that I only want the name of the checkbox to appear as a heading if it is selected.

Assuming "forms" checkboxes (vs. ActiveX type)
dim i, n, s
n = 0
for i = 1 to 10
s = "Check Box " & i
if activesheet.checkboxes(s).Value = 1 then
Activesheet.range("A1").offset(0, n).value = s
n = n + 1
end if
next i

Related

How to change visibility of a combo box based on another combo box value in VBA

I am using Microsoft Access at the moment.
I am trying to populate a number of combo boxes based on another combo box value. For example the first combo box is called cmb_BoxValue where it has a number of 1-20 and the remaining combo box will have the name cmb_Box1 until cmb_Box20. Once the user has selected the number of combo box required, using an AfterUpdate sub it will allow visibility = True based on the value. If the selected value is 3, cmb_Box1, cmb_Box2 and cmb_Box3 Visible = True. I managed to do it using a select case like below:
Private Sub cmb_BoxValue_AfterUpdate()
Dim Size1 As Integer
Size1 = Me.cmb_BoxValue.Value
Select Case Me.cmb_BoxValue
Case 1
Me.cmb_boxBox1 = True
etc...
But I feel like this is very repetitive and not the most ideal solution. I feel like a for loop is the ideal solution but I am not sure how to approach it. Thank you
Since comboboxes have similar names with number suffix, could dynamically build combobox name in an incrementing index loop, with a test if index is <= cmb_BoxValue input to set Visible property:
For x = 1 to 20
Me.Controls("cmb_Box" & x).Visible = x <= Me.cmb_BoxValue
Next

Use multiple combo boxes to select cell

I'm trying to make a simple user form in Excel to enter the date that a staff member did a certain task. I have a spreadsheet that lists all of my staff down column B and I have a list of all the tasks along row 5.
I've created a form with a couple of combo boxes so I can select the staff member and also select the task. There is also a text box which I will type a date into.
What I need is when I click the "submit" button it will enter the date contained in the text box into the cell defined by the 2 selections in the combo boxes. If either of the combo boxes have nothing selected then no data should be entered into the spreadsheet when the button is clicked.
Dim nameRow As Integer
Dim job1col As Integer
nameRow = cbxStaffName.ListIndex + 6
job1col = cbxJob1.ListIndex + 3
ThisWorkbook.Sheets("Job Rotation").Cells(nameRow, job1col).Value = tbxDate.Value
The combo box containing the list of staff names is called cbxStaffName. The combobox containg the jobs is cbxJob1. The textbox containing the date I want to enter is named tbxDate.
The combo boxes have been populated with data that exists on the spreadsheet, simply names down the left and jobs along the top. If for example Jim did the job welding I want to select Jim from one combo box, select welding from the other box and when I click the button the date will go into that cell on the spreadsheet.
You would just need an If statement to check if both checkboxes have a value:
If cbxStaffName.Value <> vbNullString And cbxJob1.Value <> vbNullString Then
'write into cell
Else
MsgBox "Please select staff and job first"
End If
Alternatively you can check the ListIndex which should be -1 if nothing was selected.
Note that I recommend to always use Long instead of Integer especially when dealing with row counts. Excel has more rows than Integer can handle.

How to fetch cells based on combo box selection in vba?

I have an excel sheet like this :
Also I have a userform like this :
So if a user selects say a from side and aa from top, The form should display all items corresponding to it from 1,2,3,4 in a textbox under this combo box
ie, there will be four textboxes with values s,qr,q,eef
How can i do this??
Here's some pseudo-code that you can use as a starting point. I'm assuming that the top dropdown returns an integer from 0 to 3, and the side dropdown returns integers such as 1 for a, 2 for b, etc... Then the code will use the offset function to position the cell properly so that each textbox gets the text from the correct cell. For example, if side = 1 and top = 0 then the cell for Textbox1 will be the one offset from A2 by 1 row, 1 columns, and if side = 2 and top =1 then the cell for Textbox1 will be the one offset from A2 by 2 rows and and 5 columns. Let me know if you have questions.
Option Explicit
Sub test()
Dim r As Range, side As Integer, top As Integer
Set r = Range("A2")
Textbox1 = r.Offset(side, top * 4 + 1)
TextBox2 = r.Offset(side, top * 4 + 2)
TextBox3 = r.Offset(side, top * 4 + 3)
TextBox4 = r.Offset(side, top * 4 + 4)
End Sub

Formatting a combo box

I have added a ComboBox to a form, no additional formatting so far. I have a text box that the user enters a number of competitors (value) between 20 and 100 into. I want to populate the ComboBox so that the user can select a competitor from 1 to 100 in the ComboBox. So the user will be able to click the drop-down menu and select a competitor from a list of competitors, for example Competitor 1 to Competitor 100.
Please let me know if you need any extra info.
Try this, explanations included:
yourComboBox.Items.Clear() 'to make sure the ComboBox is empty before populating and to avoid duplicating records
If Val(yourTextbox.Text) > 0 Then 'basic checking
For x = 1 To Val(yourTextbox.Text) 'loop from 1 up to the value you entered into the textbox
yourComboBox.Items.Add(x) 'add the value of x which holds the current loop number/competitor
Next
End If

VBA macro to select cell based on row and column criteria

I'm trying to create a macro that runs with a custom user form shown below. My ultimate goal is to have the Expenditure Category radial select the sheet in the file, then have the PO Number field search column A of the selected sheet to find a match, then subsequently have the Week Ending Date field search row 7 to find a match for the date entered. Where these two intersect (i.e. CN72) is where I want to input what is put in Accrual Amount. I have no idea about how to go about doing this, so any help would be great!
It is a loop inside another loop...I might use something like this..
x = 1
z = 1
datexx = [input]
POnumber = [input]
do
do
if Sheets("[name]").Range("[column]" & z) = Sheets("[name]").Range("[column]" & x) then
'do your stuff here
end if
z = z +1
loop until isempty(Sheets("[name]").Range("[A column that is never empty for all data]" & z)
x = x+ 1
loop until isempty(Sheets("[name]").Range("[A column that is never empty for all data]" & x)