Advertisement
Guest User

Untitled

a guest
Jun 18th, 2019
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.21 KB | None | 0 0
  1. Option Explicit
  2.  
  3. Sub test()
  4.  
  5. Dim LastColumn As Long, StartPoint As Long, EndPoint As Long, i As Long, y As Long
  6.  
  7. With ThisWorkbook.Worksheets("Sheet1")
  8.  
  9. 'Find the last column of row 1
  10. LastColumn = .Cells(1, .Columns.Count).End(xlToLeft).Column
  11.  
  12. 'Set StartPoint
  13. StartPoint = 1
  14.  
  15. 'Loop row 1
  16. For i = 1 To LastColumn
  17.  
  18. If i = StartPoint Then
  19.  
  20. For y = i + 1 To LastColumn
  21.  
  22. If .Cells(1, i).Value <> .Cells(1, y).Value Then
  23.  
  24. EndPoint = y - 1
  25. Exit For
  26.  
  27. End If
  28.  
  29. Next y
  30.  
  31. Application.DisplayAlerts = False
  32.  
  33. With .Range(.Cells(1, StartPoint), Cells(1, EndPoint))
  34. .Merge
  35. .HorizontalAlignment = xlCenter
  36. .VerticalAlignment = xlCenter
  37. End With
  38.  
  39. Application.DisplayAlerts = True
  40.  
  41. StartPoint = y
  42. EndPoint = 0
  43.  
  44. End If
  45.  
  46. Next i
  47.  
  48. End With
  49.  
  50. End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement