vba code problem

shodan

Active Member
Joined
Jul 6, 2005
Messages
486
Hi there, this is the code I've written:

Code:
Sub mu_aan_cost_zero()
Dim mijnbereik As Range
Dim aantalrij, volgenderij As Integer

Worksheets(1).Activate

'copy first row
    Cells(1, 1).Copy
    Range(Cells(2, 1), Cells(12, 1)).Select
    Selection.Insert Shift:=xlDown

Set mijnbereik = Sheets(1).Range("A1").CurrentRegion
aantalrij = mijnbereik.Rows.Count

'copy next rows

For aantalrij = 1 To aantalrij 'moet hier komen
    
    volgenderij = (aantalrij * 12) + 1
    Cells(volgenderij, 1).Copy
    Range(Cells(volgenderij + 1, 1), Cells(volgenderij + 11, 1)).Select
    Selection.Insert Shift:=xlDown
 

Next aantalrij
End Sub

Now If I count the number of rows with "aantalrij" the result is 512 what is ok. And now I expected the macro to repeat 512 times the same procedure, but it only do 2 lines and than it leaves the procedure. What am I overlooking here?

Thanks
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
When you use
Code:
For aantalrij = 1 To aantalrij

What you are doing is resetting aantalrij to 1--you're losing your count of 512 entirely.

Just use a different variable, like

Code:
For i = 1 To aantalrij
     'code
Next i
 
Upvote 0
You're changing the value of aantalrij. Try using two variables:

Code:
For new_variable= 1 To aantalrij 'moet hier komen 
    
    volgenderij = (new_variable* 12) + 1 
    Cells(volgenderij, 1).Copy 
    Range(Cells(volgenderij + 1, 1), Cells(volgenderij + 11, 1)).Select 
    Selection.Insert Shift:=xlDown 
  

Next new_variable
 
Upvote 0

Forum statistics

Threads
1,214,827
Messages
6,121,806
Members
449,048
Latest member
greyangel23

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