Loop through Worksheets

samanthareed2012

New Member
Joined
May 2, 2014
Messages
9
Hello!
I want my code to loop through the worksheets so I used For each-Next,. but it brings up an error "invalid next control variable reference".
I have more to my code but for space and time saving I have removed the more lengthy parts. Any help would be much appreciated!

Code:

Code:
Dim mysheet As Worksheet

For Each mysheet In ActiveWorkbook.Worksheets

'Counter for Excel worksheet
    For i = 1 To numCells
    variable = "A" & CStr(i)
    Range(variable).Select
    VarID = Range(variable).Value
    VarDes = "C" & CStr(i)
    Des = Range(VarDes).Value
    Dec = "U" & CStr(i)
    DecVal = Range(Dec).Value
    ResponseType = "N" & CStr(i)
    datatypevalue = Range(ResponseType).Value
    DataType = "T" & CStr(i)
    datatypevalue = Range(DataType).Value
    Section = "F" & CStr(i)
    ResponseOptions = "P" & CStr(i)
    resopvalue = Range(ResponseOptions).Value
'Text Areas
        If InStr(Range(ResponseType).Value, "textarea") Then
        wordTable.Rows.Add
        wordCount = wordCount + 1
        wordTable.Cell(wordCount, 1).Range.Text = wordCount - 1
        wordTable.Cell(wordCount, 2).Range.Text = VarID
        wordTable.Cell(wordCount, 3).Range.Text = Des
        wordTable.Cell(wordCount, 4).Range.Text = "Leave text field blank"
        wordTable.Cell(wordCount, 5).Range.Text = "Accepts input"
        wordTable.Rows.Add
        wordCount = wordCount + 1
        wordTable.Cell(wordCount, 1).Range.Text = wordCount - 1
        wordTable.Cell(wordCount, 2).Range.Text = VarID
        wordTable.Cell(wordCount, 3).Range.Text = Des
        wordTable.Cell(wordCount, 4).Range.Text = "Enter 3999 numeric, character, and special character values"
        wordTable.Cell(wordCount, 5).Range.Text = "Input Accepted"
        wordTable.Rows.Add
        wordCount = wordCount + 1
        wordTable.Cell(wordCount, 1).Range.Text = wordCount - 1
        wordTable.Cell(wordCount, 2).Range.Text = VarID
        wordTable.Cell(wordCount, 3).Range.Text = Des
        wordTable.Cell(wordCount, 4).Range.Text = "Enter more than 3999 characters"
        wordTable.Cell(wordCount, 5).Range.Text = "Input not accepted"

End If
        
Next mysheet
    
        Next
wordTable.Rows(1).HeadingFormat = True

WDApp.ActiveDocument.Save

End Sub
 
Last edited:

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
Your code loops through the same worksheet because you don't reference the loop variable mysheet anywhere within the code.

Without a worksheet reference something like this will refer to a range on the active worksheet.
Code:
  VarID = Range(variable).Value
 
Upvote 0

Forum statistics

Threads
1,214,573
Messages
6,120,310
Members
448,955
Latest member
Dreamz high

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