Solved

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

Posted on 2013-12-09
3
186 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 50

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

INDEX and MATCH can be used to great effect to replace HLOOKUP and VLOOKUP as it does not have the limitation of needing the data to be sorted so that the reference value is in the first column or row. It also has the ability to perform a bi-directi…
Approximate matching with VLOOKUP and MATCH seems to me to be a greatly under-used technique, and one which is vital for getting good performance out of large lookups. Until recently I would always have advised using an exact match for simplicity an…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.

860 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