Guest User

Untitled

a guest
Nov 20th, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.74 KB | None | 0 0
  1. Sub Replace430()
  2.  
  3. Dim MyRange As Range
  4. Dim RowCount As Long
  5. Dim ColCount As Integer
  6. Dim MyArray() As Variant
  7.  
  8. Dim i, j, k, percent30 As Long
  9. Dim Count4 As Long
  10. Const Replaced = 0 'Set Replaced Value
  11. Const found = 18 'Set Find Value
  12. Const Mycol = 3 'Correctly set the Column Number of Column in Range where 4 is to be checked
  13. Set MyRange = Range("B2:F23")
  14.  
  15. RowCount = MyRange.Rows.Count
  16. ColCount = MyRange.Columns.Count
  17. 'Get number of 4's againts 18
  18. For i = 1 To RowCount
  19. If MyRange.Columns(1).Cells(i) = found Then
  20. For j = Mycol To Mycol
  21. If MyRange.Columns(j).Cells(i) = 4 Then
  22. Count4 = Count4 + 1
  23. End If
  24. Next j
  25. End If
  26.  
  27. Next i
  28.  
  29. ReDim MyArray(Count4 - 1, 2)
  30. k = 0
  31. For i = 1 To RowCount
  32. If MyRange.Columns(1).Cells(i) = found Then
  33. For j = Mycol To Mycol
  34. If MyRange.Columns(j).Cells(i) = 4 Then
  35. MyArray(k, 1) = i
  36. MyArray(k, 2) = j
  37. k = k + 1
  38. End If
  39. Next j
  40. End If
  41.  
  42. Next i
  43.  
  44. percent30 = 0.3 * Count4
  45.  
  46. Dim shufflearray()
  47. ReDim shufflearray(Count4 - 1)
  48. For i = 0 To Count4 - 1
  49. shufflearray(i) = i
  50. Next i
  51.  
  52. 'Shuffle the shufflearray() below
  53.  
  54.  
  55. Dim N As Long
  56. Dim Temp As Variant
  57.  
  58.  
  59. Randomize
  60. For N = LBound(shufflearray) To UBound(shufflearray)
  61. j = CLng(((UBound(shufflearray) - N) * Rnd) + N)
  62.  
  63. If N <> j Then
  64. Temp = shufflearray(N)
  65. shufflearray(N) = shufflearray(j)
  66. shufflearray(j) = Temp
  67. End If
  68. Next N
  69.  
  70. 'Use randomised values from shufflearray as array subscript to replace only 30% of 4's
  71. For i = 0 To percent30 - 1
  72. MyRange.Columns(MyArray(shufflearray(i), 2)).Cells(MyArray(shufflearray(i), 1)).Value = Replaced
  73. Next i
  74.  
  75.  
  76. End Sub
Add Comment
Please, Sign In to add comment