Solved

vba to find the last non blank cell in a range and then copy

Posted on 2014-03-18
2
7,436 Views
Last Modified: 2014-03-18
Can an expert provide me with VBA code that will find the last blank cell [starting from the bottom of the sheet] and then copy the range.

i.e. the range is always A:K but the number of rows will vary.

I have tried a couple of ways of doing this but neither work for me.

Range(ActiveCell, Range(ActiveCell.Address).End _
(xlToRight).End(xlDown)).Select

Is only selecting 3 rows and

Range(Range("A1"), Cells.SpecialCells(xlCellTypeLastCell)).Select

Is selecting to the end of the worksheet.

Thanks
0
Comment
Question by:Jagwarman
2 Comments
 
LVL 19

Accepted Solution

by:
Ken Butters earned 500 total points
ID: 39936894
The sub named "Test" will run a quick test and is an example of how to use the function LastCell.

NOTE:  This function will assume that there is at least one cell with a value in the specified range. if there is not a cell with a value in the range, the result that comes back will be "nothing".

You can remove the msgbox lines when you go to use it... I just put them in there to show the values being returned by lastcell.



Sub test()
    
    Dim myLastCell As Range
    Set myLastCell = LastCell(Worksheets("Sheet1").Range("A:K"))
    
    If Not myLastCell Is Nothing Then
        MsgBox ("Last Row=" & myLastCell.Row & "  Last Column= " & myLastCell.Column)
        
        
        ' Now Copy the range:
        Worksheets("Sheet1").Range("A1:K" & myLastCell.Row).Copy
        
    Else
        MsgBox ("There is no data in specified range")
    End If
    
End Sub

Function LastCell(r As Range) As Range

'
' Note "&" denotes a long value; "%" denotes an integer value
  
    Dim LastRow&, lastCol%

    On Error Resume Next

    With r

  ' Find the last real row

    LastRow& = .Cells.Find(What:="*", _
      SearchDirection:=xlPrevious, _
      SearchOrder:=xlByRows).Row

  ' Find the last real column

    lastCol% = .Cells.Find(What:="*", _
      SearchDirection:=xlPrevious, _
      SearchOrder:=xlByColumns).Column

    End With

' Finally, initialize a Range object variable for
' the last populated row.
    
    Set LastCell = r.Cells(LastRow&, lastCol%)

 
End Function

Open in new window

0
 

Author Closing Comment

by:Jagwarman
ID: 39937197
Thanks Ken brilliant
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
macro for finding text and replacing 7 43
Cascading dropdown 9 27
Excel VBA When using VLookup 6 26
help with changing the VBA to ignore if sheet exists 6 18
Dealing with unintended Excel Active-X resizing quirks (VBA code simulates "self correction") David Miller (dlmille) Intro Not everyone is a fan of Active-X controls in spreadsheets (as opposed to the UserForm approach, the older Form controls …
This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now