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
Related
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 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
I am still pretty new to VBA but I am creating a UserForm that will input data in. But I also want to have a ComboBox in the UserForm representing the months. And each month suppose to represent a column. For example, if I pick January on the UserForm, then it will put data on the K column.
I want 1 to 12 to represent Column I to T
Private Sub CommandButton1_Click
Dim ws As Worksheet
Dim lrCal As Long
lrCal = sheets("TestCal").Cells(7,Columns.Count).End(xlToLeft).Column + 1
With sheets("TestCal")
.Cells(7, lrCal).Value = tbApple.Text
.Cells(8, lrCal).Value = tbOrange.Text
.Cells(12,lrCal).Value = tbBread.Text
.Cells(13,lrCal).Value = tbJam.Text
End With
End Sub
Right now it is going to the next available cell. But I want it to go to the column that I pick.
Private Sub cboMonth_Change()
dim cboMonth as ComboBox
dim i as long
dim ws as worksheet
Set ws = ThisWorkbook.sheets("TestCal")
For i = 1 to 12
Next
I am stuck on this part, do not know how to set it up. How would I set up each number to represent each column?
Depending on your approach to populating your combobox, there are numerous ways to do this... one example would be to populate your combobox so that you can relate the list-position to a column, e.g.:
Option Explicit
Private Sub CommandButton1_Click()
Debug.Print Application.Match(ComboBox1.Value, ComboBox1.List, 0)
End Sub
Private Sub UserForm_Initialize()
With Sheets(1)
Me.ComboBox1.List = Array("Jan","Feb","Mar")
End With
End Sub
You can test the above and see that the List position (the location number in the array) is displayed in the immediate window.
So if January is in column 11 (K), you can output your data like:
Private Sub CommandButton1_Click()
'11-1 = 10, you would need to find last row in column for this:
Sheets(1).Cells(LastRow+1,10+Application.Match(ComboBox1.Value, ComboBox1.List, 0)).value = Me.TextBox1.Value
End Sub
You could also do this without the combobox using Match() or Find().
First off all I am not very experienced with excel VBA. After searching on net and youtube I couldn't figure it out, so I hope you can help me out.
In my workbook I run a macro that gives a timestamp via =NOW() and an "unique" generated code via CHAR(RANDBETWEEN). This is then copy & pasted to a separate sheet("Dates")to create the list for my combobox, where the code is in column A and the timestamp in column B. Each time I run my macro a new timestamp and code is generated and put in Row 1 Column A & B again (so the older one is shifted down)
With the combobox I got my list of timestamp based on the code below:
Private Sub UserForm_Initialize()
Dim ws As Worksheet, lirow As Integer
Set ws = ThisWorkbook.Worksheets("Dates")
lirow = 1
While ws.Cells(lirow, 2) <> ""
Me.cboxDates.AddItem ws.Cells(lirow, 2).Value
lirow = lirow + 1
Wend
End Sub
What I want is to show the corresponding code in a textbox next to my combobox, so basically just an offset (0, -1).
Your help is appreciated.
Thanks in advance!
Kr, Marty
Private Sub cboxDates_Change()
Dim Found As Range, LastRow As Long
Set Found = Worksheets("Dates").Columns(2).Find(what:=Me.cboxDates.Text, LookIn:=xlValues, lookat:=xlWhole)
If Found Is Nothing Then Exit Sub
Me.TextBox1.Text = Worksheets("Dates").Cells(Found.Row, Found.Column).Offset(0, -1).Value
End Sub
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