PGSystemTester

VBA Union Example

Jul 6th, 2017
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Sub UnionExample()
  2. 'Creates a range of cells in Excel that are not directly connected. Below example takes cells from columns B and C
  3.  
  4. 'Creating Two Ranges that are not connected
  5. Dim Rng1 As Range: Set Rng1 = Range("B1:B100")
  6. Dim Rng2 As Range: Set Rng2 = Range("D1:D100")
  7.  
  8. 'Combines Two ranges into a single range which VBA can peform functions on
  9. Dim Rng_Combined As Range: Set Rng_Combined = Union(Rng1, Rng2)
  10.  
  11. 'Selects disconnected range
  12. Rng_Combined.Select
  13.  
  14. 'or loop through the disconnected range's cells
  15. Dim CL As Range
  16. For Each CL In Rng_Combined.Cells
  17.     CL.Interior.Color = vbRed
  18. Next CL
  19.  
  20. End Sub
Add Comment
Please, Sign In to add comment