I have an ActiveX ComboBox1 on Sheet 1 that contains each month. I then have a row on the same sheet that contains a month in each cell Range C7:N7. I would like to write a code that fills the cell in the next row Range(C8:N8) with data from cell D14 on Sheet 2 if ComboBox1 = Range(C7:N7)
The code would look similar to this:
If ComboBox1 = Range(B7, N7) Then
Range(B8, N8) = "Sheet2!$D$14"
End If
Is this possible? Do I need to define something?
For something like this, you can take advantage of the combobox's ListIndex property:
Private Sub ComboBox1_Change()
'Clear prior entries
Range("B8:N8").ClearContents
'Make sure something has been selected in the listbox
If ComboBox1.ListIndex = -1 Then Exit Sub
'Populate the appropiate cell
Range("B8").Offset(, ComboBox1.ListIndex).Value = Sheets("Sheet2").Range("D14").Value
End Sub
Related
I need some guidance how to create a selection for a excel userform that when the user selects CB1 which may be in A2, that it will also select the next cell B2 for the populated field and also select the next cell in C2 for the next populated field. I am not sure if my first selection should be a CB and the next fields maybe a list box or text field.
current code is:
Private Sub Userform_Initialize()
ComboBox_Combox5.List=Sheets("Sheet1").Range("A1:A650").Value
End Sub
Add two text boxes next to your combo-box.
I've left them with the default names of TextBox1 and TextBox2.
Code behind the form is:
Option Explicit
Private Sub Userform_Initialize()
ComboBox_Combox5.List = Sheets("Sheet1").Range("A1:A650").Value
End Sub
Private Sub ComboBox_Combox5_Change()
'Reference to selected value in column A.
Dim rng As Range
Set rng = ThisWorkbook.Worksheets("Sheet1").Cells(Me.ComboBox_Combox5.ListIndex + 1, 1)
Me.TextBox1 = rng.Offset(, 1)
Me.TextBox2 = rng.Offset(, 2)
End Sub
Always have Option Explicit at the top of your module.
Just playing around with userform. Pretty new to using them . Created a pretty simple userform that find me the percentage change between two values that I enter. (see image below). I'm just interested in going one step further, I want to know how to enter the values based on excel cells that I select. So in this example, if I select K6, the value 15 enters in current year and if I subsequently click on K8, the value 10 enters in base year). Hope that makes sense, let me know if that is possible. Thanks.
current code....
TextBox3_Change()
TextBox3.Value = Format((Val(TextBox1.Value) - Val(TextBox2.Value)) / Val(TextBox2.Value), "#0.00%")
Rather than clicking, why not load the values in those textboxes in the UserForm_Initialize() event? Something like
Private Sub UserForm_Initialize()
TextBox1.Value = Sheets("Sheet1").Range("K6").Value2
End Sub
If you really want to select a cell (after the userform is shown) and then populate the textboxes, then yes that is also possible. Here is an example
For demonstration purpose, I am going to populate 1 textbox. Let's say our userform looks like this. Note, I added a CommandButton1 next to the textbox. I changed the caption to .... We will use Application.InputBox with Type:=8 so that user can select a range.
Next paste this code in the userform code area
Option Explicit
Private Sub CommandButton1_Click()
Dim rng As Range
On Error Resume Next
'~~> Prompt user to select range
Set rng = Application.InputBox(Prompt:="Select the range", Type:=8)
On Error GoTo 0
'~~> Check if the user selected a range
If Not rng Is Nothing Then
'~~> Check if the range is single cell or not
If rng.Columns.Count > 1 Or rng.Rows.Count > 1 Then
MsgBox "Select single cell"
Exit Sub
End If
TextBox1.Text = rng.Value2
End If
End Sub
Demonstration
This isn't a built-in functionality for userforms (unlock the create a graph dialogue box).
One option would be to have the user start by selecting two cells, and then your code runs and calculates the percentage change in those two cells selected and returns the answer in a pop-up. Another option would be to have the userform automatically populate from preselected cells in your workbook (example below).
Private Sub UserForm_Activate()
Dim ActiveR As Long
ActiveR = ActiveCell.Row
TextBox1.Value = Cells(ActiveR, 1).Value
TextBox2.Value = Cells(ActiveR, 2).Value
TextBox3.Value = Cells(ActiveR, 3).Value
TextBox4.Value = Cells(ActiveR, 4).Value
End Sub
I try to make my first UserForm with combo box, I already made this:
Private Sub cmdClose_Click()
Unload Me
End Sub
Private Sub Reg1_AfterUpdate()
If WorksheetFunction.CountIf(Stock.Range("A:A"), Me.Range.Value) = 0 Then
NsgBox "This is an incorrect item"
Me.Reg1.Value = ""
Exit Sub
End If
With Me
.Reg2 = Application.WorksheetFunction.VLookup(CLng(Me.Reg1),
Stock.Range("Lookup"), 2, 0)
End Sub
Private Sub UserForm_Click()
End Sub
The thing is that I don't have any idea how to use combo box with Vlookup function.
I need a combo box because I would like to change a number on Sheet1.
For example:
Harry 10
David 20
A1 Harry B1 10
A2 David B2 20
So I would like to select a name from a Combo Box. After I selected a name I would like to type in a number into a textbox and this number is going to belong for the selected name and sum up with the existing number.
So Harry has 10. After I selected Harry from combo box and set 90 in TextBox the number is going to change for 100 for Harry. That's why I think I have to use Vlookup somehow in VBA.
Thank you
The code below should work as you asked
Sub ChangeValue()
Dim sheetName As String
sheetName = "Name of your sheet"
With ThisWorkbook.Sheets(sheetName)
'For each name in your range
For Each cell In .Range("Your range where names are")
'If the name is the one selected in your combobox
If (cell = YourComboBox.Text) Then
'Increment value
.Range(cell.Address).Offset(0, 1).Value = _
.Range(cell.Address).Offset(0, 1).Value + YourTextBoxValue.Text
End If
Next cell
End With
End Sub
Usage
Replace Name of your sheet with the name of the sheet where the names are.
Replace Your range where names are with the range where we can find all the names in your sheet.
Replace YourComboBox and YourTextBoxValue with the names of your components in your userForm.
I have two columns of data that is pulled into a worksheet from data on other sheets elsewhere in the workbook via a formula in each cell...
The first column, Column A, has either a Yes, No or is blank from data that is pulled in via a formula from another sheet.
The second column, Column B, also has data pulled in from elsewhere but every row has data in it.
What I hope to do is hide any rows that does not have anything in column A. Any rows with data in column A should be visible. I'd like this to be updated via the worksheet_change event using VBA when data is entered that appears in column A.
Many thanks if you can help.
Private Sub Worksheet_Change(ByVal Target As Range)
If Intersect(Target, Me.Range("A:A")) Is Nothing Then Exit Sub
Application.EnableEvents = False
dim lrow as Integer
dim i as Integer
lrow = Cells(1, 2).End(xlDown).Row
For i = 1 To lrow
If Cells(i, 1) = 0 Then
Rows(i).Select
Selection.EntireRow.Hidden = True
End If
Next
Application.EnableEvents = True
End Sub
You have to insert this on the code of the sheet. right click the sheet name and press the view code and save it as macro enable.
It gets activated when changes have done to column a.
I am creating userform which I will be using to insert data and then make some other stuff.
I have a Userform with ComboBox and few TextBoxes. ComboBox is filled with data from range. I want to change values of TextBoxes depending on ComboBox value. Values of TextBoxes should be filled with specific values from worksheet. I thought about creating For Each loop to determine Row of chosen ComboBox value and then change TextBoxes using Row number and setting proper offset.
Worksheet is table with headers and filled with data such as name, city etc.
However my code does not work within Userform.
Any ideas what is wrong or maybe a different approach to a problem?
klient = ComboBox name
Private Sub klient_Change()
Dim MyCell As Range, MyRange As Range
Dim wiersz As Long
Set MyRange = Range("klienci")
For Each MyCell In MyRange
If klient.Value = MyCell.Value Then
wiersz = MyCell.Value
Exit For:
End If
Next
MsgBox (wiersz)
End Sub
As follow up from comments to the questions, this code works:
Private Sub klient_Change()
MsgBox Range("klienci").Cells(1 + Klient.ListIndex, 1).Address
'do something else, e.g. get element one to the right:
MsgBox Range("klienci").Cells(1 + Klient.ListIndex, 1).Offset(,1).Address
End Sub