Insert TextBox value into first empty cell in column - excel

I have the following code which works fine. (TextBoxes are on "UserForm" in Excel VBA, and executes with clicking CommandButton1 on that UserForm.) But can anyone help me to insert the value of each TextBox into the first empty cell in Column M? This way inserts empty rows. I will use this in several workbooks, so using "ActiveSheet" is easiest for me.
Sub UserForm_Initialize()
Me.TextBox1.Value = CStr(ActiveSheet.Range("M1").Value)
Me.TextBox2.Value = CStr(ActiveSheet.Range("M2").Value)
Me.TextBox3.Value = CStr(ActiveSheet.Range("M3").Value)
Me.TextBox4.Value = CStr(ActiveSheet.Range("M4").Value)
Me.TextBox5.Value = CStr(ActiveSheet.Range("M5").Value)
Me.TextBox6.Value = CStr(ActiveSheet.Range("M6").Value)
Me.TextBox7.Value = CStr(ActiveSheet.Range("M7").Value)
Me.TextBox8.Value = CStr(ActiveSheet.Range("M8").Value)
Me.TextBox9.Value = CStr(ActiveSheet.Range("M9").Value)
Me.TextBox10.Value = CStr(ActiveSheet.Range("M10").Value)
Me.TextBox11.Value = CStr(ActiveSheet.Range("M11").Value)
Me.TextBox12.Value = CStr(ActiveSheet.Range("M12").Value)
Me.TextBox13.Value = CStr(ActiveSheet.Range("M13").Value)
Me.TextBox14.Value = CStr(ActiveSheet.Range("M14").Value)
Me.TextBox15.Value = CStr(ActiveSheet.Range("M15").Value)
Me.TextBox16.Value = CStr(ActiveSheet.Range("M16").Value)
Me.TextBox17.Value = CStr(ActiveSheet.Range("M17").Value)
Me.TextBox18.Value = CStr(ActiveSheet.Range("M18").Value)
Me.TextBox19.Value = CStr(ActiveSheet.Range("M19").Value)
Me.TextBox20.Value = CStr(ActiveSheet.Range("M20").Value)
Me.TextBox21.Value = CStr(ActiveSheet.Range("M21").Value)
Me.TextBox22.Value = CStr(ActiveSheet.Range("M22").Value)
End Sub
'Insert Button
Private Sub CommandButton1_Click()
Range("M1") = Me.TextBox1.Text
Range("M2") = Me.TextBox2.Text
Range("M3") = Me.TextBox3.Text
Range("M4") = Me.TextBox4.Text
Range("M5") = Me.TextBox5.Text
Range("M6") = Me.TextBox6.Text
Range("M7") = Me.TextBox7.Text
Range("M8") = Me.TextBox8.Text
Range("M9") = Me.TextBox9.Text
Range("M10") = Me.TextBox10.Text
Range("M11") = Me.TextBox11.Text
Range("M12") = Me.TextBox12.Text
Range("M13") = Me.TextBox13.Text
Range("M14") = Me.TextBox14.Text
Range("M15") = Me.TextBox15.Text
Range("M16") = Me.TextBox16.Text
Range("M17") = Me.TextBox17.Text
Range("M18") = Me.TextBox18.Text
Range("M19") = Me.TextBox19.Text
Range("M20") = Me.TextBox20.Text
Range("M21") = Me.TextBox21.Text
Range("M22") = Me.TextBox22.Text
Range("A25").Value = "Hide"
Unload Me
End Sub
Sub CommandButton2_Click()
'Cancel button
Unload Me
End Sub

EDIT: last guess
You can do something like this:
Private Sub CommandButton1_Click()
Dim i As Long, c As Range
ActiveSheet.Range("M1:M22").ClearContents
Set c = ActiveSheet.Range("M1")
For i = 1 to 22
c.Value = Me.Controls("TextBox" & i).Text
If Len(c.Value) > 0 then Set c = c.offset(1, 0)
Next i
Unload Me
End Sub

Related

VBA MACRO Combobox is limited in data

