Solved

VBA RowTo .... stop it from going to end

Posted on 2013-12-09
3
181 Views
Last Modified: 2013-12-11
I have the folowing code that will copy data from one sheet to another but when there is no data to copy it falls over and tell me Excel can not continue with available resources

WS.Range(RowFM & ":" & RowTO).EntireRow.Copy WSCopy.Cells(MaxRowCopy, "A")

When I hover over RowTo it tells me it returns 1048576

How do I stop it from falling over and trying to go to 1048576

Thanks
0
Comment
Question by:Jagwarman
3 Comments
 
LVL 48

Expert Comment

by:Rgonzo1971
ID: 39705874
Hi

How is RowTO defined ?

If RowTO = 1048576 Then
    MsgBox "No Data"
Else
    WS.Range(RowFM & ":" & RowTO).EntireRow.Copy WSCopy.Cells(MaxRowCopy, "A")
End If

Open in new window

Regards
0
 
LVL 43

Expert Comment

by:Saqib Husain, Syed
ID: 39705877
How do you calculate RowTo?
0
 
LVL 80

Accepted Solution

by:
byundt earned 500 total points
ID: 39707821
Typically, people encounter this problem by using SomeCell.End(xlDown) if there are no data below SomeCell. In such a case, xlDown doesn't stop until the bottom row in the worksheet (1048576). As an alternative, consider either looking up from the bottom or testing whether the next cell contains data. Both options are shown in the snippet below. As written, they assume you are looking in column A, and have already established a value for the Long variable RowFM.
RowTO = Cells(Rows.Count, "A").End(xlUp).Row

RowTO = RowFM
If Not IsEmpty(Cells(RowFM + 1, "A")) Then RowTO = Cells(RowFM, "A").End(xlDown).Row

Open in new window

0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Convert between Excel file formats (.XLS, .XLSX, .XLSM) with/without macro option David Miller (dlmille) Intro Over this past Fall, I've had the opportunity to see several similar requests and have developed a couple related solutions associate…
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…
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.

760 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

20 Experts available now in Live!

Get 1:1 Help Now