2014-11-06 166 views
2

我仍然是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是宏中的一个变量,它决定了下一个配方应该插入的位置。

任何帮助,非常感谢!

谢谢!

+0

只是想知道如果'范围( “B” &addatrow - 1 + I)',不应该是'range(“B”&(addatrow - 1 + i))'?你也可以使用'Cells(addatrow - 1 + i,2)' – 2014-11-07 18:20:46

回答

4

试试这个:

注意nameForm必须是你的表单的名称,随着Me似乎不起作用。

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

这工作!将90多行代码转换为10左右!谢谢! – 2014-11-07 00:38:54

+1

@Jose,只要您在用户表单代码的页面中,“Me”对于任何模块子代码,您需要使用Form的全局名称,或者将其从参数传递给sub,如下所示:'call Mysub(arg1,arg2,Me)',然后在模块中:'sub Mysub(byval arg1 as Long,byval arg2 as String,byref Form as UserForm)''。注意'Byref'用于保存用户窗体的任何更改。 – 2014-11-07 18:09:39

-1

如何:

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工作,希望它的工作原理)

+0

为什么通过控件循环,如果你已经知道它的名字?双循环没有意义。另外,在VBA中,当您编写“ffff”&i时,您不需要将其转换为字符串,应用程序自动执行此操作。 – 2014-11-07 18:19:11

+1

旁边没有可用的VBA。 VBA!= Vb.Net – RubberDuck 2014-11-07 18:44:01

1

这个代码必须链接到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 
+0

我知道使用VBA阵列并且一举将其写入是一件大事,但我喜欢快速的事情^^ – 2014-11-07 18:39:33