Answered by:
Use of Keyword Me

Question
-
How can I re-write the code below without using the Keyword Me ?
Private Sub b cmdClose_Click()
'Close the userform
Unload Me
End Sub
Private Sub cmdSend_Click()
'Dim the variables
Dim cNum As Integer
Dim X As Integer
Dim nextrow As Range
'change the number for the number of controls on the userform
cNum = 6
Set nextrow = Sheet3.Cells(Rows.Count, 3).End(xlUp).Offset(1, 0)
For X = 1 To cNum
nextrow = Me.Controls("Reg" & X).Value
Set nextrow = nextrow.Offset(0, 1)
Next
MsgBox "The data has been sent"
'Clear the controls
cNum = 6
For X = 1 To cNum
Me.Controls("Reg" & X).Value = ""
Set nextrow = nextrow.Offset(0, 1)
Next
End Sub
Private Sub Reg1_AfterUpdate()
'Check to see if value exists
If WorksheetFunction.CountIf(Sheet2.Range("B:B"), Me.Reg1.Value) = 0 Then
MsgBox "This is an incorrect ID"
Me.Reg1.Value = ""
Exit Sub
End If
'Lookup values based on first control
With Me
.Reg2 = Application.WorksheetFunction.VLookup(CLng(Me.Reg1), Sheet2.Range("Lookup"), 2, 0)
.Reg3 = Application.WorksheetFunction.VLookup(CLng(Me.Reg1), Sheet2.Range("Lookup"), 3, 0)
.Reg4 = Application.WorksheetFunction.VLookup(CLng(Me.Reg1), Sheet2.Range("Lookup"), 4, 0)
.Reg5 = Application.WorksheetFunction.VLookup(CLng(Me.Reg1), Sheet2.Range("Lookup"), 5, 0)
.Reg6 = Application.WorksheetFunction.VLookup(CLng(Me.Reg1), Sheet2.Range("Lookup"), 6, 0)
End With
End Sub
Thursday, February 22, 2018 6:56 PM
Answers
-
I was nearly certain that it is the equivalent to the "this" in C# and C++ but I was too lazy to look it up. See Me, My, MyBase, and MyClass in Visual Basic. In C++ "this" is a pointer to the instance of the class, whatever the class the reference is in. In C# it is the same thing except a reference instead of a pointer. If there is a member variable called "something" in the class and there is no other variable called "something" that might cause confusion then "Me.something" is exactly the same as "something", the "Me." would only make a difference to the programmer.
Note that a "shared" method is not associated with any instance; in C++ and C# the equivalent is "static" and we often say that there is no "this" for static methods so for VB.Net a shared method has no "Me".
Sam Hobbs
SimpleSamples.Info- Marked as answer by eganji Thursday, March 15, 2018 6:54 PM
Friday, February 23, 2018 1:49 AM
All replies
-
If you have named the userform, simply replace Me with the name of the userform, like UserForm2 But if you ever change the codename, then you will need to edit your code. So it is a bad idea to not use Me, since that applies to the object whose codemodule the code resides in, and does not rely on the name of the object. It is similar to using ThisWorkbook instead of Workbooks("Work book name.xlsm")....
- Edited by Bernie Deitrick, Excel MVP 2000-2010 Thursday, February 22, 2018 8:06 PM
Thursday, February 22, 2018 8:05 PM -
What happens when you omit it? Try deleting "Me."; what happens? What happens when you use "Unload" instead of "Unload Me"? In C# the equivalent to "Me" is "this". There are times when we need to qualify a reference by saying "this." but usually the "this" is implied. I assume that VB.Net works the same.
Sam Hobbs
SimpleSamples.InfoThursday, February 22, 2018 8:39 PM -
For Unload, an object is required. For the others, it defaults to the active object (IIRC), which may or may not be a good thing. And using Me. allows the use of autocomplete and type ahead of child objects, properties and methods. A user at the level to be asking that question would probably benefit from using Me in their code.
- Edited by Bernie Deitrick, Excel MVP 2000-2010 Thursday, February 22, 2018 9:32 PM
Thursday, February 22, 2018 9:31 PM -
Not exactly active object but definitely current object, which is the same as the current instance.
Sam Hobbs
SimpleSamples.InfoFriday, February 23, 2018 1:03 AM -
I was nearly certain that it is the equivalent to the "this" in C# and C++ but I was too lazy to look it up. See Me, My, MyBase, and MyClass in Visual Basic. In C++ "this" is a pointer to the instance of the class, whatever the class the reference is in. In C# it is the same thing except a reference instead of a pointer. If there is a member variable called "something" in the class and there is no other variable called "something" that might cause confusion then "Me.something" is exactly the same as "something", the "Me." would only make a difference to the programmer.
Note that a "shared" method is not associated with any instance; in C++ and C# the equivalent is "static" and we often say that there is no "this" for static methods so for VB.Net a shared method has no "Me".
Sam Hobbs
SimpleSamples.Info- Marked as answer by eganji Thursday, March 15, 2018 6:54 PM
Friday, February 23, 2018 1:49 AM -
If you have named the userform, simply replace Me with the name of the userform, like UserForm2
I think that would not work.
Sam Hobbs
SimpleSamples.InfoFriday, February 23, 2018 1:55 AM -
It certainly does:
Friday, February 23, 2018 2:16 PM