General questiion about my code on two different sheet

ipbr21054

Well-known Member
Joined
Nov 16, 2010
Messages
5,832
Office Version
  1. 2007
Platform
  1. Windows
I have a code which works on a worksheet fine.

I then copied it to another worksheet in a different workbook BUT i kept seeing an error saying Not Defind.
The line when debugged is shown below in Blue.
So i added the Line of code in Red & works fine.

My question is Why when on the other sheet does it run WITHOUT the code in Red & Blue ?

Should i be using the code below also on the other sheet ?

Thanks

Rich (BB code):
Private Sub FindDog_Click()
    Dim myRange As Range
    Dim myCell As Range
    Set myRange = Range("Table2").ListObject.DataBodyRange ' CHANGE TABLE NAME
    For Each myCell In Intersect(Columns("C"), myRange) ' CHANGE COLUMN LETTER FOR WHERE TO LOOK FOR VALUE
    If myCell.Value = "BUDDY" Then ' CHANGE NAME OF WHAT TO LOOK FOR
      With Range("A" & myCell.Row) ' SELECT CUSTOMER IN COLUMN A ONCE FOUND
        If MsgBox("BUDDY LOCATED AT ROW: " & myCell.Address(0, 0) & vbCr & vbCr & _
          "THE CUSTOMER IS : " & .Value & vbCr & vbCr & _
          "IS THIS WHAT YOU ARE LOOKING FOR ?", vbCritical + vbYesNo, "FIND BUDDY") = vbYes Then ' CHANGE TEXT TO SUIT
          .Select
          Exit Sub
        End If
      End With
    End If
  Next myCell
  MsgBox "THERE ARE NO TO BE FOUND" & vbNewLine & vbNewLine & "SO THE SEARCH IS NOW COMPLETE", vbInformation, "FIND BUDDY MESSAGEE" ' CHANGE TEXT TO SUIT
  Range("A2").Select

End Sub
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
Probably because the first sheet does not have an Option Explicit statement (which forces you to declare all variables) at the at the top of the code module, while the second sheet does have an Option Explicit statement.

With VBA, you can be lazy and not declare variables. From a programming practice standpoint is it VASTLY better to always declare all variables. The Option Explicit statement enforces variable declaration, and you change change the VBE options to turn it on by default.

1702059139987.png
 
Upvote 0
Solution

Forum statistics

Threads
1,224,816
Messages
6,181,141
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