Solved

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

Posted on 2013-12-09
3
190 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 51

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 81

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

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

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

Improved? Move/Copy Add-in Replacement - How to avoid the annoying, “A formula or sheet you want to move or copy contains the name XXX, which already exists on the destination worksheet.” David Miller (dlmille)  It was one of those days… I wa…
Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
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.

738 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