从不同的文本框向列插入值 vba
Insert value to column from diffrent textbox vba
我有一个机器列表,上面有 20 个机器零件,我希望能够插入排序的列表,但是当我想插入 10 个不同的零件机器名称时,它就是行不通(它只留下空白单元格而不是值),也有可能使这段代码更简单的方法,也许使用 for 循环。
任何帮助将不胜感激
Set dest = ThisWorkbook.Worksheets("Daftar Bagian Mesin")
LastRow = dest.Cells(dest.Rows.Count, "A").End(xlUp).Offset(1).Row
'----------- Part Machine Names -----------'
Cells(LastRow, 1).Value = TextBox6.Value
Cells(LastRow, 1).Value = TextBox9.Value
Cells(LastRow, 1).Value = TextBox12.Value
Cells(LastRow, 1).Value = TextBox15.Value
Cells(LastRow, 1).Value = TextBox18.Value
Cells(LastRow, 1).Value = TextBox21.Value
Cells(LastRow, 1).Value = TextBox24.Value
Cells(LastRow, 1).Value = TextBox27.Value
Cells(LastRow, 1).Value = TextBox30.Value
Cells(LastRow, 1).Value = TextBox33.Value
Cells(LastRow, 1).Value = TextBox36.Value
Cells(LastRow, 1).Value = TextBox39.Value
Cells(LastRow, 1).Value = TextBox42.Value
Cells(LastRow, 1).Value = TextBox45.Value
Cells(LastRow, 1).Value = TextBox48.Value
Cells(LastRow, 1).Value = TextBox51.Value
Cells(LastRow, 1).Value = TextBox54.Value
Cells(LastRow, 1).Value = TextBox57.Value
Cells(LastRow, 1).Value = TextBox60.Value
Cells(LastRow, 1).Value = TextBox63.Value
'-----------------------------------------'
'----------- Part Machine Code -----------'
Cells(LastRow, 2).Value = TextBox7.Value
Cells(LastRow, 2).Value = TextBox10.Value
Cells(LastRow, 2).Value = TextBox13.Value
Cells(LastRow, 2).Value = TextBox16.Value
Cells(LastRow, 2).Value = TextBox19.Value
Cells(LastRow, 2).Value = TextBox22.Value
Cells(LastRow, 2).Value = TextBox25.Value
Cells(LastRow, 2).Value = TextBox28.Value
Cells(LastRow, 2).Value = TextBox31.Value
Cells(LastRow, 2).Value = TextBox34.Value
Cells(LastRow, 2).Value = TextBox37.Value
Cells(LastRow, 2).Value = TextBox40.Value
Cells(LastRow, 2).Value = TextBox43.Value
Cells(LastRow, 2).Value = TextBox46.Value
Cells(LastRow, 2).Value = TextBox49.Value
Cells(LastRow, 2).Value = TextBox52.Value
Cells(LastRow, 2).Value = TextBox55.Value
Cells(LastRow, 2).Value = TextBox58.Value
Cells(LastRow, 2).Value = TextBox61.Value
Cells(LastRow, 2).Value = TextBox64.Value
'-----------------------------------------'
'-------- Notes On Part Machines --------'
Cells(LastRow, 3).Value = TextBox8.Value
Cells(LastRow, 3).Value = TextBox11.Value
Cells(LastRow, 3).Value = TextBox14.Value
Cells(LastRow, 3).Value = TextBox17.Value
Cells(LastRow, 3).Value = TextBox20.Value
Cells(LastRow, 3).Value = TextBox23.Value
Cells(LastRow, 3).Value = TextBox26.Value
Cells(LastRow, 3).Value = TextBox29.Value
Cells(LastRow, 3).Value = TextBox32.Value
Cells(LastRow, 3).Value = TextBox35.Value
Cells(LastRow, 3).Value = TextBox38.Value
Cells(LastRow, 3).Value = TextBox41.Value
Cells(LastRow, 3).Value = TextBox44.Value
Cells(LastRow, 3).Value = TextBox47.Value
Cells(LastRow, 3).Value = TextBox50.Value
Cells(LastRow, 3).Value = TextBox53.Value
Cells(LastRow, 3).Value = TextBox56.Value
Cells(LastRow, 3).Value = TextBox59.Value
Cells(LastRow, 3).Value = TextBox62.Value
Cells(LastRow, 3).Value = TextBox65.Value
'----------------------------------------'
我建议按如下方式重命名用户表单中的文本框
'----------- Part Machine Names -----------'
TextBox6 ' rename to txtPartMachineNames1
TextBox9 ' rename to txtPartMachineNames2
TextBox12 ' rename to txtPartMachineNames3
TextBox15 ' rename to txtPartMachineNames4
TextBox18 ' rename to txtPartMachineNames5
TextBox21 ' rename to txtPartMachineNames6
TextBox24 ' rename to txtPartMachineNames7
TextBox27 ' rename to txtPartMachineNames8
TextBox30 ' rename to txtPartMachineNames9
TextBox33 ' rename to txtPartMachineNames10
TextBox36 ' rename to txtPartMachineNames11
TextBox39 ' rename to txtPartMachineNames12
TextBox42 ' rename to txtPartMachineNames13
TextBox45 ' rename to txtPartMachineNames14
TextBox48 ' rename to txtPartMachineNames15
TextBox51 ' rename to txtPartMachineNames16
TextBox54 ' rename to txtPartMachineNames17
TextBox57 ' rename to txtPartMachineNames18
TextBox60 ' rename to txtPartMachineNames19
TextBox63 ' rename to txtPartMachineNames20
'-----------------------------------------'
Part Machine Code
的文本框
TextBox7 ' rename to txtPartMachineCode1
TextBox10 ' rename to txtPartMachineCode2
' and so on (you get the idea).
然后您可以对所有三种类型的文本框(Part Machine Names
、Part Machine Code
和 Notes On Part Machines
)使用一个循环:
Dim dest As Worksheet
Set dest = ThisWorkbook.Worksheets("Daftar Bagian Mesin")
Dim LastRow As Long
LastRow = dest.Cells(dest.Rows.Count, "A").End(xlUp).Offset(1).Row
Dim i As Long
For i = 1 To 20
Cells(LastRow, 1).Offset(RowOffset:=i-1).Value = Controls("txtPartMachineNames" & i).Value
Cells(LastRow, 2).Offset(RowOffset:=i-1).Value = Controls("txtPartMachineCode" & i).Value
Cells(LastRow, 2).Offset(RowOffset:=i-1).Value = Controls("txtNotesOnPartMachine" & i).Value
next i
我有一个机器列表,上面有 20 个机器零件,我希望能够插入排序的列表,但是当我想插入 10 个不同的零件机器名称时,它就是行不通(它只留下空白单元格而不是值),也有可能使这段代码更简单的方法,也许使用 for 循环。
任何帮助将不胜感激
Set dest = ThisWorkbook.Worksheets("Daftar Bagian Mesin")
LastRow = dest.Cells(dest.Rows.Count, "A").End(xlUp).Offset(1).Row
'----------- Part Machine Names -----------'
Cells(LastRow, 1).Value = TextBox6.Value
Cells(LastRow, 1).Value = TextBox9.Value
Cells(LastRow, 1).Value = TextBox12.Value
Cells(LastRow, 1).Value = TextBox15.Value
Cells(LastRow, 1).Value = TextBox18.Value
Cells(LastRow, 1).Value = TextBox21.Value
Cells(LastRow, 1).Value = TextBox24.Value
Cells(LastRow, 1).Value = TextBox27.Value
Cells(LastRow, 1).Value = TextBox30.Value
Cells(LastRow, 1).Value = TextBox33.Value
Cells(LastRow, 1).Value = TextBox36.Value
Cells(LastRow, 1).Value = TextBox39.Value
Cells(LastRow, 1).Value = TextBox42.Value
Cells(LastRow, 1).Value = TextBox45.Value
Cells(LastRow, 1).Value = TextBox48.Value
Cells(LastRow, 1).Value = TextBox51.Value
Cells(LastRow, 1).Value = TextBox54.Value
Cells(LastRow, 1).Value = TextBox57.Value
Cells(LastRow, 1).Value = TextBox60.Value
Cells(LastRow, 1).Value = TextBox63.Value
'-----------------------------------------'
'----------- Part Machine Code -----------'
Cells(LastRow, 2).Value = TextBox7.Value
Cells(LastRow, 2).Value = TextBox10.Value
Cells(LastRow, 2).Value = TextBox13.Value
Cells(LastRow, 2).Value = TextBox16.Value
Cells(LastRow, 2).Value = TextBox19.Value
Cells(LastRow, 2).Value = TextBox22.Value
Cells(LastRow, 2).Value = TextBox25.Value
Cells(LastRow, 2).Value = TextBox28.Value
Cells(LastRow, 2).Value = TextBox31.Value
Cells(LastRow, 2).Value = TextBox34.Value
Cells(LastRow, 2).Value = TextBox37.Value
Cells(LastRow, 2).Value = TextBox40.Value
Cells(LastRow, 2).Value = TextBox43.Value
Cells(LastRow, 2).Value = TextBox46.Value
Cells(LastRow, 2).Value = TextBox49.Value
Cells(LastRow, 2).Value = TextBox52.Value
Cells(LastRow, 2).Value = TextBox55.Value
Cells(LastRow, 2).Value = TextBox58.Value
Cells(LastRow, 2).Value = TextBox61.Value
Cells(LastRow, 2).Value = TextBox64.Value
'-----------------------------------------'
'-------- Notes On Part Machines --------'
Cells(LastRow, 3).Value = TextBox8.Value
Cells(LastRow, 3).Value = TextBox11.Value
Cells(LastRow, 3).Value = TextBox14.Value
Cells(LastRow, 3).Value = TextBox17.Value
Cells(LastRow, 3).Value = TextBox20.Value
Cells(LastRow, 3).Value = TextBox23.Value
Cells(LastRow, 3).Value = TextBox26.Value
Cells(LastRow, 3).Value = TextBox29.Value
Cells(LastRow, 3).Value = TextBox32.Value
Cells(LastRow, 3).Value = TextBox35.Value
Cells(LastRow, 3).Value = TextBox38.Value
Cells(LastRow, 3).Value = TextBox41.Value
Cells(LastRow, 3).Value = TextBox44.Value
Cells(LastRow, 3).Value = TextBox47.Value
Cells(LastRow, 3).Value = TextBox50.Value
Cells(LastRow, 3).Value = TextBox53.Value
Cells(LastRow, 3).Value = TextBox56.Value
Cells(LastRow, 3).Value = TextBox59.Value
Cells(LastRow, 3).Value = TextBox62.Value
Cells(LastRow, 3).Value = TextBox65.Value
'----------------------------------------'
我建议按如下方式重命名用户表单中的文本框
'----------- Part Machine Names -----------'
TextBox6 ' rename to txtPartMachineNames1
TextBox9 ' rename to txtPartMachineNames2
TextBox12 ' rename to txtPartMachineNames3
TextBox15 ' rename to txtPartMachineNames4
TextBox18 ' rename to txtPartMachineNames5
TextBox21 ' rename to txtPartMachineNames6
TextBox24 ' rename to txtPartMachineNames7
TextBox27 ' rename to txtPartMachineNames8
TextBox30 ' rename to txtPartMachineNames9
TextBox33 ' rename to txtPartMachineNames10
TextBox36 ' rename to txtPartMachineNames11
TextBox39 ' rename to txtPartMachineNames12
TextBox42 ' rename to txtPartMachineNames13
TextBox45 ' rename to txtPartMachineNames14
TextBox48 ' rename to txtPartMachineNames15
TextBox51 ' rename to txtPartMachineNames16
TextBox54 ' rename to txtPartMachineNames17
TextBox57 ' rename to txtPartMachineNames18
TextBox60 ' rename to txtPartMachineNames19
TextBox63 ' rename to txtPartMachineNames20
'-----------------------------------------'
Part Machine Code
TextBox7 ' rename to txtPartMachineCode1
TextBox10 ' rename to txtPartMachineCode2
' and so on (you get the idea).
然后您可以对所有三种类型的文本框(Part Machine Names
、Part Machine Code
和 Notes On Part Machines
)使用一个循环:
Dim dest As Worksheet
Set dest = ThisWorkbook.Worksheets("Daftar Bagian Mesin")
Dim LastRow As Long
LastRow = dest.Cells(dest.Rows.Count, "A").End(xlUp).Offset(1).Row
Dim i As Long
For i = 1 To 20
Cells(LastRow, 1).Offset(RowOffset:=i-1).Value = Controls("txtPartMachineNames" & i).Value
Cells(LastRow, 2).Offset(RowOffset:=i-1).Value = Controls("txtPartMachineCode" & i).Value
Cells(LastRow, 2).Offset(RowOffset:=i-1).Value = Controls("txtNotesOnPartMachine" & i).Value
next i