Receiving Compile Error When Adding a Loop Inside Another Loop

ashperson95

New Member
Joined
May 16, 2014
Messages
36
Okay, so I have a block of code that asks the user for several inputs and puts them into their corresponding cells in a table on the worksheet. Here's a look at it:

Code:
locationNum = 1
For Each InfoInput In Range("Table2[Location]")


InfoInput.Value = InputBox("Name of location " & locationNum, "Name of Location")
fixtures = InputBox("How many fixtures does location " & locationNum & " have?")


InfoInput.Offset(, 1).Value = fixtures


LightTypes.Show
InfoInput.Offset(, 2).Value = LightTypes.ComboBox1.Value


hoursDay = InputBox("How many hours a day do the fixtures in location " & locationNum & " run?")
InfoInput.Offset(, 3).Value = hoursDay


locationNum = locationNum + 1
Next InfoInput

Now, the problem I'm having is when I go to check that the input the user gives is valid. I'm only on the first one, the fixtures variable. Everything was working fine, but when I added in the code that's below and tried to run it, I received a compile error that says "Next without For". I'm not sure where I need to add in the For.

Code:
fixtures = InputBox("How many fixtures does location " & locationNum & " have?")
If Not IsNumeric(fixtures) Then
Do Until IsNumeric(fixtures)
fixtures = InputBox("Sorry, that's not a valid entry. How many fixtures does location " & locationNum & " have?")
Loop

Any help would be appreciated!
 
Last edited:

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
ashperson95,

It is difficult to say because you have not posted all your macro code, but:

It looks like you are missing an End If

Try:

Rich (BB code):
fixtures = InputBox("How many fixtures does location " & locationNum & " have?")
If Not IsNumeric(fixtures) Then
  Do Until IsNumeric(fixtures)
    fixtures = InputBox("Sorry, that's not a valid entry. How many fixtures does location " & locationNum & " have?")
  Loop
End If
 
Upvote 0
Wow, I feel stupid for that mistake. Thank you. But now, when I purposely enter an invalid entry for that input, I get a type-mismatch error instead of my input box.
 
Upvote 0
Here is the entire code for the macro, so you can better understand what's going on.

Code:
Sub EnterLocationInfo()
Dim locations As Integer
Dim lightrows As Long
Dim locationNum As Integer
Dim fixtures As Integer
Dim hoursDay As Integer
lightrows = Range("Table2").Rows.Count
locations = Application.InputBox("How many locations do you have?", "Location Input", , , , , , Type:=1)


If locations > lightrows Then
Do Until locations = lightrows
ListObjects("Table2").ListRows.Add
AlwaysInsert = True
lightrows = Range("Table2").Rows.Count
Loop
End If


If locations < lightrows Then
Do Until locations = lightrows
ActiveSheet.ListObjects("Table2").ListRows(lightrows).Delete
lightrows = Range("Table2").Rows.Count
Loop
End If


LightTypes.ComboBox1.List = Sheets("Light Bulbs and Fixtures").Range("Table1[Type of Light]").Value


locationNum = 1
For Each InfoInput In Range("Table2[Location]")


InfoInput.Value = InputBox("Name of location " & locationNum, "Name of Location")
fixtures = InputBox("How many fixtures does location " & locationNum & " have?")


If Not IsNumeric(fixtures) Then
Do Until IsNumeric(fixtures)
fixtures = InputBox("Sorry, that's not a valid entry. How many fixtures does location " & locationNum & " have?")
Loop
End If


InfoInput.Offset(, 1).Value = fixtures


LightTypes.Show
InfoInput.Offset(, 2).Value = LightTypes.ComboBox1.Value


hoursDay = InputBox("How many hours a day do the fixtures in location " & locationNum & " run?")
InfoInput.Offset(, 3).Value = hoursDay


locationNum = locationNum + 1
Next InfoInput
End Sub
 
Upvote 0
ashperson95,

Here is the entire code for the macro, so you can better understand what's going on.

Without having your workbook, I do not have a clue as to what you are trying to do.

Click on the Reply to Thread button, and just put the word BUMP in the thread. Then, click on the Post Quick Reply button, and someone else will assist you.
 
Upvote 0
Try this way:

Code:
    Do
        fixtures = InputBox("How many fixtures does location " & locationNum & " have?")
    Loop Until IsNumeric(fixtures)
 
Upvote 0
hiker95,
I'd be happy to send you the workbook if you'd like.


Andrew,
I still received a type mismatch error instead.
 
Upvote 0
ashperson95,

It looks like you are in good hands with Andrew Poulsom.


hiker95,
I'd be happy to send you the workbook if you'd like.

If Andrew Poulsom is not able to solve your request, then:

You can upload your workbook to Box Net,
sensitive data changed
mark the workbook for sharing
and provide us with a link to your workbook.
 
Upvote 0

Forum statistics

Threads
1,215,745
Messages
6,126,633
Members
449,323
Latest member
Smarti1

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