I have a script in VBA (downloaded from internet and adjusted to my use). It is for Data Entry. But i have the problem with some Comboboxes. It is not alowing me to see the whole list of choises. It only shows 2 values instead of the 128 values.
I have tried to put it in a different column but still the same. At this moment C_06.List = [bom].Value is my problem!
Option Explicit
Dim Rng As Range, fnd As Range
Dim Ctrl As Control
Dim iRow As Integer
Dim wsAE As Worksheet
Private Sub C_02_Click()
T_ARR.Value = C_02.Column(1)
T_RENOV.Value = C_02.Column(3)
T_FINC.Value = C_02.Column(4)
T_AREA.Value = C_02.Column(9)
T_CALLE.Value = C_02.Column(11)
T_DIAS.Value = C_02.Column(12)
T_ESTATUS.Value = C_02.Column(13)
T_SUELO.Value = C_02.Column(15)
T_COLONIA.Value = C_02.Column(16)
T_CP.Value = C_02.Column(14)
T_ESTADO.Value = C_02.Column(17)
T_M2.Value = C_02.Column(20)
End Sub
Private Sub CMB_addnew_Click()
Set wsAE = Worksheets("INGRESOS")
If MsgBox("Correct entry?", vbYesNo + vbQuestion, "Check the data!") = vbNo Then Exit Sub
iRow = wsAE.Cells.Find(What:="*", SearchOrder:=xlRows, SearchDirection:=xlPrevious, LookIn:=xlValues).Row + 1
wsAE.Cells(iRow, 1).Resize(, 25).Value = Array(T_id.Value, C_02.Value, T_AREA.Value, T_ARR.Value, T_DATE.Value, C_CONC.Value, T_REFBANC.Value, T_PROYECTO.Value, T_CARGO.Value, T_RAD.Value, T_NRAD.Value, T_NPAGO.Value, C_METH.Value, T_RENOV.Value, T_FINC.Value, _
T_DIAS.Value, T_PAGARFEC.Value, T_SUELO.Value, T_ESTATUS.Value, T_M2.Value, T_CALLE.Value, T_COLONIA.Value, T_ESTADO.Value, T_CP.Value, _
, T_NOTA.Value)
'Columns.AutoFit
MsgBox "The new entry has been saved.", vbInformation, "Done"
For Each Ctrl In Controls
If TypeName(Ctrl) = "TextBox" Or TypeName(Ctrl) = "ComboBox" Then Ctrl.Value = ""
Next Ctrl
LB_01.ListIndex = -1
LB_01.TopIndex = 0
Call UserForm_Initialize
End Sub
Private Sub CMB_clear_Click()
For Each Ctrl In Controls
If TypeName(Ctrl) = "TextBox" Or TypeName(Ctrl) = "ComboBox" Then Ctrl.Value = ""
Next Ctrl
LB_01.ListIndex = -1
LB_01.TopIndex = 0
Call UserForm_Initialize
End Sub
Private Sub CMB_close_Click()
Unload Me
End Sub
Private Sub LB_01_Click()
T_id.Value = LB_01.Column(0)
C_02.Value = LB_01.Column(1)
T_ARR.Value = LB_01.Column(2)
T_CARGO.Value = LB_01.Column(3)
T_RENOV.Value = LB_01.Column(4)
T_FINC.Value = LB_01.Column(5)
C_METH.Value = LB_01.Column(6)
C_EMPTY.Value = LB_01.Column(7)
C_CONC.Value = LB_01.Column(8)
T_NRAD.Value = LB_01.Column(9)
T_NOTA.Value = LB_01.Column(10)
T_DIAS.Value = LB_01.Column(11)
T_PAGARFEC.Value = LB_01.Column(12)
T_NPAGO.Value = LB_01.Column(13)
C_06.Value = LB_01.Column(14)
C_BOM.Value = LB_01.Column(15)
T_CP.Value = LB_01.Column(16)
T_ESTADO.Value = LB_01.Column(17)
T_DATE.Value = LB_01.Column(18)
T_PROYECTO.Value = LB_01.Column(19)
T_RAD.Value = LB_01.Column(20)
T_AREA.Value = LB_01.Column(21)
T_CALLE.Value = LB_01.Column(22)
T_ESTATUS.Value = LB_01.Column(23)
T_SUELO.Value = LB_01.Column(24)
T_COLONIA.Value = LB_01.Column(25)
End Sub
Private Sub UserForm_Initialize()
T_id.Value = WorksheetFunction.Max([ids]) + 1
LB_01.List = [database].Value
C_02.List = [datalist].Value
C_CONC.List = [nouns].Value
C_BOM.List = [mrp].Value
C_METH.List = [BUoM].Value
C_06.List = [bom].Value
T_DATE.Value = Now
End Sub

