How to have checkbox in each row enter values in respective row - excel

I have a checkbox in each row of column K {Checkbox1, Checkbox2, etc.} I want each checkbox to put an "X" in each respective row. I have this code to do it for one of the checkboxes:
Private Sub CheckBox1_Click()
If CheckBox1.Value = True Then
Range(Cells(3, 12), Cells(3, 35)).Value = "X"
Else
Range(Cells(3, 12), Cells(3, 35)).Value = ""
End If
End Sub
But I want this for all the checkboxes and plan to have around 30 checkboxes. Is it possible to assign a variable i to have checkboxi and each row depend on the variable?

This is going to be a multipart procedure.
Create a new Module
In the VBAProject Window
Right Click on the Microsoft Excel Objects
Select Insert
Select Module
Copy/Paste the below macros in the new Module
Sub AsgnOnAction()
Dim ckBx As CheckBox, Rng As Range
For Each ckBx In ActiveSheet.CheckBoxes
ckBx.OnAction = "CkBxClk"
Next ckBx
End Sub
Sub CkBxClk()
Dim Rng As Range
Set Rng = ThisWorkbook.Sheets("Sheet2").Range("K3:K" & Range("K" & Rows.Count).End(xlUp).Row)
For Each CheckBox In Rng
If CheckBox.Value = True Then CheckBox.Offset(, 1).Resize(, 24).Value = "X"
If CheckBox.Value = False Then CheckBox.Offset(, 1).Resize(, 24).Value = ""
Next
End Sub
Run AsgnOnAction() to update all the checkboxes .OnAction parameter
Goto your worksheet and Right Click one of the checkboxes to verify that the macro has been assigned.
Select a checkbox and an X will be pasted to your range. When you deselect a checkbox all Xs will be removed.
I hope this helps

Related

Copying data from Userform CheckBox and Textbox to columns

I am trying to create a tool with a userform where the user types a Model in a textbox and selects all the countries where this Model comes from in 10 possible checkboxes.
This information is transferred to "Country" Worksheet through command button.
My code places textbox value in column A and country name from checkbox label in column B.
The problem is that I have more than one country for the same model so I'm getting blank cells without matching Model.
E.g. "Type A" belongs to USA, Brazil, Sweden and Mexico, so I should have "Type A" copied four times along country names instead of just one.
Private sub Transfer()
Dim i As Long
Dim aCol As Range
Dim BS As Worksheet
Set aCol = Worksheets("Country").Range("A:A")
Set BS = Worksheets("Country")
For i = 1 To 10
With Me.Controls("CheckBox" & i)
If .Value Then
aCol.Cells(82, 2).End(xlUp).Offset(1, 0).Value = .Caption
End If
End With
Next i
Dim b As Integer
b = 1
Do Until BS.Range("A" & b).Value = ""
b = b + 1
end sub
With the code you shared and without major changes, I would suggest you to think about writing the info contained in the textbox within the loop of the checkboxes and right after the if. This way you will be adding the textbox text no matter what avoiding the blanks
If .Value Then
aCol.Cells(82, 1).End(xlUp).Offset(1, 0).Value = Me.Controls("TextBox1").Text
aCol.Cells(82, 2).End(xlUp).Offset(1, 0).Value = .Caption
End If
Let me know if that works, below the full code I used to replicate your issue:
Private Sub CommandButton1_Click()
Call Transfer
End Sub
Private Sub Transfer()
Dim i As Long
Dim aCol As Range
Dim BS As Worksheet
Set aCol = Worksheets("Country").Range("A:A")
Set BS = Worksheets("Country")
For i = 1 To 3
With Me.Controls("CheckBox" & i)
If .Value Then
aCol.Cells(82, 1).End(xlUp).Offset(1, 0).Value = Me.Controls("TextBox1").Text
aCol.Cells(82, 2).End(xlUp).Offset(1, 0).Value = .Caption
End If
End With
Next i
End Sub
How the form I did looks in VBA
How the results look like in the file

Add Items on Combo Box depending on the Cell Color

I am new to Excel-VBA and I'm trying to add items in combo box depending on the cell color.
In this example, I only want the text of those blue shades be added on the combobox list.
Sheets("Application").ComboBox1.List = Range("A:A").Value
This code will go through out column A and if it finds a cell with specific color, it will add it to the combobox, this will occurred every time the sheet gets activated.
Option Explicit
Private Sub Worksheet_Activate()
Dim rng As Range
Dim i As Long
Dim LastRow As Long
Me.ComboBox1.Clear
With Sheets("Application")
LastRow = .Cells(Rows.Count, "A").End(xlUp).Row
For i = 2 To LastRow
If .Cells(i, 1) <> vbNullString Then
If Cells(i, 1).Interior.Color = 12611584 Then
Me.ComboBox1.AddItem Cells(i, 1)
End If
End If
Next
End With
End Sub

