I want to paste copied cells into only visible cells inside a sheet where the filter is active using VBA
Thinking it would be a simple task I initially create a sub like this:
Public Sub PasteFlt()
Selection.SpecialCells(xlCellTypeVisible).PasteSpecial xlPasteValues
End Sub
but it doesn't work at all
After a lot of tries and debugging I finally made it work using this code:
Public Sub PasteFlt()
On Error Resume Next
Dim rDest As Range, rSrc As Range
Dim tCell As Range
Dim r As Integer, tR As Integer
Dim c As Integer
Application.ScreenUpdating = False
Set rDest = Selection
Worksheets.Add
ActiveSheet.Paste
Set rSrc = Selection
r = 0
tR = 0
For Each tCell In rDest.SpecialCells(xlCellTypeVisible)
If (tCell.row - rDest.row + 1) > tR Then
r = r + 1
tR = tCell.row - rDest.row + 1
End If
c = tCell.Column - rDest.Column + 1
If r <= rSrc.Rows.Count Then
If c <= rSrc.Columns.Count Then
tCell.Value = rSrc(r, c)
End If
Else
Exit For
End If
Next tCell
Application.DisplayAlerts = False
ActiveWindow.SelectedSheets.Delete
Application.DisplayAlerts = True
End Sub
It does what I want but I don't really like it. My question is: Is there an easier way on doing so without creating and deleting a new sheet, like my first attempt? Maybe I'm simply missing something there
This is a procedure I wrote in reply to a question posted elsewhere - it may or may not suit your requirement:-