Copy/Paste Range issue with VBA when cells are empty (but with formulas inside)

Dani_LobP

Board Regular
Joined
Aug 16, 2019
Messages
134
Office Version
  1. 365
Platform
  1. Windows
Hello,

Currently having some issues when copy/pasting some range of rows.
There are some formulas in there that would be populating depending of another table.
So in the end there would be some rows populating from such table, and eventually remaining rows without populated values would remain blank, but, with formulas inside.
When i use vba to copy range, it would actually take those blank cells as cells with values and copy them too, when i only would like to just have rows populated actually..

so this would be first part of the code. Table is pasted in rows A6:A28 (sometimes is just used A6:A8 others more.. thats why i use formulas to populate the rows below A28, because sometimes is longer and others shorter).
A28 would be headers of the small table that autopopulates with formulas that will find values from table above.

VBA Code:
LastRow = Range("A28").End(xlDown).Row
Range("A29").Select
Range("A29:CI" & LastRow).Copy

And then when copy this, it would extract it to another sheet/book and paste it. And is where my problem appears, it pastes all rows, populated and ones blanks too.

this part of the code will basically append new rows to the end row of some other table in other file. And it adds blank rows, so next time i do this, it creates a big gap.

Code:
Range("A1").End(xlDown).Offset(1, 0).Select
Selection.PasteSpecial xlValues

And this is actually what i have in those cells with formulas i talk about (there are many more columns but as example so it makes sense or easier to understand):
this would be example of row 30 (basically from row 29 till row 40 is same). Row 29 would be populated, and row 30 and so on all empty since there was no more rows in main table to obtain data to populate.
columns below would be A:E

=CONCATENATE(B30,D30,E30)=IF(OR(P7="",P7=P6),"",P7)=IF(B30="","",0)=IF(C30="","",IF(D7="","",LEFT(D7,3)))=IF(B30="","",0)

Hopefully it makes sense the explanation and someone can help to find a way to fix or change the way i made it work.

Thanks in advance.
Dani
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
I *think* I understand the problem.

What happens if you try this:
VBA Code:
With Range("A1").End(xlDown).Offset(1, 0)
    .PasteSpecial xlValues
    .Value = .Value
End With
 
Upvote 0
Thanks for the reply, i tried modifying this part of code but still would paste empty cells in the rows. Formulas won't appear since it pastes values, but it would "paste" empty cells that actually are accounted later as if they had something inside.

No luck i guess.

I *think* I understand the problem.

What happens if you try this:
VBA Code:
With Range("A1").End(xlDown).Offset(1, 0)
    .PasteSpecial xlValues
    .Value = .Value
End With
 
Upvote 0
Does the below help any?
VBA Code:
Dim LastRow As Long
LastRow = Columns("A").Find("*", , xlValues, , xlByRows, xlPrevious).Row
Range("A29:CI" & LastRow).Copy
 
Upvote 0
Thanks for the reply, i tried modifying this part of code but still would paste empty cells in the rows. Formulas won't appear since it pastes values, but it would "paste" empty cells that actually are accounted later as if they had something inside.

No luck i guess.
Did you try this? Because whilst it will still copy the blank rows, the .Value = .Value component will convert those to true blanks, thus allowing your LastRow calculation to go to the last non-blank, thus reporting the correct last row position.
 
Upvote 0
Did you try this? Because whilst it will still copy the blank rows, the .Value = .Value component will convert those to true blanks, thus allowing your LastRow calculation to go to the last non-blank, thus reporting the correct last row position.

yes, i tried. when pasting in the new sheet. it would still grab empty rows when doing ctrl+shift down for example. Not sure thou 100% what changes with your code, but still happens same outcome.
 
Upvote 0
Ok, well first I think you need to clear the non-blank blank rows from your destination sheet (as a once off), and then try:

VBA Code:
Range("A" & Rows.Count).End(xlUp).Offset(1, 0).PasteSpecial xlValues
Selection.Value = Selection.Value

I had an error in my prior code, it was not applying the value conversion to the pasted range.
 
Upvote 0
How about this;

VBA Code:
Dim r as Range

With Range("A1").End(xlDown).Offset(1, 0)
    .PasteSpecial xlValues
    for each r in .Cells
        If r.Value = "" Then r.ClearContents
    Next

End With
 
Upvote 0
Does the below help any?
VBA Code:
Dim LastRow As Long
LastRow = Columns("A").Find("*", , xlValues, , xlByRows, xlPrevious).Row
Range("A29:CI" & LastRow).Copy

Nice! thanks! this one seems to work , togehter with @Jon von der Heyden code, just one thing.
I changed a bit LastRow since as i said, table where issue happens starts in Row 28 (headers) with data appering starting row 29.

VBA Code:
Dim LastRow As Long
LastRow = Range("A28").Find("*", , xlValues, , xlByRows, xlPrevious).Row
Range("A29:CI" & LastRow).Copy

This actually copies the header also, since for some reason it was not doing it before.
What can i change so it just copies from row 29 to below without header in row 28?

Thanks in advance!
 
Upvote 0
Ok, well first I think you need to clear the non-blank blank rows from your destination sheet (as a once off), and then try:

VBA Code:
Range("A" & Rows.Count).End(xlUp).Offset(1, 0).PasteSpecial xlValues
Selection.Value = Selection.Value

I had an error in my prior code, it was not applying the value conversion to the pasted range.

ok this one works wonders! thanks!
 
Upvote 0

Forum statistics

Threads
1,224,818
Messages
6,181,151
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top