2017-07-31 104 views
0

我是新手,這是我的第一篇文章。我不得不使用這種代碼很多,所以一些指導將非常感激。我不確定如何獲得最佳標題/問這個問題,但我想簡化Excel VBA中的代碼。在對象名稱中使用變量

txtItem1.Value = txtItem2.Value 
txtItem2.Value = txtItem3.Value 
txtItem3.Value = txtItem4.Value 
txtItem4.Value = txtItem5.Value 
txtItem5.Value = txtItem6.Value 
txtItem6.Value = txtItem7.Value 
txtItem7.Value = txtItem8.Value 
txtItem8.Value = txtItem9.Value 
txtItem9.Value = txtItem10.Value 

*一旦我有一個更好的理解,我可以更新標題,使其他人更有用。

+0

*我想簡化以下*代碼 - 你要如何簡化呢? [Code Review](http://codereview.stackexchange.com)可能的候選人 –

回答

1

假設你正在使用Excel文本框和你的命名約定保持連續的,你可以做這樣的事情:

For i = 1 To 9 
    Me.Controls("txtItem" & i) = Me.Controls("txtItem" & i + 1).Value 
Next