VBA code to delete copied blank cells

KevinMMO

New Member
Joined
Mar 2, 2018
Messages
17
Hi, I'm doing a macro that copies a specific range and then pastes it on another sheet to make a database. This range contains blank cells and also another ones that have a formula that if a cell is blank, then the value of the cell's formula is blank. If(B7="","",.....).

the range that I copy is from C7:E57. all this cells have a formula like that above. They all depend on the "B" column's value, but the person doesn't always fill the 50 rows. Here's the code:

Code:
Public Sub copiar()   'macro terminado para copiar datos e insertarlos en base de datos


'Declare Variables
Dim wsOrigin As Worksheet
Dim wsDataBase As Worksheet
Set wsOrigin = ThisWorkbook.Sheets("Recoleccion")
Set wsDataBase = ThisWorkbook.Sheets("Basedatos")

Application.ScreenUpdating = False    'para que código sea más rápido

'Copy/Special Paste para datos

Dim COPYME As Range

Dim contador As Long




contador = wsDataBase.Range("A" & wsDataBase.Rows.Count).End(xlUp).Row

Set COPYME = wsOrigin.Range("C7:E57")   'Rango que se va a copiar

COPYME.Copy

wsDataBase.Range("A" & contador + 1).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False




Application.ScreenUpdating = True

End Sub


Is there a way to delete the blank cells copied to the database sheet, so when the macro runs again it doesn't have all that blank spaces? Without doing it manually.

If this isn't explained very clearly, I could try being more specific if asked. Thanks.
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
Hi. Add the red parts and give it a try.


Dim wsOrigin As Worksheet, k As Long



wsDataBase.Range("A" & contador + 1).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False

With wsDataBase
For k = .Cells(Rows.Count, 1).End(xlUp).Row To contador + 1 Step -1
If .Cells(k, 1) = "" Then .Rows(k).Delete
Next k
End With

Application.ScreenUpdating = True



 
Upvote 0
It works, Thanks! :)



Hi. Add the red parts and give it a try.


Dim wsOrigin As Worksheet, k As Long



wsDataBase.Range("A" & contador + 1).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False

With wsDataBase
For k = .Cells(Rows.Count, 1).End(xlUp).Row To contador + 1 Step -1
If .Cells(k, 1) = "" Then .Rows(k).Delete
Next k
End With

Application.ScreenUpdating = True



 
Upvote 0

Forum statistics

Threads
1,223,247
Messages
6,171,007
Members
452,374
Latest member
keccles

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