我正在制作一个ERP系统,我的代码可以工作,但是很丑陋,可以满足我的需求。我只是第一次尝试使用数组,使用该数组时,即使我为该数组分配了9个以上的文本框,也只能将第一个文本框输入到“存档”电子表格中。有点难以解释,但更容易理解我在代码中要做什么。
我尝试将nextRow变量的“ B2”更改为“ C2”,但是它仍然只给我第一个文本框输入,并将其粘贴到nextRow变量中首先提到的任何单元格中。
(此代码是在codereview网站上提供给我的,但我也尝试过进行一些更改,但没有任何运气。
'Making the variable that stores the input from all 9 textboxes in the userform
Dim inputs As Variant
'Variabler for inputs
inputs = Array(TextBox1.Text, TextBox2.Text, TextBox3.Text, TextBox4.Text, TextBox10.Text, TextBox6.Text, TextBox7.Text, TextBox8.Text, TextBox9.Text)
'Declaring the variable for the next available row from B to J so I can copy the input from each textbox into the corresponding cell in the Archive worksheet.
Dim nextRowB As Range
Set nextRowB = Sheets("Arkiv").Range("C" & Rows.Count & ":J" & Rows.Count).End(xlUp).Offset(1, 0)
nextRowB.Value = inputs
与其将每个文本框输入复制到相应的单元格,不如将第一个文本框输入(textbox1)复制到下一个可用行的第一列,然后将其余部分保留为空。
我期望这段代码将每个文本框输入粘贴到B列中的下一个可用行,一直到J,使用的顺序与将不同文本框输入分配给数组时使用的顺序相同。
答案 0 :(得分:1)
可以尝试这样
Dim inputs As Variant
inputs = Array(TextBox1.Text, TextBox2.Text, TextBox3.Text, TextBox4.Text, TextBox10.Text, TextBox6.Text, TextBox7.Text, TextBox8.Text, TextBox9.Text)
Dim nextRowB As Range
Set nextRowB = Sheets("Arkiv").Range("B" & Rows.Count).End(xlUp).Offset(1, 0)
nextRowB.Resize(1, UBound(inputs) + 1).Value = inputs