How to retrieve check of checkbox after clicking listbox_click() in Excel VBA?

What it would like to happen, when listbox is click it would retrieve the check of checkbox from the worksheet.
Private Sub ListBox1_Click()
Dim say, lastrow As Long
TextBox15 = ListBox1.Column(0)
TextBox18 = ListBox1.Column(1)
TextBox1 = ListBox1.Column(2)
TextBox2 = ListBox1.Column(3)
TextBox19 = ListBox1.Column(4)
TextBox3 = ListBox1.Column(5)
TextBox24 = ListBox1.Column(6)
TextBox26 = ListBox1.Column(7)
TextBox5 = ListBox1.Column(8)
TextBox6 = ListBox1.Column(9)
TextBox7 = ListBox1.Column(10)
TextBox8 = ListBox1.Column(11)
TextBox23 = ListBox1.Column(12)
TextBox22 = ListBox1.Column(13)
TextBox4 = ListBox1.Column(14)
TextBox21 = ListBox1.Column(15)
TextBox20 = ListBox1.Column(16)
TextBox11 = ListBox1.Column(17)
TextBox12 = ListBox1.Column(18)
TextBox13 = ListBox1.Column(19)
TextBox14 = ListBox1.Column(20)
TextBox27 = ListBox1.Column(21)
TextBox28 = ListBox1.Column(22)
TextBox29 = ListBox1.Column(23)
TextBox25 = ListBox1.Column(24)
TextBox30 = ListBox1.Column(25)
TextBox31 = ListBox1.Column(26)
TextBox32 = ListBox1.Column(27)
TextBox33 = ListBox1.Column(28)
CheckBox1 = ListBox1.Column(29)
CheckBox2 = ListBox1.Column(30)
CheckBox3 = ListBox1.Column(31)
CheckBox4 = ListBox1.Column(32)
CheckBox5 = ListBox1.Column(33)
CheckBox6 = ListBox1.Column(34)
CheckBox7 = ListBox1.Column(35)
CheckBox8 = ListBox1.Column(36)
CheckBox9 = ListBox1.Column(37)
CheckBox10 = ListBox1.Column(38)
CheckBox11 = ListBox1.Column(39)
CheckBox12 = ListBox1.Column(40)
CheckBox13 = ListBox1.Column(41)
lastrow = Sheets("liste").Cells(Rows.Count, "D").End(xlUp).Row
Set sonsati = Sheets("liste").Range("D2:D" & lastrow).Find(What:=TextBox1.Text, LookIn:=xlValues, LookAt:=xlWhole)
say = sonsati.Row
Sheets("liste").Range("A" & say & ":AD" & say).Select
CommandButton1.Enabled = False
CommandButton2.Enabled = True
CommandButton3.Enabled = True
End Sub
I tried the code above but I am always prompted with an error of
"Run-time error '-2147024809', could not get the column property. Invalid argument.
in the listbox, it shows as True / False. – Crusin18 1 hour ago
You are strying to assign a String to a Boolean. Convert it to Boolean first and then try it. For example
CheckBox6.Value = CBool(ListBox1.Column(x))
CBool("True") will return Boolean True and CBool("False") will return Boolean False. If you think there can be spaces then try this
CheckBox6.Value = CBool(Trim(ListBox1.Column(x)))

