Results 1 to 3 of 3

LastRow = Selection.Row(Compile Error: Variable not defined)

This is a discussion on LastRow = Selection.Row(Compile Error: Variable not defined) within the Excel Questions forums, part of the Question Forums category; Dear All, I am trying to set the last row of data to a variable LastRow, which I have used ...

  1. #1
    New Member
    Join Date
    Jan 2004
    Location
    Melbourne
    Posts
    10

    Default LastRow = Selection.Row(Compile Error: Variable not defined)

    Dear All,

    I am trying to set the last row of data to a variable LastRow, which I have used many times before successfully! However, it just doesn't want to work on my current Macro and I get a compile error?

    (Compile Error: Variable not defined)

    (Has this beginner obviously missed something or inadvertantly flagged someing to hinder his progress?) Code to follow!

    Code:
    Range("A17").Select
        ActiveCell.End(xlDown).Select
        LastRow = Selection.Row
    Any thoughts?
    javascript:emoticon('')
    Bang head
    Many thanks!
    Dean
    [/i]

  2. #2
    MrExcel MVP parry's Avatar
    Join Date
    Aug 2002
    Location
    Wellington, New Zealand
    Posts
    3,355

    Default Re: LastRow = Selection.Row(Compile Error: Variable not defi

    Hi Dean, this error occurs when you have used a variable but havent declared it prior to using the variable. Declare means using the DIM keyword to define the variable name and its type. I suspect you have a line that has Option Explicit in your module where you didnt before. This enforces declaring variables. You can remove this line, or declare the variable & IMHO you should declare variables as its good programming practice.

    Code:
    Option Explicit
    
    Sub Test()
    Dim LastRow As Long
    Range("A17").Select
        ActiveCell.End(xlDown).Select
        LastRow = Selection.Row
    End Sub

  3. #3
    New Member
    Join Date
    Jan 2004
    Location
    Melbourne
    Posts
    10

    Default Re: LastRow = Selection.Row(Compile Error: Variable not defi

    Thank-you Parry

    I don't know how I flagged Option Explicit but It gives me something to look into tonight!

    Have tried your code ...I'm back on track!

    Many, many Thanks!


    Regards
    Dean

Like this thread? Share it with others

Like this thread? Share it with others

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


DMCA.com