+ Reply to Thread
Results 1 to 4 of 4

Invalid next control variable reference?

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    04-04-2014
    Location
    Tetbury, England
    MS-Off Ver
    Excel 2010
    Posts
    254

    Invalid next control variable reference?

    Could someone explain why this doesn't work?

    I get the error message: Invalid Next control variable reference
    And it highlights the i in 'Next i'


    Private Sub cmbTestSub_Click()
        Dim ws As Worksheet
        Dim i As Integer, r As Integer, col As Integer
        Set ws = Sheets("Sub Master")
    For col = 1 To 143
        If ComboBox3.Value = Worksheets("PLHeadings").Range("C" & col + 3).Value Then
            For i = 1 To 12
            For r = 3 To 145
                If Me.Controls("txtY1M" & i).Value <> "" Then ws.Cells(r, 2 + i).Value = Me.Controls("txtY1M" & i).Value
                If Me.Controls("txtY2M" & i).Value <> "" Then ws.Cells(r, 14 + i).Value = Me.Controls("txtY2M" & i).Value
                If Me.Controls("txtY3M" & i).Value <> "" Then ws.Cells(r, 26 + i).Value = Me.Controls("txtY3M" & i).Value
                If Me.Controls("txtY4M" & i).Value <> "" Then ws.Cells(r, 38 + i).Value = Me.Controls("txtY4M" & i).Value
                If Me.Controls("txtY5M" & i).Value <> "" Then ws.Cells(r, 50 + i).Value = Me.Controls("txtY5M" & i).Value
            Next i
        End If
    Next col
    End Sub

  2. #2
    Forum Expert
    Join Date
    04-22-2013
    Location
    .
    MS-Off Ver
    .
    Posts
    4,418

    Re: Invalid next control variable reference?

    You initialise 3 for loops, but only provide next statements for two of them:
    Private Sub cmbTestSub_Click()
        Dim ws As Worksheet
        Dim i As Integer, r As Integer, col As Integer
        Set ws = Sheets("Sub Master")
    For col = 1 To 143
        If ComboBox3.Value = Worksheets("PLHeadings").Range("C" & col + 3).Value Then
            For i = 1 To 12
            For r = 3 To 145
                If Me.Controls("txtY1M" & i).Value <> "" Then ws.Cells(r, 2 + i).Value = Me.Controls("txtY1M" & i).Value
                If Me.Controls("txtY2M" & i).Value <> "" Then ws.Cells(r, 14 + i).Value = Me.Controls("txtY2M" & i).Value
                If Me.Controls("txtY3M" & i).Value <> "" Then ws.Cells(r, 26 + i).Value = Me.Controls("txtY3M" & i).Value
                If Me.Controls("txtY4M" & i).Value <> "" Then ws.Cells(r, 38 + i).Value = Me.Controls("txtY4M" & i).Value
                If Me.Controls("txtY5M" & i).Value <> "" Then ws.Cells(r, 50 + i).Value = Me.Controls("txtY5M" & i).Value
            Next i
        End If
    Next col
    End Sub

  3. #3
    Valued Forum Contributor Miroslav R.'s Avatar
    Join Date
    05-16-2013
    Location
    NMnV, Slovakia
    MS-Off Ver
    Excel 2007
    Posts
    479

    Re: Invalid next control variable reference?

    Hi,
    I am missing 'Next r' in your preview.

    Regards

    M

  4. #4
    Registered User
    Join Date
    06-25-2013
    Location
    San Diego, CA
    MS-Off Ver
    Excel 2010
    Posts
    51

    Re: Invalid next control variable reference?

    Also, just a suggestion, you use indenting, but you didn't indent the For r line. You should be able to line up your For lines with the corresponding Next lines.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. invalid next control variable reference
    By stockgoblin42 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-03-2013, 05:08 PM
  2. invalid next control variable reference for looping
    By alienalvan in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 05-30-2013, 03:19 PM
  3. [SOLVED] Invalid Next control variable reference
    By dslack in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 06-05-2012, 08:20 PM
  4. invalid next control variable reference
    By stevesunfold in forum Excel Programming / VBA / Macros
    Replies: 13
    Last Post: 05-17-2008, 08:09 PM
  5. Invalid Next Control Variable Reference
    By teresa in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 05-31-2005, 08:05 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1