Could not get column property.Invalid argument

Could net get column property. invalid argument
How not to populate the listbox and put all the data in textbox instead? Is there anyway to loop this?
Private Sub ListBox1_Click()
Dim say, LastRow As Long
TextBox1 = ListBox1.Column(0)
TextBox2 = ListBox1.Column(1)
TextBox3 = ListBox1.Column(2)
TextBox4 = ListBox1.Column(3)
TextBox5 = ListBox1.Column(4)
TextBox6 = ListBox1.Column(5)
TextBox7 = ListBox1.Column(6)
TextBox8 = ListBox1.Column(7)
TextBox11 = ListBox1.Column(8)
TextBox12 = ListBox1.Column(9)
TextBox13 = ListBox1.Column(10)
TextBox14 = ListBox1.Column(11)
TextBox15 = ListBox1.Column(12)
TextBox16 = ListBox1.Column(13)
TextBox17 = ListBox1.Column(14)
TextBox18 = ListBox1.Column(15)
TextBox19 = ListBox1.Column(16)
TextBox20 = ListBox1.Column(17)
TextBox21 = ListBox1.Column(18)
TextBox22 = ListBox1.Column(19)
TextBox23 = ListBox1.Column(20)
TextBox24 = ListBox1.Column(21)
TextBox25 = ListBox1.Column(22)
TextBox26 = ListBox1.Column(23)
LastRow = Sheets("Database").Cells(Rows.Count, "B").End(xlUp).Row
Sheets("Database").Range("B2:B" & LastRow).Find(What:=ListBox1.Value,
LookIn:=xlValues, LookAt:=xlWhole).Activate
say = ActiveCell.Row
Sheets("Database").Range("A" & say & ":Y" & say).Select
Have a look at the following demo:
Option Explicit
Private Sub UserForm_Initialize()
With ListBox1
.ColumnCount = 2
.AddItem
.List(.ListCount - 1, 0) = "Test"
.List(.ListCount - 1, 1) = "abc"
.AddItem
.List(.ListCount - 1, 0) = "Test2"
.List(.ListCount - 1, 1) = "def"
End With
End Sub
Private Sub ListBox1_Click()
Dim SelectedItem As Long
Dim j As Long
Dim ctrls As Variant
Dim TextBoxCounter As Long
SelectedItem = ListBoxSelectedIndex(Me.ListBox1)
ctrls = Array(TextBox1, TextBox2)
For j = 0 To Me.ListBox1.ColumnCount - 1
If j <> 9 And j <> 10 And j <= UBound(ctrls) Then
ctrls(j).Text = Me.ListBox1.List(SelectedItem, TextBoxCounter)
TextBoxCounter = TextBoxCounter + 1
End If
Next j
End Sub
Private Function ListBoxSelectedIndex(lBox As msforms.ListBox) As Long
With lBox
For ListBoxSelectedIndex = 0 To .ListCount - 1
If .Selected(ListBoxSelectedIndex) Then Exit For
Next ListBoxSelectedIndex
End With
End Function
This produces:
This can be easily expanded with the rest of your controls for your situation

VBA Run time Error '70': Could not Set the list property. Permission Denied

