Response
Extract Element
First, the extract element function is declared to extract elements separated by space " "
, where each element has an index.
Example: 1 a 3
in cell A1, with the function EXTRACTELEMENT("A1",1," ")
the answer is 1
and for EXTRACTELEMENT("A1",2," ")
the answer is a
Function EXTRACTELEMENT(Txt As String, n, Separator As String) As String
EXTRACTELEMENT = Split(Application.Trim(Txt), Separator)(n - 1)
End Function
Expand (Master Code)
This code is not optimized, and if the spreadsheet is too large (over 50,000 rows), it can become slow.
Elements must be in ascending order, for example:
+---+---------+
| | A |
+---+---------+
| 1 | 1 a 3 |
| 2 | 6 a 9 |
| 3 | 20 a 23 |
+---+---------+
However, if it is out of order, an error occurs. For example:
+---+---------+
| | A |
+---+---------+
| 1 | 1 a 3 |
| 2 | 20 a 23 |
| 3 | 15 a 9 |
+---+---------+
If it is not in ascending order, some conditionals must be added.
Dim ws As Worksheet
Set ws = ThisWorkbook.Worksheets(1)
Do While y <> 1
lastrow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
If i - 1 = lastrow Or lastrow = 1 Then y = 1
For i = 1 To lastrow
Let Rng = "A" & i
If IsNumeric(ws.Range(Rng)) = False And ws.Range(Rng).Value <> "" Then
ele1 = EXTRACTELEMENT(ws.Range(Rng), 1, " ")
ele2 = EXTRACTELEMENT(ws.Range(Rng), 3, " ")
On Error Resume Next
j = ws.Range(Rng).Row
x = CLng(ele2) - j
Rows(j & ":" & j + x).Insert
Z = ws.Cells(j + x + 1, 2)
For k = ele1 To ele2
ws.Cells(k, 1) = k
ws.Cells(k, 2) = Z
Next k
Rows(j + x + 1).EntireRow.Delete
End If
Next i
Loop
Optional, user defined function description (UDF)
Add function description to be shown when using it in Excel spreadsheet.
Sub DescribeFunction()
Dim FuncName As String
Dim FuncDesc As String
Dim Category As String
Dim ArgDesc(1 To 3) As String
FuncName = "EXTRACTELEMENT"
FuncDesc = "Returns the nth element of a string that uses a separator character/Retorna o enésimo elemento da string que usa um caractér separador."
Category = 7 'Text category
ArgDesc(1) = "String that contains the elements/String que contém o elemento"
ArgDesc(2) = "Element number to return/ Número do elemento a retornar"
ArgDesc(3) = "Single-character element separator/ Elemento único separador (spc por padrão)"
Application.MacroOptions _
Macro:=FuncName, _
Description:=FuncDesc, _
Category:=Category, _
ArgumentDescriptions:=ArgDesc
End Sub
Optional
This code does not accomplish what was asked, due to lack of attention to
The opposite task was written. However, it can be used after the
Evert to group and make very large spreadsheets more organized.
This code first reorders the data in column B in ascending order, then enumerates in column A of 1 to the last cell.
After that, gather the data.
According to the image
DimwsAsWorksheetApplication.ScreenUpdating=FalseSetws=ThisWorkbook.Sheets(1)rLastA=ws.Cells(ws.Rows.Count,1).End(xlUp).RowrLastB=ws.Cells(ws.Rows.Count,2).End(xlUp).RowWithwsOnErrorResumeNext.Outline.ShowLevelsRowLevels:=8.Rows.UngroupOnErrorGoTo0Setr=ws.Range(ws.Cells(1,2),ws.Cells(rLastB,2))EndWithRange("B1").Sort Key1:=Range("B1"), Order1:=xlAscending, Header:=xlGuess, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal
For i = 1 To 4
ws.Cells(i, 1) = i
Next i
Range("A1:A4").AutoFill Destination:=Range("A1:A" & rLastB)
With r
'identify common groups in column B
j = 1
v = .Cells(j, 1).Value
For i = 2 To .Rows.Count
If v <> .Cells(i, 1) Then
' Colum B changed, create group
v = .Cells(i, 1)
If i > j + 1 Then
.Cells(j + 1, 1).Resize(i - j - 1, 1).Rows.Group
End If
j = i
v = .Cells(j, 1).Value
End If
Next
' create last group
If i > j + 1 Then
.Cells(j + 1, 1).Resize(i - j - 1, 1).Rows.Group
End If
' collapse all groups
.Parent.Outline.ShowLevels RowLevels:=1
End With
Application.ScreenUpdating = True
Each part of the code will then be divided to better understand it
Ungrouping
Is performed to properly reorder column B
With ws
On Error Resume Next
.Outline.ShowLevels RowLevels:=8
.Rows.Ungroup
On Error GoTo 0
Set r = ws.Range(ws.Cells(1, 2), ws.Cells(rLastB, 2))
End With
Sorting
Use the Range.Sort to sort the values in column B, this code has been removed of this link
Range("B1").Sort Key1:=Range("B1"), Order1:=xlAscending, Header:=xlGuess, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal
Enumeration
Lists up to the last line with Autofill, the AutoFill tool for Excel, where after selecting the Range, two clicks are given at the black dot in the lower corner of the selection
For i = 1 To 4
ws.Cells(i, 1) = i
Next i
Range("A1:A4").AutoFill Destination:=Range("A1:A" & rLastB)
Grouping
This code has been removed from the Global OS and performs grouping
With r
'identify common groups in column B
j = 1
v = .Cells(j, 1).Value
For i = 2 To .Rows.Count
If v <> .Cells(i, 1) Then
' Colum B changed, create group
v = .Cells(i, 1)
If i > j + 1 Then
.Cells(j + 1, 1).Resize(i - j - 1, 1).Rows.Group
End If
j = i
v = .Cells(j, 1).Value
End If
Next
' create last group
If i > j + 1 Then
.Cells(j + 1, 1).Resize(i - j - 1, 1).Rows.Group
End If
' collapse all groups
.Parent.Outline.ShowLevels RowLevels:=1
End With