a command box for updating data on a spreadsheet - excel

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

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

I have 4 values obtained from a form, I would like to paste these values into a worksheet

I have these four values obtained from a userform.
With Worksheets("Sheet1)
Worksheets("Sheet1").Range("A2").value = TextBox1.Value
Worksheets("Sheet1").Range("B2").value = ListBox1.Value
Worksheets("Sheet1").Range("C2").value = TextBox2.Value
Worksheets("Sheet1").Range("D2").value = ListBoxw.Value
End with
But I want to paste in the next available row instead of just replacing the data in row 2. How can I make it dynamic like that? Do I need to count the number of items already there? So if the user opens the form again, the previously entered data is still there and we just add on. Also, how could I put the current date as the default value in TextBox1?
In order to paste the values to the last empty row, use the next code:
Sub PasteBelowTheLastRow()
Dim lastEmptyRow As Long, sh As Worksheet
Set sh = ActiveWorkbook.Sheets("Sheet1")
lastEmptyRow = sh.Range("A:A").End(xlUp).Row + 1
With sh
.Range("A" & lastEmptyRow).value = TextBox1.value
.Range("B" & lastEmptyRow).value = ListBox1.value
.Range("C" & lastEmptyRow).value = TextBox2.value
.Range("D" & lastEmptyRow).value = ListBox2.value
End With
End Sub
In order to automatically set the current data in TextBox1 you must have the next code in the UserForm_Initialize() event:
Private Sub UserForm_Initialize()
Me.TextBox1.value = Date
End Sub

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

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

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

Excel VBA - Populate a Combobox given selection of another Combobox

I am trying to populate a second combobox given the selection of a first combobox. The first combobox is the name of all the columns on the sheet. The second combobox should display all of the values in that column except for duplicates. The code below populates combobox1. I am struggling with how to take data out of a column given the varying column name. Thank you for any help.
Dim myArray As Variant
lastcol = Sheets(4).Range("A1").End(xlToRight).Column
With Sheets(4)
Set SourceRng = .Range(.Cells(1, 1), .Cells(1, lastcol))
End With
myArray = WorksheetFunction.Transpose(SourceRng)
With Me.ComboBox1
.List = myArray
End With
You could try to get the listindex of combobox1. Keep in mind that the ListIndex is 0-Based while Excel Rows and Columns are not:
Private Sub ComboBox1_AfterUpdate()
Dim selectedCol as Variant
selectedCol = Me.ComboBox1.ListIndex + 1
Set SourceRng = ws.Range(Cells(2, selectedCol), Cells(4, selectedCol))
Me.ComboBox2.List = WorksheetFunction.Transpose(SourceRng)
End Sub
To get rid of the duplicate values and junk: Set SourceRng = ws.Range(Cells(varRow, Me.ComboBox1.ListIndex + 1), Cells(varRow2, Me.ComboBox1.ListIndex + 1)).RemoveDuplicates Columns:= selectedCol, Header:=xlNo
Here is a workaround for removing the duplicates. Using the RemoveDuplicates function of the Range class will delete your rows, and I'm assuming you don't want that:
Private Sub ComboBox1_AfterUpdate()
Dim colSelect As Integer
colSelect = Me.ComboBox1.ListIndex + 1
Set SourceRng = ws.Range(Cells(2, colSelect), Cells(5, colSelect))
SourceRng.Sort SourceRng, xlDescending
Dim c
For Each c In SourceRng.Cells
If c.Value c.Offset(-1, 0).Value Then
Me.ComboBox2.AddItem c.Value
End If
Next
'You would take out the array and assigning it to the Combobox2.List
End Sub

Resources