Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8741021
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T11:14:04+00:00 2026-06-13T11:14:04+00:00

I want to write a simple function that tells me the column length, giving

  • 0

I want to write a simple function that tells me the column length, giving the first record of the column. Hence, I write something like…

Public Function getColumnLen(firstCell As Range) As Long

Dim lastRow As Long
lastRow = Range(firstCell.Address).End(xlDown).Row

getColumnLen = lastRow - firstCell.Row + 1
End Function

Now I have two worksheets in my project, “A” and “B” and in VBA I call

Dim colLen as Long
colLen = getColumnLen(Worksheets("A").Range("A1"))

when I want to know the height of data starting on A1. The resulting value of colLen depends on whether sheet “A” is active or “B” is active. The thing is that in the getColumnLen function, firstCell remembers that it is a part of “A” sheet, however scrolling is done on the active sheet. Is there any way to make this give correct results regardless of the active sheet, other than activating firstCell in each call?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T11:14:05+00:00Added an answer on June 13, 2026 at 11:14 am

    It’s because you’re giving it a not-fully-qualified cell address:

    lastRow = Range(firstCell.Address).End(xlDown).Row
    

    Here, firstCell.Address returns $A$1 regardless of what worksheet you specify when you call the function.

    • Worksheets("A").Range("A1").Address
    • Worksheets("B").Range("A1").Address
    • Worksheets("Whatever other sheet").Range("A1").Address

    These all return $A$1!

    Forget about the Address property. If you want to know what it does, read VBA help. Just do this instead:

    lastRow = firstCell.End(xlDown).Row
    

    Be advised, however, that this method of finding the last row will fail if you range contains blank cells. This issue has been addressed a gazillion times; just search this site but be aware that not all of the proposed solutions are equally valid!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want the write a simple application, that input some strings, and do something
I want to write a simple regular expression in Python that extracts a number
I'm trying to write a simple php function that will strip everything between two
I want to write a function such that when class clipartcategory clicked above of
I am trying to write a simple function that will get me a number
I need to write a simple java function that takes a URL and processes
I want to write a function that behaves differently depending on its type parameter.
In Scala language, I want to write a function that yields odd numbers within
I want write a simple query which will fetch data from a table (which
I want to write simple script to copy/backup directory then remove on server startup.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.