Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel VBA for循环中的递增变量_Vba_Excel_Userform - Fatal编程技术网

Excel VBA for循环中的递增变量

Excel VBA for循环中的递增变量,vba,excel,userform,Vba,Excel,Userform,我还是VBA编程的新手,遇到了一个似乎无法解决的问题。我正在尝试创建一个工作簿来处理食谱。我正在调用一个用户表单,该表单用于在包含多个文本框输入(其中约96个)的宏中添加配方。当我输入所有成分、数量、单位并按下OK按钮后,我希望它将用户表单中的内容写入工作表。所有文本框都以升序数字结尾(例如,..txtIngredient1、.txtIngredient2、…)命名。是否可以使用for循环将Me.txtIngredientX.Value复制到工作表上的某个范围 我当前代码的一个片段: Sheet

我还是VBA编程的新手,遇到了一个似乎无法解决的问题。我正在尝试创建一个工作簿来处理食谱。我正在调用一个用户表单,该表单用于在包含多个文本框输入(其中约96个)的宏中添加配方。当我输入所有成分、数量、单位并按下OK按钮后,我希望它将用户表单中的内容写入工作表。所有文本框都以升序数字结尾(例如,..txtIngredient1、.txtIngredient2、…)命名。是否可以使用for循环将Me.txtIngredientX.Value复制到工作表上的某个范围

我当前代码的一个片段:

Sheets("Recipes").Range("B" & addatrow + 0) = Me.txtIngredient1.Value
Sheets("Recipes").Range("B" & addatrow + 1) = Me.txtIngredient2.Value
Sheets("Recipes").Range("B" & addatrow + 2) = Me.txtIngredient3.Value
Sheets("Recipes").Range("B" & addatrow + 3) = Me.txtIngredient4.Value
Sheets("Recipes").Range("B" & addatrow + 4) = Me.txtIngredient5.Value
Sheets("Recipes").Range("B" & addatrow + 5) = Me.txtIngredient6.Value
Sheets("Recipes").Range("B" & addatrow + 6) = Me.txtIngredient7.Value
Sheets("Recipes").Range("B" & addatrow + 7) = Me.txtIngredient8.Value
Sheets("Recipes").Range("B" & addatrow + 8) = Me.txtIngredient9.Value
Sheets("Recipes").Range("B" & addatrow + 9) = Me.txtIngredient10.Value
Sheets("Recipes").Range("B" & addatrow + 10) = Me.txtIngredient11.Value
Sheets("Recipes").Range("B" & addatrow + 11) = Me.txtIngredient12.Value
Sheets("Recipes").Range("B" & addatrow + 12) = Me.txtIngredient13.Value
我已经尝试过我所知道的唯一一件事,那就是:

for i = 1 to 32
    Sheets("Recipes").Range("B" & addatrow - 1 + i) = me.txtIngredient & i.value
这是行不通的。注意:addatrow是宏中的一个变量,指示下一个配方应插入的位置

非常感谢您的帮助

谢谢

那么:

for i as integer = 1 to 32
    for each c as control in me.controls
        if typeof(c) is textbox andAlso c.name = "txtIngredient" & i.tostring then
            Sheets("Recipes").Range("B" & addatrow - 1 + i) = c.value
            exit for
        end if
    next c
next i
好问题,我经常想自己做这种事情:)

(这是VB.NET代码,不知道其中有多少可以在VBA中使用,希望它可以使用)

试试这个:

请注意,
nameForm
必须是表单的名称,而
Me
似乎不起作用

for i = 1 to 32
    Sheets("Recipes").Range("B" & addatrow - 1 + i) = nameForm.Controls("txtIngredient" & i).Value

此代码必须链接到UserForm代码页内的Ok按钮:

Sub Button_OK_Click 'Assuming the 'Ok' button is called Button_Ok on the userform

dim DATA () 'automatically declared as Variant, need to be Variant.
dim Sh as worksheet
dim i& ' declared as Long , this is our looping variable
redim DATA (1 to 96, 1 to 1) 'a one colone array with 96 rows
set Sh = thisworkbook.Sheets("Recipes")

with Me
    for i = 1 to 96
        DATA (i,1) = .Controls("txtIngredient" & i).Value
    next i
end with

with application
    .screenupdating = false
    .enableevents = false
    .calculation = XlManual
end with


with Sh
    .Range( .cells(addatrow,2) , .cells (addatrow+95 , 2) ).value2 = DATA 'write it to the sheet in a single line
end with

erase DATA
Set Sh = Nothing

with application
    .screenupdating = true
    .enableevents = true
    .calculation = XlAutomatic
end with

end sub

这成功了!将90多行代码转换为大约10行!谢谢大家!@Jose,
Me
只要你在用户表单代码的页面上就可以工作;对于任何模块sub的代码,您需要使用表单的全局名称,或者在参数中将其传递(从表单传递到模块),如下所示:
调用Mysub(arg1,arg2,Me)
,然后在模块中:
sub Mysub(byval arg1为长,byval arg2为字符串,byref Form为UserForm)
。请注意,
Byref
用于保存对UserForm的任何更改。如果您已经知道控件的名称,为什么还要循环使用这些控件?双重循环毫无意义。另外,在VBA中,在编写“ffff”和“i”时,不需要将其转换为字符串,而是由应用程序自动完成。只是想知道
范围(“B”和addatrow-1+i)
,不应该是
范围(“B”和(addatrow-1+i))
??您也可以使用
单元格(addatrow-1+i,2)
我知道使用VBA数组并一下子将其写入是一件非常过分的事情,但我喜欢快速完成^^