-
Notifications
You must be signed in to change notification settings - Fork 0
/
unmerger.vbs
118 lines (89 loc) · 3.47 KB
/
unmerger.vbs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
Sub unmerger()
Dim thisCell, origCell, thisRow, thisCol, numRows, nextCellDown, alertsOn, mergeRange, rowHeight, prevCol, nextCol, currentCell
Dim values()
Dim segments, empties, x, i, a
' ------ get & format address for active cell ------
thisCell = ""
thisCol = ""
segments = Split(ActiveCell.Address, "$")
For Each x In segments
thisCell = thisCell + x
If thisCol = "" Then
thisCol = x
Else
thisRow = x
End If
Next
rowHeight = ActiveCell.rowHeight
nextCellDown = thisCol & (CInt(thisRow) + 1)
' see how many rows in selected cell
numRows = 0
segments = Split(ActiveCell.Value, Chr(10))
For Each x In segments
' change ubound of array, get individual values, increment counter
numRows = numRows + 1
ReDim Preserve values(numRows)
values(numRows) = x
Next
' for # of values-1 (return-separated) insert that many rows & insert each value in the new row
numRows = numRows - 1
If numRows < 1 Then Exit Sub
For i = 1 To numRows
Range(thisCol & (CInt(thisRow) + i)).EntireRow.Insert
Range(thisCol & (CInt(thisRow) + i)).Value = values(i + 1)
Next
' see if alerts are on; if so, turn them off temporarily
alertsOn = False
If Application.DisplayAlerts = True Then Application.DisplayAlerts = False: alertsOn = True
' then delete all but the 1st value from the original cell
Range(thisCell).Value = values(1)
' then merge the preceeding cells (w/o alert showing)
' have to merge A10 through A12, B10 through B12, etc. up through the preceeding column
'mergeRange = "A" & thisRow & ":" & thisCol & (CInt(thisRow) + numRows)
' get .Previous.Address, then keep going until previous = A
prevCol = ""
Set currentCell = ActiveCell
Set origCell = ActiveCell
Do Until prevCol = "A"
' get previous column
segments = Split(currentCell.Previous.Address, "$")
a = 0
For Each x In segments
If a = 1 Then prevCol = x
a = a + 1
Next
Set currentCell = currentCell.Previous
' merge previous column
mergeRange = prevCol & thisRow & ":" & prevCol & (CInt(thisRow) + numRows)
Range(mergeRange).Merge
Loop
' reset row heights
For i = 0 To numRows
Range(thisCol & (CInt(thisRow) + i)).rowHeight = (rowHeight) / (numRows + 1)
Next
' using format painter to set succeeding cells back to their original height
empties = 0
Set currentCell = ActiveCell
Do Until empties > 2
' get next column
segments = Split(currentCell.Next.Address, "$")
a = 0
For Each x In segments
If a = 1 Then nextCol = x
a = a + 1
Next
Set currentCell = currentCell.Next
' merge next column
mergeRange = nextCol & thisRow & ":" & nextCol & (CInt(thisRow) + numRows)
Range(mergeRange).Merge
If IsEmpty(currentCell) Then
empties = empties + 1
Else
empties = 0
End If
Loop
origCell.Select
If alertsOn Then Application.DisplayAlerts = True
Set origCell = Nothing
Set currentCell = Nothing
End Sub