我仍然是VBA编程的新学习者,并且遇到了一个我似乎无法找到解决方案的问题。我正在尝试创建一个工作簿来处理食谱。我打电话给一个用户窗体,用来在一个宏中添加一个配方,并带有几个文本框输入(〜96个)。当我输入所有成分,数量,单位并按OK按钮时,我希望它将用户表单中的内容写入工作表。所有文本框以升序命名(例如,.txtIngredient1,.txtIngredient2,...)。是否可以使用for循环将Me.txtIngredientX.Value复制到工作表上的范围?for Excel循环中的Excel VBA增量变量
我目前的代码片段:
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是宏中的一个变量,它决定了下一个配方应该插入的位置。
任何帮助,非常感谢!
谢谢!
只是想知道如果'范围( “B” &addatrow - 1 + I)',不应该是'range(“B”&(addatrow - 1 + i))'?你也可以使用'Cells(addatrow - 1 + i,2)' – 2014-11-07 18:20:46