Finding last Value in A Column of a Multi-Column Dynamic Range of Cells

Ark68

Well-known Member
Joined
Mar 23, 2004
Messages
4,564
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
I have a set variable name 'rng_pdaservices' to a dynamic defined range of cells. In this particular case, referring to A22:Q32.
I am looking for an efficient VBA solution to find the last occupied cell in column A within this range , ie the last cell in A22:A32 that holds a value.

Thoughts?
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
VBA Code:
Dim f As Range
Set f = Intersect(Range("rng_pdaservices"), [A:A]).Find(What:="*", After:=Range("rng_pdaservices")(1), SearchDirection:=xlPrevious)
 
Last edited:
Upvote 0
Is this what you mean?
(I have assumed at least one cell in column A of that range will hold a value)

VBA Code:
MsgBox rng_pdaservices.Columns(1).Find(What:="*", SearchDirection:=xlPrevious).Address(0, 0)
 
Upvote 0
Thank you both for your replies. Peter pointed out something I hadn't thought of, and that was the fact that that range may not initially have any value in it. The purpose of the routine that Iseek this help for is building a list of data in the range, always building on the last cell of that range. But it may very well be that there is no value in that range yet and that this info will be needed to place the first.

How does this change your suggestions?
 
Upvote 0
How does this change your suggestions?
One way would be
VBA Code:
Dim msg As String

msg = "No data found"
On Error Resume Next
msg = rng_pdaservices.Columns(1).Find(What:="*", SearchDirection:=xlPrevious).Address(0, 0)
On Error GoTo 0
MsgBox msg
 
Upvote 0
Solution
VBA Code:
Dim f As Range
Set f = Intersect(Range("rng_pdaservices"), [A:A]).Find(What:="*", After:=Range("rng_pdaservices")(1), SearchDirection:=xlPrevious)
Thank you Footoo, your help is appreciated!!
 
Upvote 0
Thank you Peter, this works wonderfully.

So that I don't get an unpleasant surprise, will this work ok if there is data and empty cells in that range? For instance, consider rng_pdaservices data in cells A13:A32. Will the spaces between the data cause problems, or will the answer of 26 be returned?

task allocation.xlsm
A
13data
14data
15data
16
17
18data
19data
20data
21data
22data
23data
24data
25
26data
27
28
29
30
31
32
Sheet13
 
Upvote 0
Thanks again Peter! It might take a while before I encounter this scenario unless I can find a way to recreate it intentionally. This scenario would be very rare but not impossible.
 
Upvote 0
unless I can find a way to recreate it intentionally.
That is pretty easy to do.
Run this code with the Active sheet empty (or at least nothing in A22:A32)

VBA Code:
Sub Find_Last()
  Dim rng_pdaservices As Range
  Dim msg As String
  
  Set rng_pdaservices = Range("A22:Q32")
  msg = "No data found"
  On Error Resume Next
  msg = rng_pdaservices.Columns(1).Find(What:="*", SearchDirection:=xlPrevious).Address(0, 0)
  On Error GoTo 0
  MsgBox msg
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,924
Messages
6,122,294
Members
449,077
Latest member
Rkmenon

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top