Copy Filtered Rows and Paste to Different Column

Jeffrey Mahoney

Well-known Member
Joined
May 31, 2015
Messages
3,142
Office Version
  1. 365
Platform
  1. Windows
I created this macro to copy items in one column with filtered rows and past them into a different column. As you may know Excel fails at this task

This works, I just find part of the code to be clunky. The part that starts with For Each CC In PRng. Is there a better way to choose the next cell in a non-contiguous range?


VBA Code:
Sub CopyFilteredValues()
  Dim CopyRng As Range
  Dim PRng As Range
  Dim Sel As Range
  Dim Cel As Range
  Dim CC As Range
  Dim TL As Range
  Dim X As Long
  Dim Y As Long
  
  If Application.CutCopyMode Then
    Set CopyRng = fGetClipboardRange
    Set CopyRng = CopyRng.SpecialCells(xlCellTypeVisible)
    If CopyRng.Columns.Count > 1 Then
      MsgBox "You may only copy one column at a time"
      Exit Sub
    End If
    
  Else
    MsgBox "You need to copy the range of numbers first"
    Exit Sub
  End If
  
  Set Sel = Selection
  Set PRng = Selection.SpecialCells(xlCellTypeVisible)
  If PRng.Count <> CopyRng.Count Then
    MsgBox "Please select the same number of rows as your copy range"
    Exit Sub
  End If
  
  Application.Calculation = xlCalculationManual
  
  X = 0
  Set TL = PRng.Areas(1).Resize(1, 1)
  For Each Cel In CopyRng
    X = X + 1
    Y = 0
    
[CODE=rich]
[B]For Each CC In PRng
      Y = Y + 1
      If Y = X Then
        CC.Value = Cel.Value
        Exit For
      End If
    Next CC[/B]
Next Cel
Application.Calculation = xlCalculationAutomatic

End Sub
[/CODE]
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.

Forum statistics

Threads
1,223,888
Messages
6,175,213
Members
452,618
Latest member
Tam84

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