Hi
Is it possible to copy data in a range but ignore the blanks?
I usually use this code to copy data in a range but it’s copying rows where there a formulas that return blank values, which I don’t want it to:
Range("A1", Range("A1").End(xlToRight).End(xlDown)).Copy
To illustrate the issue, please see the simple example below:
I have two sheets.
In Sheet 1, the values 1, 2, and 3 are in cells A1, A2, and A3, respectively:
[TABLE="width: 64"]
<tbody>[TR]
[TD]1
[/TD]
[/TR]
[TR]
[TD]2
[/TD]
[/TR]
[TR]
[TD]3
[/TD]
[/TR]
</tbody>[/TABLE]
In Sheet 2, cells A1 and B1 have the titles “Number” and “Letter,” respectively.
Cell A2 in Sheet 2 has this formula: =IF(Sheet1!A1="","",Sheet1!A1)
And cell B2 in Sheet 2 has this formula: =IF(Sheet1!B1="","",Sheet1!B1)
Both formulas have been dragged down to row 10 of each column.
So the result looks like this – ie cells B2:B4 have values because the formulas in Sheet 2 find values for them from Sheet 1.
[TABLE="width: 128"]
<tbody>[TR]
[TD]Number
[/TD]
[TD]Letter
[/TD]
[/TR]
[TR]
[TD]1
[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]2
[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]3
[/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]
The problem is that when this Sub is run, it copies everything in the range, including the rows with the blank formulas!
Is there a way for it to stop at the last populated cell? ie the row with the value “3” in Sheet 2 in this example?
I'd prefer to avoid using a loop, as they can take forever, when there's lots of data.
TIA
Is it possible to copy data in a range but ignore the blanks?
I usually use this code to copy data in a range but it’s copying rows where there a formulas that return blank values, which I don’t want it to:
Range("A1", Range("A1").End(xlToRight).End(xlDown)).Copy
To illustrate the issue, please see the simple example below:
I have two sheets.
In Sheet 1, the values 1, 2, and 3 are in cells A1, A2, and A3, respectively:
[TABLE="width: 64"]
<tbody>[TR]
[TD]1
[/TD]
[/TR]
[TR]
[TD]2
[/TD]
[/TR]
[TR]
[TD]3
[/TD]
[/TR]
</tbody>[/TABLE]
In Sheet 2, cells A1 and B1 have the titles “Number” and “Letter,” respectively.
Cell A2 in Sheet 2 has this formula: =IF(Sheet1!A1="","",Sheet1!A1)
And cell B2 in Sheet 2 has this formula: =IF(Sheet1!B1="","",Sheet1!B1)
Both formulas have been dragged down to row 10 of each column.
So the result looks like this – ie cells B2:B4 have values because the formulas in Sheet 2 find values for them from Sheet 1.
[TABLE="width: 128"]
<tbody>[TR]
[TD]Number
[/TD]
[TD]Letter
[/TD]
[/TR]
[TR]
[TD]1
[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]2
[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]3
[/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]
The problem is that when this Sub is run, it copies everything in the range, including the rows with the blank formulas!
Code:
Sub CopyNonBlankRowsInRange()
Sheet2.Activate
Range("A1", Range("A1").End(xlToRight).End(xlDown)).Copy
End Sub
Is there a way for it to stop at the last populated cell? ie the row with the value “3” in Sheet 2 in this example?
I'd prefer to avoid using a loop, as they can take forever, when there's lots of data.
TIA