I'm still a new learner of VBA programming, and I've come across an issue that I can't seem to find a solution to. I am trying to create a workbook to handle recipes. I am calling a user form used to add a recipe in a macro with several text box inputs (~96 of them). When I've input all ingredients, quantity, units and press the OK button, I want it to write what's in the user form to the worksheet. All text boxes are named ending with a number in ascending order (e.g. .txtIngredient1, .txtIngredient2, ...). Is it possible to use a for loop to copy the Me.txtIngredientX.Value to a range on a sheet?
A snippet of my current code:
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
I have tried the only thing I know to do which is something like this:
for i = 1 to 32
Sheets("Recipes").Range("B" & addatrow - 1 + i) = me.txtIngredient & i.value
which does not work. Note: addatrow is a variable in the macro that dictates where the next recipe should be inserted.
Any help is greatly appreciated!
Thanks!
range("B" & addatrow - 1 + i), shouldn't berange("B" & (addatrow - 1 + i))?? Also you could useCells (addatrow - 1 + i,2)