Advertisement
YasserKhalil2019

T3946_Transfer Numbers By Column Rank

Sep 20th, 2019
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.60 KB | None | 0 0
  1. https://excel-egy.com/forum/t3946
  2. ---------------------------------
  3.  
  4. Sub Transfer_Numbers_By_Column_Rank()
  5. Dim x, c As Range, rng As Range, m As Long, r As Long
  6.  
  7. Application.ScreenUpdating = False
  8. With Range("F1").CurrentRegion.Offset(2, 1)
  9. .Value = Empty
  10. .Font.ColorIndex = xlAutomatic
  11. .Interior.Color = xlNone
  12. .Borders.Value = 0
  13. End With
  14.  
  15. x = Application.Match(Range("F3").Value, Columns(1), 0)
  16. r = 3
  17.  
  18. If Not IsError(x) Then
  19. m = IIf(Cells(x, 1).End(xlDown).Row = Rows.Count, Cells(x, 2).End(xlDown).Row + 1, Cells(x, 1).End(xlDown).Row)
  20. Set rng = Range(Cells(x, 2), Cells(m - 1, 2))
  21.  
  22. For Each c In rng
  23. If Len(c.Value) = 6 Then
  24. r = r + 4
  25. Cells(r - 1, "I").Value = c.Value
  26. Cells(r - 2, "I").Value = Left(c.Value, 5): Cells(r - 2, "I").Font.Color = vbRed
  27. Cells(r - 3, "H").Value = Left(c.Value, 4): Cells(r - 3, "H").Font.Color = vbRed
  28. Cells(r - 4, "G").Value = Left(c.Value, 3): Cells(r - 3, "H").Font.Color = vbRed
  29. ElseIf Len(c.Value) = 5 Then
  30. r = r + 3
  31. Cells(r - 1, "I").Value = c.Value
  32. Cells(r - 2, "H").Value = Left(c.Value, 4): Cells(r - 2, "H").Font.Color = vbRed
  33. Cells(r - 3, "G").Value = Left(c.Value, 3): Cells(r - 3, "G").Font.Color = vbRed
  34. ElseIf Len(c.Value) = 4 Then
  35. r = r + 2
  36. Cells(r - 1, "H").Value = c.Value
  37. Cells(r - 2, "G").Value = Left(c.Value, 3): Cells(r - 2, "G").Font.Color = vbRed
  38. ElseIf Len(c.Value) = 3 Then
  39. r = r + 1
  40. Cells(r - 1, "G").Value = c.Value
  41. End If
  42. Next c
  43. End If
  44.  
  45. For r = Range("F1").CurrentRegion.Rows.Count To 3 Step -1
  46. If Cells(r, 7).Value <> "" And Application.CountIf(Range("G3:G" & r), Cells(r, 7).Value) > 1 _
  47. Or Cells(r, 8).Value <> "" And Application.CountIf(Range("H3:H" & r), Cells(r, 8).Value) > 1 _
  48. Or Cells(r, 9).Value <> "" And Application.CountIf(Range("I3:I" & r), Cells(r, 9).Value) > 1 _
  49. Then Cells(r, 7).Resize(1, 3).Delete
  50. Next r
  51.  
  52. With Range("G3:I" & Range("F3").CurrentRegion.Rows.Count)
  53. .Interior.Color = vbYellow
  54. .Borders.Value = 1
  55. End With
  56. Application.ScreenUpdating = True
  57. End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement