简体   繁体   中英

VBA Excel “Target” highlighted as “Compile Error: Sub or function not defined”

Maybe I'm dim but I can't see why I'm getting a "Compile Error: Sub or function not defined" error on the following code. The compiler highlights the term "Target" as though I am supposed to define it. What am I missing here? I thought it was a library missing but they seem to be okay. Googling hasn't helped.

Option Explicit
Sub Employee_Entered()
Dim Employee_name As String

Employee_name = Target.Value

Target(0, 2) = Employee_name

End Sub

(Obviously I've cut out the rest of the code. But this still gives me the error described.)

I'm using Excel 2010, Version 14.0.7165.5000.

Thanks Tim

Modify the Set to suit your needs:

Option Explicit

Sub Employee_Entered()
    Dim Employee_name As String
    Dim Target As Range

    Set Target = Range("A1")

    Employee_name = Target.Value
    Target.Offset(0, 2) = Employee_name
End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM