Home > Back-end >  How to copy and paste a filtered table using VBA from one column to another visible column?
How to copy and paste a filtered table using VBA from one column to another visible column?

Time:11-12

I have trouble in solving this particular issue . I am currently trying to automate a function where the macro would automatically update a value from Column C to Column D

For example if in filtered Cell C2 and C4 have a value i would like it to paste to Cell D2 and D4 and would skip D3

enter image description here

enter image description here

enter image description here

I hope my explanation makes sense , as this is a sample i can think of in explaining on larger scale

CodePudding user response:

In order to copy the filtered cells of column "C:C" (starting from the second row), to the corresponding rows of column "D:D":

Sub copyVisCells()
  Dim sh As Worksheet, lastR As Long, rngVS As Range, Ar As Range
  
  Set sh = ActiveSheet
  lastR = sh.Range("C" & sh.rows.count).End(xlUp).row
  Set rngVS = sh.Range("C2:C" & lastR).SpecialCells(xlCellTypeVisible) 'here it needs error checking in case of no visible cells...
  For Each Ar In rngVS.Areas
      Ar.Copy Ar.Offset(0, 1)
  Next Ar
End Sub

If you can define a rule based on what to copy values from B:B, let us say a row before the visible cells in D:D, the code can be adapted to do it, but you must explain based on what rule to do it...

  • Related