Compile error, variable not defined.

mageeg

Board Regular
Joined
Jun 6, 2021
Messages
81
Probably a noob question, i am struggling with getting a piece of code to run.

I have a textbox in which in which the user enters a number, and if it is in between a certain range which is defined in a table, it responds and displays data in 2 different textboxes.

However see code below, I am getting a compile error on "for each i"

I can solve this by putting "Dim i As Range"

But then i get the same error on "Receiver Data" which is my worksheet name.

VBA Code:
Private Sub TextBox4_AfterUpdate()

Dim ID As Long
Dim StartID As Long
Dim EndID As Long

If TextBox4.Value = "" Then

Else

    ID = TextBox4.Value
    
        For Each i In ReceiverData.Range("Table2[From]")
        
        StartID = i.Value
        EndID = i.Offset(0, 1).Value
        
            If ID >= StartID And ID < EndID Then

            TextBox5.Value = i.Offset(0, 3).Value
            TextBox6.Value = i.Offset(0, 4).Value
            TextBox7.Value = i.Offset(0, 5).Value
        
            
         End If
        
        Next i

End If



End Sub
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
If it's the sheet name use
VBA Code:
For Each i In Sheets("ReceiverData").Range("Table2[From]")
 
Upvote 0
Solution
If it's the sheet name use
VBA Code:
For Each i In Sheets("ReceiverData").Range("Table2[From]")

Hmm good thinking!

unfortunately now getting the error subscript out of range. Is it not finding the sheet now?
 
Upvote 0
Does the sheet name have a space in it?
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,176
Members
453,021
Latest member
Justyna P

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