Copy only cells with values

Romano_odK

Active Member
Joined
Jun 4, 2020
Messages
379
Office Version
  1. 365
Platform
  1. Windows
Good afternoon,

Now I have a macro to copy a colum to another column in a table, but what I really need is when the column is copied that only the filled in values are copied. Is that possible?

Thank you for your time,

Romano


Private Sub KopieerNaarVerkoopprijs_Click()
With Sheets("OPXML").ListObjects("Table_Query_from_A100")
.ListColumns(3).DataBodyRange.Value = .ListColumns(4).DataBodyRange.Value
End With
End Sub
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
It helps if you tell how those copied values are arragned in the destination locations. Best if you can give illustration ;)
 
Upvote 0
It helps if you tell how those copied values are arragned in the destination locations. Best if you can give illustration ;)
It helps if you tell how those copied values are arragned in the destination locations. Best if you can give illustration ;)
Good afternoon,

You got a point. OK so I added a small part of the sheet just to give an idea what I need. With the mentioned macro I need the values who are a part of table to be copied from column D to C without the blanks in D overwriting the values in column C.

Thank you in advance.


Private Sub KopieerNaarVerkoopprijs_Click()
With Sheets("OPXML").ListObjects("Table_Query_from_A100")
.ListColumns(3).DataBodyRange.Value = .ListColumns(4).DataBodyRange.Value
End With
End Sub


1641385270744.png
 

Attachments

  • 1641385180548.png
    1641385180548.png
    57.3 KB · Views: 4
Upvote 0
How about
VBA Code:
Private Sub KopieerNaarVerkoopprijs_Click()
With Sheets("OPXML").ListObjects("Table_Query_from_A100")
   .ListColumns(4).DataBodyRange.Copy
   .ListColumns(3).DataBodyRange.PasteSpecial xlPasteValues, , True
   Application.CutCopyMode = False
End With
End Sub
 
Upvote 0
How about
VBA Code:
Private Sub KopieerNaarVerkoopprijs_Click()
With Sheets("OPXML").ListObjects("Table_Query_from_A100")
   .ListColumns(4).DataBodyRange.Copy
   .ListColumns(3).DataBodyRange.PasteSpecial xlPasteValues, , True
   Application.CutCopyMode = False
End With
End Sub
Exactly what I needed, thank you for your time and solution.

Have a nice day,

Romano
 
Upvote 0
Glad we could help & thanks for the feedback.
 
Upvote 0
Exactly what I needed, thank you for your time and solution.

Have a nice day,

Romano
Oh one little thing. Is is also possible to not overwrite when a cell in column D is empty. Now it overwrites values in column C when there is no value in a cell in column D.
Thank you.
 
Upvote 0
Is this usable for your needs?

VBA Code:
Sub TS_CopyNonEmpty()
Dim DRng As Range, CRng As Range
Dim DArr As Variant, CArr As Variant

With Sheets(1)
    rw = .Range("C65536").End(xlUp).row + 1
        Set CRng = .Range("C2" & ":" & "C" & rw)
        Set DRng = .Range("D2" & ":" & "D" & rw)
        
        CArr = CRng.Value
        DArr = DRng.Value

        For i = LBound(CArr, 1) To UBound(CArr, 1)
            If Len(DArr(i, 1)) > 0 Then CArr(i, 1) = DArr(i, 1)
        Next i

End With

CRng.Value = CArr

End Sub
 
Upvote 0
But that's what the code does & you said it worked.
 
Upvote 0
Is this usable for your needs?

VBA Code:
Sub TS_CopyNonEmpty()
Dim DRng As Range, CRng As Range
Dim DArr As Variant, CArr As Variant

With Sheets(1)
    rw = .Range("C65536").End(xlUp).row + 1
        Set CRng = .Range("C2" & ":" & "C" & rw)
        Set DRng = .Range("D2" & ":" & "D" & rw)
       
        CArr = CRng.Value
        DArr = DRng.Value

        For i = LBound(CArr, 1) To UBound(CArr, 1)
            If Len(DArr(i, 1)) > 0 Then CArr(i, 1) = DArr(i, 1)
        Next i

End With

CRng.Value = CArr

End Sub
Thank you, but now its does not overwrite the values in de column C unfortunately.
 
Upvote 0

Forum statistics

Threads
1,215,480
Messages
6,125,047
Members
449,206
Latest member
Healthydogs

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