Excel VBA eaching data in different worksheet - excel

I wrote a code that should take value form activecell in sheet1 and look for this value in column A:A in sheet2. When it finds it, code should input data into the cell to the right (still in sheet2). It should input data from txtform (which is data written by user into form textbox.
Private Sub BtnOK_Click()
For Each element In Sheets("sheet2").Range("A:A")
If element.Value = ActiveCell Then
element.Select
ActiveCell.Offset(0, 1).Value = txtform
Exit For
End If
Next
Unload Me
End Sub
It doesn't work. Runtime error 1004. Please, help me.

A number of possible reasons:
Vou are selecting a different cell (element.Select), thereby changing ActiveCell
You are comparing a value (element.value) to an object (ActiveCell); change to ActiveCell.Value
I am assuming txtform is a VBA variable (e.g. a String) declared somewhere above. If not, declare it in a variable or reference the object property.
.
Private Sub BtnOK_Click()
For Each element In Sheets("sheet2").Range("A:A")
If element.Value = ActiveCell.Value Then
element.Offset(0, 1).Value = txtform
Exit For
End If
Next
Unload Me
End Sub

Related

Updating Value to First Empty cell of Column via Textbox1

I have been using a below piece of code that adds the TextBox1 value into the Col"B" and Col"L" first empty cell of two column 1 is table column and second is range column. Both are from different sheets.
But receiving an error Object does not support this property and range
Any help will be appreciated.
Private Sub CommandButton1_Click()
Sheet37.ListObjects("Table14").ListColumns("Condition").End(xlDown).Offset(1, 0).Value = TextBox1.Value
Sheet5.Range("L2").End(xlDown).Offset(1, 0).Value = TextBox1.Value
ActiveCell.Value = TextBox1.Value
Unload Me
End Sub
When i saw Unload Me, i am assuming that you are using Userform to write data on excel sheet, in your case, the error occur due to you did not mention where the textbox is located:
All the 3 VBA are missing me. that refer to userform that you have created:
Sheet37.ListObjects("Table14").ListColumns("Condition").End(xlDown).Offset(1, 0).Value = me.TextBox1.Value
Sheet5.Range("L2").End(xlDown).Offset(1, 0).Value = me.TextBox1.Value
ActiveCell.Value = me.TextBox1.Value
If you still not clear how to link Userform-textbox-excel, you can refer to my simple instruction on this link Can't update a table with vba
To update the value on ListObject Table, you may use the following method as we need to use databodyrange in order to access the cell in Listobject:
Sub submit()
Dim tb1 As ListObject
Dim s As Long
Set tb1 = Sheet2.ListObjects("Table1")
s = tb1.ListRows.Count
tb1.DataBodyRange.Cells(s + 1, 1) = Me.TextBox1.Value
Unload Me
End Sub

Userform and Selecting Cell values

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

How to populate a listbox based on a conditional statement

I'm trying to populate a listbox in a userform, but the listbox needs to change based on what is in a cell on the active sheet. The complication is that I am trying to refer to a named range, which is on another sheet. So for example, if the cell says "hi" - I would want to check that the cell says hi, and then go to the named range on another sheet called "hi" and bring in the values in that range into the listbox.
Here's what I have so far:
Private Sub UserForm_Initialize()
'Populate Combobox Based on Cell Value
Dim celltxt As String
celltxt = ActiveSheet.Range("cellTest").Text
If InStr(1, celltxt, "hi") Then
'Code to bring in routes from named range called "hi"
ListBox1.RowSource = Worksheets("Sheet4").Range("hi").Value
End If
End Sub
I would repeat the "if-end if" segment of code multiple times based on other cell values, such as "hey" or "what's up."
I keep getting a run-time 1004 error. Help!!
The problem was that I had renamed "Sheet 4" as "DropDown" because it was where I was storing all of my dropdown menus. The code should read:
Private Sub UserForm_Initialize()
'Populate Combobox Based on Cell Value
Dim celltxt As String
celltxt = ActiveSheet.Range("cellTest").Text
If InStr(1, celltxt, "hi") Then
'Code to bring in routes from named range called "hi"
ListBox1.RowSource = Worksheets("DropDown").Range("hi").Value
End If
End Sub
And that works great!

Populate textboxes based on Combobox value

I am trying to populate a textbox based on the selection i made on the combobox. It would basically look up the combobox value and return the assigned value from the column (the 3rd) I chose. I have some code done but it returns a type mismatch error
Private Sub UserForm_Initialize()
ComboBox1.List = Sheets(1).Range("C15:C39").Value
End Sub
Private Sub ComboBox1_Change()
TextBox1.Text = Application.VLookup(ComboBox1.Value, Worksheets("sheet1").Range("A15:K39"), 3, False)
End Sub
Can anyone let me know what's wrong and if there is a better way to go about this?
Assuming your combo is not multiselect then load it with all of your range and just show the columns you are interested in. Make the number of columns in the ComboBox the same number of columns in your range.
ComboBox1.List = Sheets(1).Range("A15:K39").Value
ComboBox1.ColumnCount=11
ComboBox.ColumnWidths="0,0,100,0,0,0,0,0,0,0,0"
TextBox1.Value=ComboBox1.List(ComboBox.ListIndex,2) 'the columns are zero-based
Try maybe this:
TextBox1.Text = Application.WorksheetFunction.VLookup(ComboBox1.List(ComboBox1.ListIndex, 0), Range("C15:K39"), 1, False)
Thank you guys,
I ended up going with this
Private Sub ComboBox1_Change()
Dim myRange As Range, f As Range
Set myRange = Worksheets("Sheet2").Range("A2:B26")
Set f = myRange.Find(What:=ComboBox1.Value, LookIn:=xlValues, Lookat:=xlWhole, MatchCase:=False) '<--| try and find combobox selected value
If f Is Nothing Then
TextBox1.Value = ""
Else '<--| ... otherwise...
TextBox1.Value = f.Offset(, 1)
TextBox2.Value = f.Offset(, 2)
TextBox3.Value = f.Offset(, 3)
End If
End Sub
I had 3 textboxes that i populated with three different columns once an option is chosen from the Combobox.
What i am trying to do is have two last textboxes that I would eventually write numbers in and that would automatically update the corresponding cells.
The Problem I face is updating the cell that relates to the value entered on the combobox.
The code below is very rudimentary and i am wondering if i should do the same manually. First part of the code populates three first textboxes, second part is supposed to populate corresponding cells with value I enter depending on the combobox selection. Please let me know if i am not clear enough.
Private Sub OK_Click()
Sheets("Sheet1").Range("C9") = TextBox4.value
Sheets("Sheet1").Range("D9") = TextBox5.value
End Sub

Excel VBA: Userfom - textbox value depends on combobox value

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

Resources