I am trying to update the data by clicking the Listbox,but getting error, please see my code below.
Using below code to display the list box value to text boxes
Private Sub ListBox1_Click()
Dim i As Integer
i = Me.ListBox1.ListIndex
Me.ListBox1.Selected(i) = True
Me.TextBox1.Value = Me.ListBox1.Column(0, i)
Me.TextBox2.Value = Me.ListBox1.Column(1, i)
Me.TextBox3.Value = Me.ListBox1.Column(2, i)
Me.TextBox4.Value = Me.ListBox1.Column(3, i)
End Sub
Trying to update data with following code and getting error.
Private Sub btnUpdate_Click()
If ListBox1.ListIndex <> -1 Then
With ListBox1
.List(.ListIndex, 0) = TextBox1.Value
.List(.ListIndex, 1) = TextBox2.Value
.List(.ListIndex, 2) = TextBox3.Value
.List(.ListIndex, 3) = TextBox4.Value
End With
End If
End Sub
enter code here
Any help will be greatly appreciated.
Unlike ComboBoxes, you can't edit values in a ListBox on the fly like this. You have to delete the entry and insert one back in, with your new values. I'm not sure this is the most elegant way to do it, but the following works:
Private Sub btnUpdate_Click()
Dim values(3)
Dim u As Long
u = ListBox1.ListIndex
If u <> -1 Then
values(0) = TextBox1.Value
values(1) = TextBox2.Value
values(2) = TextBox3.Value
values(3) = TextBox4.Value
With ListBox1
.RemoveItem u
.AddItem values(0), u
.List(u, 1) = values(1)
.List(u, 2) = values(2)
.List(u, 3) = values(3)
End With
End If
End Sub
Private Sub ListBox1_Click()
Dim i As Long
With Me
i = .ListBox1.ListIndex
.ListBox1.Selected(i) = True
.TextBox1.Value = .ListBox1.Column(0, i)
.TextBox2.Value = .ListBox1.Column(1, i)
.TextBox3.Value = .ListBox1.Column(2, i)
.TextBox4.Value = .ListBox1.Column(3, i)
End With
End Sub
As I say, not elegant: I had to store the values of TextBox1 etc. in the array as when the RemoveItem runs, the Index changes which causes the ListBox1_Click to run - resetting all the Textboxes.

Excel Sharing sheet with vba and working buttons

I'm trying to share a sheet with a button that open a new window(made in vb) but when I share it the button that guides to another window lock simple doesn't work (yes I unchecked lock), theres something I need to do to share that button and the another window?
The work of this windows is to edit cells.
Here's the code from the window, there's:
private Sub CommandButton1_Click()
Dim i, a, b, c, d
Dim prio As String
i = CInt(Sheets("Sheet1").Cells(6, 17))
a = CInt(Sheets("Sheet1").Cells(7, 17))
Sheets("Sheet1").Cells(i, 1) = a
Sheets("Sheet1").Cells(i, 2) = Peca.Text
Sheets("Sheet1").Cells(i, 3) = Qt.Text
Sheets("Sheet1").Cells(i, 4) = ComboBox1.Value
Sheets("Sheet1").Cells(i, 5) = Responsavel.Text
Sheets("Sheet1").Cells(i, 6) = Cliente.Text
Sheets("Sheet1").Cells(i, 7) = Maquina.Text
Sheets("Sheet1").Cells(i, 8) = NumSerie.Text
Sheets("Sheet1").Cells(i, 9) = Modelo.Text
Sheets("Sheet1").Cells(i, 10) = Obser.Text
Sheets("Sheet1").Cells(6, 17) = CInt(Sheets("Sheet1").Cells(6, 17)) + 1
Sheets("Sheet1").Cells(7, 17) = CInt(Sheets("Sheet1").Cells(7, 17)) + 1
Peca.Text = ""
Qt.Text = ""
ComboBox1.Value = ""
Responsavel.Text = ""
Cliente.Text = ""
Maquina.Text = ""
NumSerie.Text = ""
Modelo.Text = ""
Obser.Text = ""
End Sub
Private Sub CommandButton2_Click()
Sheets("Sheet1").Cells(12, 15) = Cliente.Text
End Sub
Private Sub CommandButton3_Click()
Sheets("Sheet1").Cells(12, 15) = Maquina.Text
Sheets("Sheet1").Cells(13, 15) = NumSerie.Text
Sheets("Sheet1").Cells(14, 15) = Modelo.Text
End Sub
Private Sub CommandButton4_Click()
Cliente.Text = Sheets("Sheet1").Cells(12, 15)
End Sub
Private Sub CommandButton5_Click()
Maquina.Text = Sheets("Sheet1").Cells(12, 15)
NumSerie.Text = Sheets("Sheet1").Cells(13, 15)
Modelo.Text = Sheets("Sheet1").Cells(14, 15)
End Sub

Resources