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

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
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,222,436
Messages
6,166,020
Members
452,008
Latest member
Customlogoflipflops

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