Dynamically adding column values based on combo box selection

I need your help. It seems what I have written in code does not accomplish what I am trying to do here.
The objective would be to have 2 userform combo boxes one for the (floor) values which are manually added once [3,4,5] and the other combo boxes (offices) in which values are dynamically added based on the selection made in the floor selection box.
Let's say for example that if I chose the value [3] in my floor combo box that the office combo box would contain the following values:
A-01
A-02
A-03
A-04
A-05
A-06
A-07
A-08
I thought this code would work but it doesn't:
'Cells(row, col)
Private Sub floor_Change()
lRow = Sheets("Office Spaces").UsedRange.Rows.Count
With Sheets("Office Spaces")
For i = 2 To lRow
If .Cells(i, 1).Value = UserForm1.floor.Value Then
UserForm1.office.AddItem .Cells(i, 2).Value
End If
Next i
End With
End Sub
Here's what the data looks in my excel sheet:
'Cells(row, col)
Private Sub floor56_Change()
UserForm1.office.Clear
Dim sh
Dim rw
Set sh = Sheets("Office Spaces")
For Each rw In sh.Rows
If sh.Cells(rw.row, 1).Text = UserForm1.floor.Value Then
UserForm1.office.AddItem (sh.Cells(rw.row, 2).Value)
End If
Next rw
End Sub
or
Private Sub floor_Change()
If UserForm1.floor.Value <> "" Then
UserForm1.office.Clear
Dim ws
Set ws = ThisWorkbook.Worksheets("Office Spaces")
Dim rng
Set rng = ws.Range("A:A")
For Each cell In rng
If cell.Text = UserForm1.floor.Value Then
UserForm1.office.AddItem (cell.Offset(0, 1).Value)
End If
Next cell
End If
End Sub

Be able to select next cell down every time button is clicked Excel VBA

Below is the code that I have so far I am able to click the button and every time the button is clicked the cell selection is moved down the row by 1.
What I need is to start the selection on F3 and select down until about F35 but when I range it doesn't select the cells one by one.
Here is my code:
Dim rng As Range
Dim row As Range
Dim cell As Range
Set rng = Range("F2")
rng.Select
For Each row In rng.Rows
For Each cell In row.Cells
ActiveCell.Offset(1, 0).Select
Next cell
Range("G66") = ActiveCell
Next row
if you have a Form button called Button1 then attach it a sub called Button1_Click() (or whatever, but be consistent with the name of the attached Sub) and place the following code in any module:
Option Explicit
Dim notFirst As Boolean
Dim rng As Range
Sub Button1_Click()
If notFirst Then
If rng.row = 35 Then
MsgBox "Sorry: you've already reached last valid cell in column F"
Exit Sub
Else
Set rng = rng.Offset(1)
End If
Else
Set rng = Range("F3")
notFirst = True
End If
Range("G66").Value = rng.Value
End Sub
if you have a ActiveX button called Button1 then write the same code as above in its sheet code pane

a command box for updating data on a spreadsheet

Good day guys I created a userform on excel with a combobox and a command button, the command button is for updating the data on the spread sheet based on the item selected on the combobox and a textbox with data, the problem is every time I click the command button it does not update the data, instead it goes to the next column and fill it in. and i want the data to be updated on the item selected
this is what I tried
Private Sub CommandButton6_Click()
Dim lrCD As Long
lrCD = Sheets("Sheet2").Range("A" & Rows.Count).End(xlUp).Row
Sheets("Sheet2").Cells(lrCD + 1, "A").Value = ComboBox1.Text
Sheets("Sheet2").Cells(lrCD + 1, "B").Value = TextBox5.Text
Create your form as below:
Column A in Sheet2 contains unique values that will appear in your combobox.
This code sits within the form.
The UserForm_Initialize() event fires when you open the form and will populate the combobox with values.
The CommandButton6_Click event searches column A and places the text in textbox5 next to the selected value.
Private Sub UserForm_Initialize()
Dim lrCD As Long
'Populate ComboBox with values from column A on Sheet2.
With ThisWorkbook.Worksheets("Sheet2")
lrCD = .Range("A" & Rows.Count).End(xlUp).Row
Me.ComboBox1.RowSource = "'Sheet2'!" & .Range(.Cells(1, 1), .Cells(lrCD, 1)).Address
End With
End Sub
Private Sub CommandButton6_Click()
Dim rCell As Range
With ThisWorkbook.Worksheets("Sheet2")
'Look for the selected value in column A.
Set rCell = .Columns(1).Find( _
What:=Me.ComboBox1.Value, _
After:=.Cells(1, 1), _
LookIn:=xlValues, _
LookAt:=xlWhole)
'If a value is found place the textbox value in the column next to it.
If Not rCell Is Nothing Then
rCell.Offset(, 1) = Me.TextBox5.Value
End If
End With
End Sub

Resources