Invalid Next Control Variable Reference

VBABeginner1

New Member
Joined
Dec 6, 2014
Messages
31
I am currently getting an error when I run this on the "Next i" section. If I change it to "Next" and remove the "i", then I don't get an error, but it loops the "i", but not the "i"

I am trying to concatenate 2 cells from one sheet and move them into another on a separate sheet


Code:
Sub Concate()


Dim i As Long
Dim c As Long
Dim final As Long




Dim WS1 As Worksheet
Dim WS2 As Worksheet


Set WS1 = Worksheets("BEFORE AND AFTER")
Set WS2 = Worksheets("COMPARE")


For i = 3 To 10003
For c = 4 To 10004


WS2.Cells(i, 226) = WS1.Cells(c, 194) & " " & WS1.Cells(c, 195)




Next i
Next c
 


End Sub
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
You've got the 2 Next lines the wrong way round. if you indent your code like this
Code:
   For i = 3 To 10003
      For c = 4 To 10004
         WS2.Cells(i, 226) = WS1.Cells(c, 194) & " " & WS1.Cells(c, 195)
      Next c
   Next i
it helps to spot mistakes of that sort
 
Upvote 0
Looking at your code more closely, I think you can get rid of 1 of the loops like
Code:
   For i = 3 To 10003
      WS2.Cells(i, 226) = WS1.Cells(i, 194) & " " & WS1.Cells(, 195)
   Next i
 
Upvote 0
Next c
Next i
when I do it this way, it won't loop through the "i", just the "c"

I need the i and c references to be different as they pull data from separate sheets that are formatted differently.
 
Last edited:
Upvote 0
It does loop through i, but it's got to loop through c first (ie all 10,001 rows).
Also if you run with both loops, WS2 will have the same value all the way the column.
 
Upvote 0
Looking at your code more closely, I think you can get rid of 1 of the loops like
Code:
   For i = 3 To 10003
      WS2.Cells(i, 226) = WS1.Cells(i, 194) & " " & WS1.Cells(, 195)
   Next i

This way did work, so I used this code and re-formatted all my other code in the sheet to allow the starting points to line up.

Thanks for your help!
 
Upvote 0
Glad to help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,214,520
Messages
6,120,017
Members
448,937
Latest member
BeerMan23

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