Advertisement
Gorgozoth

Excel Pushbutton Macros

Sep 16th, 2024
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VisualBasic 2.03 KB | Source Code | 0 0
  1. Sub MoveAndClearBlackCellsUp()
  2.     Dim rng As Range
  3.     Dim cell As Range
  4.    
  5.     ' Define the range to search within (adjust Sheet1 to your sheet name if different)
  6.    Set rng = ThisWorkbook.Sheets("Sheet1").UsedRange
  7.    
  8.     For Each cell In rng
  9.         If cell.Interior.Color = RGB(0, 0, 0) Then
  10.             If cell.Row > 1 Then
  11.                 cell.Interior.ColorIndex = x1None
  12.                 cell.Offset(-1, 0).Interior.Color = RGB(0, 0, 0)
  13.                 cell.ClearContents
  14.                 Exit For
  15.             End If
  16.         End If
  17.     Next cell
  18. End Sub
  19.  
  20. Sub MoveAndClearBlackCellsDown()
  21.     Dim rng As Range
  22.     Dim cell As Range
  23.    
  24.     Set rng = ThisWorkbook.Sheets("Sheet1").UsedRange
  25.    
  26.     For Each cell In rng
  27.         If cell.Interior.Color = RGB(0, 0, 0) Then
  28.             If cell.Row > 1 Then
  29.                 cell.Interior.ColorIndex = x1None
  30.                 cell.Offset(1, 0).Interior.Color = RGB(0, 0, 0)
  31.                 cell.ClearContents
  32.                 Exit For
  33.             End If
  34.         End If
  35.     Next cell
  36. End Sub
  37.  
  38. Sub MoveAndClearBlackCellsLeft()
  39.     Dim rng As Range
  40.     Dim cell As Range
  41.    
  42.     Set rng = ThisWorkbook.Sheets("Sheet1").UsedRange
  43.    
  44.     For Each cell In rng
  45.         If cell.Interior.Color = RGB(0, 0, 0) Then
  46.             If cell.column > 1 Then
  47.                 cell.Interior.ColorIndex = x1None
  48.                 cell.Offset(0, 1).Interior.Color = RGB(0, 0, 0)
  49.                 cell.ClearContents
  50.                 Exit For
  51.             End If
  52.         End If
  53.     Next cell
  54. End Sub
  55.  
  56. Sub MoveAndClearBlackCellsRight()
  57.     Dim rng As Range
  58.     Dim cell As Range
  59.    
  60.     Set rng = ThisWorkbook.Sheets("Sheet1").UsedRange
  61.    
  62.     For Each cell In rng
  63.         If cell.Interior.Color = RGB(0, 0, 0) Then
  64.             If cell.column > 1 Then
  65.                 cell.Interior.ColorIndex = x1None
  66.                 cell.Offset(0, -1).Interior.Color = RGB(0, 0, 0)
  67.                 cell.ClearContents
  68.                 Exit For
  69.             End If
  70.         End If
  71.     Next cell
  72. End Sub
Tags: excel macro
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement