+ Reply to Thread
Results 1 to 7 of 7

questions about Dim

  1. #1
    Forum Contributor funkymonkUK's Avatar
    Join Date
    01-07-2005
    Location
    London, England
    Posts
    500

    questions about Dim

    Hi

    I want to make the lastrow of sheet1 column A common knowledge between my macros. Reason being is i want to refer the the lastrow a lot of times

    I thought maybe public sub would help this is my code

    Public figs
    ' Count Rows in table
    Dim lastRowpub As Long
    lastRowpub = Worksheets("Sheet1").Cells(Rows.Count, "A").End(xlUp).Row


    It keeps coming up saying

    Compile Error: Invalid Outside Procedure

    and then highlights the (xlUp) part of the line


    any ideas?

  2. #2
    Bob Phillips
    Guest

    Re: questions about Dim

    You need a sub

    Public figs
    Public lastRowpub As Long

    Sub CountRows()
    ' Count Rows in table
    lastRowpub = Worksheets("Sheet1").Cells(Rows.Count,"A").End(xlUp).Row
    End Sub

    --
    HTH

    Bob Phillips

    (remove nothere from email address if mailing direct)

    "funkymonkUK" <[email protected]>
    wrote in message
    news:[email protected]...
    >
    > Hi
    >
    > I want to make the lastrow of sheet1 column A common knowledge between
    > my macros. Reason being is i want to refer the the lastrow a lot of
    > times
    >
    > I thought maybe public sub would help this is my code
    >
    > Public figs
    > ' Count Rows in table
    > Dim lastRowpub As Long
    > lastRowpub = Worksheets("Sheet1").Cells(Rows.Count,
    > "A").End(xlUp).Row
    >
    >
    > It keeps coming up saying
    >
    > Compile Error: Invalid Outside Procedure
    >
    > and then highlights the (xlUp) part of the line
    >
    >
    > any ideas?
    >
    >
    > --
    > funkymonkUK
    > ------------------------------------------------------------------------
    > funkymonkUK's Profile:

    http://www.excelforum.com/member.php...o&userid=18135
    > View this thread: http://www.excelforum.com/showthread...hreadid=516165
    >




  3. #3
    Forum Contributor funkymonkUK's Avatar
    Join Date
    01-07-2005
    Location
    London, England
    Posts
    500
    thanks

    so would I need to run this sub each time before using the lastrow ?

  4. #4
    Bob Phillips
    Guest

    Re: questions about Dim

    Yep, in fact it would be better as a function and call the function.

    --
    HTH

    Bob Phillips

    (remove nothere from email address if mailing direct)

    "funkymonkUK" <[email protected]>
    wrote in message
    news:[email protected]...
    >
    > thanks
    >
    > so would I need to run this sub each time before using the lastrow ?
    >
    >
    > --
    > funkymonkUK
    > ------------------------------------------------------------------------
    > funkymonkUK's Profile:

    http://www.excelforum.com/member.php...o&userid=18135
    > View this thread: http://www.excelforum.com/showthread...hreadid=516165
    >




  5. #5
    Forum Contributor funkymonkUK's Avatar
    Join Date
    01-07-2005
    Location
    London, England
    Posts
    500
    how i do that?

  6. #6
    Ardus Petus
    Guest

    Re: questions about Dim

    Function lastRowpub() As Long
    ' Count Rows in table
    lastRowpub = Worksheets("Sheet1").Cells(Rows.Count, "A").End(xlUp).Row
    End Function

    You can use this function as follows:

    set rng = cells(lasrRowpub(),"a")

    HTH
    --
    AP

    "funkymonkUK" <[email protected]> a
    écrit dans le message de
    news:[email protected]...
    >
    > how i do that?
    >
    >
    > --
    > funkymonkUK
    > ------------------------------------------------------------------------
    > funkymonkUK's Profile:

    http://www.excelforum.com/member.php...o&userid=18135
    > View this thread: http://www.excelforum.com/showthread...hreadid=516165
    >




  7. #7
    Bob Phillips
    Guest

    Re: questions about Dim

    I would actually pass the sheet and column, to make it more generic

    Function lastRowpub(colnum As Long, Optional sh As Worksheet) As Long
    ' Count Rows in table
    If sh Is Nothing Then Set sh = Activesheet
    lastRowpub = sh.Cells(sh.Rows.Count, colnum).End(xlUp).Row
    End Function

    Then use as

    iLastRow = lastRowpub(1,Worksheets("Sheet1"))


    --
    HTH

    Bob Phillips

    (remove nothere from email address if mailing direct)

    "Ardus Petus" <[email protected]> wrote in message
    news:%[email protected]...
    > Function lastRowpub() As Long
    > ' Count Rows in table
    > lastRowpub = Worksheets("Sheet1").Cells(Rows.Count, "A").End(xlUp).Row
    > End Function
    >
    > You can use this function as follows:
    >
    > set rng = cells(lasrRowpub(),"a")
    >
    > HTH
    > --
    > AP
    >
    > "funkymonkUK" <[email protected]> a
    > écrit dans le message de
    > news:[email protected]...
    > >
    > > how i do that?
    > >
    > >
    > > --
    > > funkymonkUK
    > > ------------------------------------------------------------------------
    > > funkymonkUK's Profile:

    > http://www.excelforum.com/member.php...o&userid=18135
    > > View this thread:

    http://www.excelforum.com/showthread...hreadid=516165
    > >

    >
    >




+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1