adding a variable to a variable in a loop

gtopmafia

New Member
Joined
Jun 8, 2018
Messages
23
Hi Guys Im trying to make a Loop function for the following I want to make the payPer iterate so payPer1, payPer2 etc.

Any idea how I can do this? cheers.

pmtCount = 0
incremental = 0
x = 1
Do While pmtCount < 20
Cells(Rows.Count, 22).End(xlUp).Offset(0, 0).Select
ActiveCell.Offset(0, incremental).Select
ActiveCell.Value = payPer+(what here)
incremental = incremental + 1
pmtCount = pmtCount + 1
x = x + 1
Loop
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
It is not necessary to actually select the cells, and doing so will actually slow your code down.
Try this:
Code:
    pmtCount = 0
    incremental = 0
    x = 1

    Do While pmtCount < 20
        Cells(Rows.Count, 22).End(xlUp).Offset(0, incremental).Value = "payPer" & x
        incremental = incremental + 1
        pmtCount = pmtCount + 1
        x = x + 1
    Loop
 
Upvote 0
Hi Cheers for help so far, I need to adapt it to somthing more like this but this isnt working, CPS is a userform, and when i do "CPS.payPer & x" it literally returns "CPS.payPer1", "CPS.payPer2" etc in the excel cell and not the value attributable for example 42.

Do While pmtCount < 20
Cells(Rows.Count, 22).End(xlUp).Offset(0, incremental).Value = CPS.payPer & x
incremental = incremental + 1
pmtCount = pmtCount + 1
x = x + 1
Loop
End Sub
 
Upvote 0
is there a way to maybe make a loop before to generate a variable to use in the loop i posted above? maybe that would be more efficient
 
Upvote 0
as a side not when declaring variables how can i put the declarations on the same line can you split them with a comma or?
 
Upvote 0
is there a way to maybe make a loop before to generate a variable to use in the loop i posted above? maybe that would be more efficient
I am not sure what you mean.

as a side not when declaring variables how can i put the declarations on the same line can you split them with a comma or?
You can separate them with a comma, but note that each needs to be declared explicitly.

This is the wrong way to declare a, b, and c as Integers:
Code:
Dim a, b, c as Integer

This is the right way:
Code:
Dim a as Integer, b as Integer, c as Integer

In the first example, only c would be declared as an Integer. a and b would be declared as Variant.
 
Upvote 0
further comment, sorry for spam, Ive adjusted the script to make the variable payPer have a value but I still want to loop with the +1 so how would I do this?
 
Upvote 0
I am not sure what you mean.


You can separate them with a comma, but note that each needs to be declared explicitly.

This is the wrong way to declare a, b, and c as Integers:
Code:
Dim a, b, c as Integer

This is the right way:
Code:
Dim a as Integer, b as Integer, c as Integer

In the first example, only c would be declared as an Integer. a and b would be declared as Variant.


Ok thanks
 
Upvote 0
Ive adjusted the script to make the variable payPer have a value but I still want to loop with the +1 so how would I do this?
Once again, I am unclear what you mean. How is this different that anything we have already discussed?
If you changed anything, please post your new code and explain exactly what it is you are trying to do now.
 
Upvote 0

Forum statistics

Threads
1,224,822
Messages
6,181,165
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top