How to parse a row? - excel

I need to write VBA code that:
Reads in rows in a sheet
Checks if column E has the character ";#" and parses the string on that character
Creates a new row and copies and pastes the row contents from the parsed row to the new row (both rows will have the same contents)
Renames the original column to the word that comes before ;#" and renames the copied column to the word that follows ";#"
Example with three columns:
Original row: String A;#String B;#StringC (cell 1) Complete (cell 2) 5/20/2019 (cell 3)
What I need:
Updated_Original row: String A Complete 5/20/2019
New row 1: String B Complete 5/20/2019
New row 2: String C Complete 5/20/2019
Private Sub CommandButton1_Click()
Dim SplitText
Dim WrdArray() As String, size As Integer
'iterate through all the rows in the sheet
For i = 1 To i = 2000
'take one cell at a time
cell_value = ThisWorkbook.ActiveSheet.Cells(i, 1).Value
size = WorksheetFunction.CountA(Worksheets(1).Columns(1))
'Split cell contents
WrdArray() = Split(cell_value, vbLf)
For j = LBound(WrdArray) To UBound(WrdArray)
Var = WrdArray()(0)
Next j
' WrdArray().Resize(UBound(SplitText) + 1).Value = Application.Transpose(SplitText)
ReDim WrdArray(size)
counter = counter + 1
Var = SplitText
Next i
End Sub

So long as you don't need the Title column of your screenshot in the particular order you show, this is a simple task for Power Query (aka Get & Transform in excel 2016+).
Merely
Get&Transform Data from Table/Range
Split by delimiter (and your delimiter appears to be ;# and not just #
Split into rows
And you're done:
This is the M-Code for the PQ:
let
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Status", type text}, {"Priority", type text}, {"Name", type text}, {"Date", type date}}),
#"Split Column by Delimiter" = Table.ExpandListColumn(Table.TransformColumns(#"Changed Type", {{"Name", Splitter.SplitTextByDelimiter(";#", QuoteStyle.Csv), let itemType = (type nullable text) meta [Serialized.Text = true] in type {itemType}}}), "Name"),
#"Changed Type1" = Table.TransformColumnTypes(#"Split Column by Delimiter",{{"Name", type text}})
in
#"Changed Type1"

I chose to use a combo of Len() and InStr() to figure where "complete" was in your string to figure the content to append to each part of the split. I made a few assumptions related to your columns/rows (see image below):
Option Explicit
Sub fdsa()
Dim arr As Variant, i As Long, s As Long, lr As Long, c As Long, z As String
lr = Cells(Rows.Count, 1).End(xlUp).Row
For i = 1 To lr
arr = Split(Cells(i, 1).Value, ",")
If InStr(Cells(i, 1).Value, "Complete") Then z = Right(Cells(i, 1).Value, Len(Cells(i, 1).Value) - InStr(Cells(i, 1).Value, "Complete") + 1)
c = 2
For s = LBound(arr) To UBound(arr)
If s = UBound(arr) Then z = ""
Cells(i, c).Value = arr(s) & " " & z
c = c + 1
Next s
Next i
End Sub
Here's the data I used:

Making a lot of assumptions based on what appears to be incomplete information here, but according to the information and examples provided, something like this should work for you:
Sub tgr()
Dim ws As Worksheet
Dim rData As Range
Dim aResults() As Variant
Dim aData As Variant
Dim vTemp As Variant
Dim sTemp As String
Dim ixResult As Long
Dim i As Long, j As Long
Set ws = ActiveWorkbook.ActiveSheet
Set rData = ws.Range("A1").CurrentRegion
If rData.Cells.Count = 1 Then
ReDim aData(1 To 1, 1 To 1)
aData(1, 1) = rData.Value
Else
aData = rData.Value
End If
ReDim aResults(1 To 65000, 1 To UBound(aData, 2))
ixResult = 0
For i = 1 To UBound(aData, 1)
For Each vTemp In Split(Replace(aData(i, 1), ";#", ","), ",")
If Len(Trim(vTemp)) > 0 Then
ixResult = ixResult + 1
aResults(ixResult, 1) = Trim(vTemp)
For j = 2 To UBound(aData, 2)
aResults(ixResult, j) = aData(i, j)
Next j
End If
Next vTemp
Next i
rData.Resize(ixResult).Value = aResults
End Sub

Related

How to parse unique records and their row index

I have a set of files, some have no suffix and some with different suffixes. I would like to segregate the file names irrespective of their suffixes and list them along with the range of their row indices in the same spreadsheet. Below is the example and my failed code. Also attached the spreadsheet snapshot. Can you please help? Any new code/logic is welcome.
Input:
Row index
Filename
1
File1
2
File2_a
3
File2_b
4
File2_c
5
File3_a
6
File3_b
Output:
Filename
Row indices range
File1
1
1
File2
2
4
File3
5
6
VBA code
Sub GetUniqueFiles()
Dim sh As Worksheet
Set sh = ThisWorkbook.Sheets("Sheet1")
lastrow = sh1.Range("A" & Rows.Count).End(xlUp).Row
SameFile = False ' Flag to compare 2 consecutive file names
i = 3: j = 3
While i <= (lastrow - 1)
name_curt = sh.Range("B" & i).Value
name_next = sh.Range("B" & i + 1).Value
file_curt = Split(name_curt, "_")(0)
file_next = Split(name_next, "_")(0)
If file_curt <> file_next Then
sh.Range("D" & j).Value = file_curt
k1 = i
sh.Range("E" & j).Value = k1
sh.Range("F" & j).Value = k2
i = i + 1: j = j + 1
ElseIf file_curt = file_next Then
SameFile = True
sh.Range("B" & j).Value = file_curt
k1 = i
While SameFile
i = i + 1
name_curt = sh.Range("B" & i).Value
name_next = sh.Range("B" & i + 1).Value
file_curt = Split(name_curt, "_")(0)
file_next = Split(name_next, "_")(0)
Wend
End If
Wend
End Sub
Try this:
Sub GetUniqueFiles()
Dim sh As Worksheet, m, indx, rw As Range, f As String
Dim r As Long
Set sh = ThisWorkbook.Sheets("Sheet1")
Set rw = sh.Range("A3:B3") 'first input row
r = 2 'start row for output
Do While Application.CountA(rw) = 2 'loop while have data
indx = rw.Cells(1).Value
f = Split(rw.Cells(2).Value, "_")(0) ' "base" file name
m = Application.Match(f, sh.Columns("D"), 0) 'see if already listed
If IsError(m) Then 'not already listed ?
sh.Cells(r, "D").Value = f 'write file name
sh.Cells(r, "E").Value = indx 'write "first seen" index
m = r
r = r + 1
End If
sh.Cells(m, "F").Value = indx 'write "last seen" index
Set rw = rw.Offset(1, 0) 'next input row
Loop
End Sub
You can solve this without VBA. Add something like this in column C:
=LEFT(B2,IFERROR(FIND("_",B2)-1,LEN(B2)+1))
It will cut the underscore and everything after it. Next step is counting the distinct values. I would go for a pivot table, but there are lots of other ways.
You can obtain your desired output using Power Query, available in Windows Excel 2010+ and Office 365 Excel
Select some cell in your original table
Data => Get&Transform => From Table/Range or From within sheet
When the PQ UI opens, navigate to Home => Advanced Editor
Make note of the Table Name in Line 2 of the code.
Replace the existing code with the M-Code below
Change the table name in line 2 of the pasted code to your "real" table name
Examine any comments, and also the Applied Steps window, to better understand the algorithm and steps
M Code
let
//Change table name in next line to actual table name in your workbook
Source = Excel.CurrentWorkbook(){[Name="Table22"]}[Content],
//split on the underscore and remove the splitted suffix
#"Split Column by Delimiter" = Table.SplitColumn(Source, "Filename",
Splitter.SplitTextByDelimiter("_", QuoteStyle.Csv), {"Filename", "Filename.2"}),
//set data types -- frequently a good idea in PQ
#"Changed Type1" = Table.TransformColumnTypes(#"Split Column by Delimiter",{
{"Filename", type text}, {"Filename.2", type text}}),
#"Removed Columns" = Table.RemoveColumns(#"Changed Type1",{"Filename.2"}),
//Group by file name and extract the lowest and highest rows
#"Grouped Rows" = Table.Group(#"Removed Columns", {"Filename"}, {
{"Start Row", each List.Min([Row index]), type number},
{"End Row", each List.Max([Row index]), type number}})
in
#"Grouped Rows"
For execution speed and resource conservation it's best to minimize interaction with the worksheet from VBA. For example the following references the worksheet precisely twice no matter how long the list of files. Don't underestimate the value of restricting worksheet interaction from VBA.
Sub GetUniqueFiles()
Dim c&, i&, a$, b$, vIn, vOut
Const FILES_IN$ = "b3"
Const FILES_OUT$ = "d3"
With Range(FILES_IN)
vIn = .Resize(.End(xlDown).Row - .Row + 1)
End With
ReDim vOut(1 To UBound(vIn), 1 To 3)
For i = 1 To UBound(vIn)
b = Split(vIn(i, 1), "_")(0)
If a <> b Then
a = b
c = c + 1
vOut(c, 1) = b
vOut(c, 2) = i
If c > 1 Then vOut(c - 1, 3) = i - 1
End If
Next
If c > 1 Then vOut(c, 3) = i - 1
Range(FILES_OUT).Resize(UBound(vIn), 3) = vOut
End Sub

Transform table structure

Hello is any sensible algorithm for transforming data table (Table A to Table B) ?
I trying to moving cells , but have no idea how to calculate a place where I should place additional row after my key field Name.
Table A origin
Name
Salary
Bonus
Amount
John S.
5000
Bonus A
50
John S.
Bonus B
100
Alex G.
7000
Bonus C
150
Alex G.
Bonus D
300
Table B (Expected outcome)
Name
Salary
Bonus
Amount
John S.
5000
John S.
Bonus A
50
John S.
Bonus B
100
Alex G.
7000
Alex G.
Bonus C
150
Alex G.
Bonus D
300
Sub TransformTable()
' Setting variables
Dim Name As String
Dim BaseSalary As String
Dim BonusName As String
Dim BonusAmount As Double
'Setting worksheet object
Dim SheetData As Worksheet
Set SheetData = Sheets("SheetData")
'counter for main loop
Dim x As Long
'Setting main object array
Dim MyArray As Variant
Dim Item As Integer
Item = 1
'reading values from table
MyArray = Worksheets("SheetData").ListObjects("Table1").DataBodyRange.Value
'counting last row value
'main loop
For x = LBound(MyArray) To UBound(MyArray)
'condition check how many costcenter ids with fixed value
lstRowSrs = SheetData.Cells(Rows.Count, 1).End(xlUp).Row
Worksheets("SheetData").Cells(Item + 1, 13).Value = MyArray(x, 1)
Worksheets("SheetData").Cells(Item + 1, 14).Value = MyArray(x, 2)
If MyArray(x, 3) <> "" Then
' Cells(x, lstRowSrs).EntireRow.Insert
Worksheets("SheetData").Cells(Item + 2, 15).Value = MyArray(x, 3)
Worksheets("SheetData").Cells(Item + 2, 16).Value = MyArray(x, 4)
Item = Item + 1
Else
Worksheets("SheetData").Cells(Item + 1, 15).Value = MyArray(x, 3)
Worksheets("SheetData").Cells(Item + 1, 16).Value = MyArray(x, 4)
Item = Item + 1
End If
Next x
End Sub
You can obtain your desired output using Power Query, available in Windows Excel 2010+ and Office 365 Excel
Select some cell in your original table
Data => Get&Transform => From Table/Range
When the PQ UI opens, navigate to Home => Advanced Editor
Make note of the Table Name in Line 2 of the code.
Replace the existing code with the M-Code below
Change the table name in line 2 of the pasted code to your "real" table name
Examine any comments, and also the Applied Steps window, to better understand the algorithm and steps
The basic algorithm:
Unpivot the Salary and Amount columns which puts them all into a single column
the Bonus column will have some duplicates -- remove them if the Attribute column contains "Salary"
Remove the contents of the Salary column; rename and reorder the columns
M Code
let
//change table name in next line to actual name in your workbook
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
//set the data types
#"Changed Type" = Table.TransformColumnTypes(Source,{
{"Name", type text}, {"Salary", Int64.Type}, {"Bonus", type text}, {"Amount", Int64.Type}}),
//Unpivot the columns other than Name and Bonus
#"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Changed Type", {"Name", "Bonus"}, "Attribute", "Value"),
//blank the "bonus" if attribute=salary
#"Replace Bonus with null" = Table.ReplaceValue(#"Unpivoted Other Columns",
each [Bonus],
each if [Attribute]="Salary" then null else [Bonus],
Replacer.ReplaceValue,{"Bonus"}),
//set columns in correct order
#"Reordered Columns" = Table.ReorderColumns(#"Replace Bonus with null",{"Name", "Attribute", "Bonus", "Value"}),
//rename "Attribute"=>"Salary" and blank the contents
Rename = Table.RenameColumns(#"Reordered Columns",{{"Attribute","Salary"},{"Value","Amount"}}),
blankIt = Table.ReplaceValue(Rename, each [Salary],null, Replacer.ReplaceValue,{"Salary"})
in
blankIt
Here's another way. It has the same results as #Sgdva but uses some slightly different techniques. Not better, just something to consider.
Sub TransformTable()
Dim vaValues As Variant
Dim i As Long
Dim aOutput() As Variant
Dim lCnt As Long
'put all the values in a 2-d array
vaValues = Sheet1.ListObjects(1).DataBodyRange
'make your output array - double the rows of the input
'it will be too many rows, but you won't run out of room
ReDim aOutput(1 To UBound(vaValues, 1) * 2, 1 To 4)
'Loop through the 2-d array
For i = LBound(vaValues, 1) To UBound(vaValues, 1)
If Len(vaValues(i, 2)) > 0 Then 'a salary exists
'add a row to the output array
lCnt = lCnt + 1
aOutput(lCnt, 1) = vaValues(i, 1)
aOutput(lCnt, 4) = vaValues(i, 2)
End If
If Len(vaValues(i, 4)) > 0 Then 'a bonus exists
'add a row to the output array
lCnt = lCnt + 1
aOutput(lCnt, 1) = vaValues(i, 1)
aOutput(lCnt, 3) = vaValues(i, 3)
aOutput(lCnt, 4) = vaValues(i, 4)
End If
Next i
'write out the output array in one shot
Sheet1.Range("G1").Resize(UBound(aOutput, 1), UBound(aOutput, 2)).Value = aOutput
End Sub
Solution
I changed the logic that you posted as follows
Identify the rows to be added
Insert them at once (to save memory instead of inserting one by one)
Append the data needed by looping again in the rows
For demonstration purposes, I limited the logic to the active sheet and with the data sample shown.
Demo
Code
Sub Exec_DivideSalary()
Dim CounterRow As Long
Dim RangeRowsToAdd As Range
For CounterRow = 2 To Cells.SpecialCells(xlCellTypeLastCell).Row
If Cells(CounterRow, 2).Value <> "" Then ' 1. If Cells(CounterRow, 2).Value <> ""
If RangeRowsToAdd Is Nothing Then ' 2. If RangeRowsToAdd Is Nothing
Set RangeRowsToAdd = Rows(CounterRow + 1)
Else ' 2. If RangeRowsToAdd Is Nothing
Set RangeRowsToAdd = Union(RangeRowsToAdd, Rows(CounterRow + 1))
End If ' 2. If RangeRowsToAdd Is Nothing
End If ' 1. If Cells(CounterRow, 2).Value <> ""
Next CounterRow
RangeRowsToAdd.Insert Shift:=xlDown
For CounterRow = 2 To Cells.SpecialCells(xlCellTypeLastCell).Row
If Cells(CounterRow, 2).Value <> "" Then ' 3. If Cells(CounterRow, 2).Value <> ""
Cells(CounterRow + 1, 1).Value = Cells(CounterRow, 1).Value: Cells(CounterRow + 1, 3).Value = Cells(CounterRow, 3).Value: Cells(CounterRow + 1, 4).Value = Cells(CounterRow, 4).Value
Cells(CounterRow, 4).Value = Cells(CounterRow, 2).Value
Cells(CounterRow, 2).Value = "": Cells(CounterRow, 3).Value = ""
End If ' 3. If Cells(CounterRow, 2).Value <> ""
Next CounterRow
End Sub

Excel VBA - Separate data by pattern by using Split Function

I am working on a special filter to divide cell from column E24:E33 into separate cells in range F24:M33
There are pattern words by which I want to divide the data:
amount: price: price2: status: min: opt: cat: code z:
I have already tested several methods to solve this issue, but none gave me a proper results. Unfortunately I am not a VBA programmer on such a high level to write it on my own completely from scratch.
This is the code I have so far but still the data is not being correctly divided:
Sub qtest()
Dim a As Variant, b As Variant
'a = SpecialSplit("asdf:asdf asds:ert ert qwe d:sdfg") 'THIS TEST DATA WORKS FINE
a = SpecialSplit("amount:3 pc. price:2397 price2:0 EU status:In use min:1 opt:3 cat: DESTACO code z:") DOESN'T WORK FOR ONE CELL
'SpecialSplit = Sheets("T4").Range("E24" & Rows.Count).End(xlUp).Row 'ALSO DOESN't WORK FOR ENTIRE RANGE
ActiveCell.Offset(0, 1).Resize(1, UBound(a) - LBound(a) + 1).value = a
End Sub
Function SpecialSplit(Compound As String) As Variant
Dim pos1 As Long, pos2 As Long
Dim firstSplit As Variant
Dim elmts As Integer
Dim elmt As Integer
Dim oneel As String
firstSplit = Split(Compound, ":")
For elmt = LBound(firstSplit) To UBound(firstSplit) - 1
oneel = firstSplit(elmt + 1)
pos1 = InStr(oneel, " ")
pos2 = Len(oneel)
While pos1 > 0 And pos1 < Len(oneel) - 1
pos2 = pos1
pos1 = InStr(pos2 + 1, oneel, " ")
Wend
firstSplit(elmt) = firstSplit(elmt) & ":" & Left(oneel, pos2 - 1)
firstSplit(elmt + 1) = Right(oneel, Len(oneel) - pos2)
Next elmt
elmts = UBound(firstSplit) - LBound(firstSplit) ' one shorter
If elmts > 0 Then ReDim Preserve firstSplit(1 To elmts)
SpecialSplit = firstSplit
End Function
Sub SpecialSpread(FromCell As Range)
Dim splitSet As Variant
splitSet = SpecialSplit(FromCell(1).Text)
If UBound(splitSet) >= 0 Then
FromCell(1).Offset(0, 1).Resize(1, UBound(splitSet) - LBound(splitSet) + 1).value = splitSet
End If
End Sub
Sub qtest()
Dim a As Variant, b As Variant
'a = SpecialSplit("asdf:asdf asds:ert ert qwe d:sdfg") 'BY THIS PATTERN CODE WORKS
a = SpecialSplit("amount:3 pc. price:2397 price2:0 EU status:In use min:1 opt:3 cat: DESTACO code z:") ' BY THIS PATTERN CODE DOESN'T WORK
'SpecialSplit = Sheets("T4").Range("E2" & Rows.Count).End(xlUp).Row
ActiveCell.Offset(0, 1).Resize(1, UBound(a) - LBound(a) + 1).value = a
End Sub
You can do this in Power Query (available in Windows Excel 2010+ and Office 365.
This relies on the pattern words always being present and always in the same order.
In PQ we can actually split on the pattern words, so that part is easy.
If you just wanted to have the pattern words as column headers, we could stop there.
But since you want to have the pattern words precede the value in each cell, we have to Unpivot the table; create a merged column of the pattern word + : + value, then re-pivot on the Attribute column
To use it:
Select some cell in your Data Table
Data => Get&Transform => from Table/Range
When the PQ Editor opens: Home => Advanced Editor
Make note of the Table Name in Line 2
Paste the M Code below in place of what you see
Change the Table name in line 2 back to what was generated originally.
Read the comments and explore the Applied Steps to understand the algorithm
M Code
let
Source = Excel.CurrentWorkbook(){[Name="Table7"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Column1", type text}}),
//List to split on
codeList = {
"amount:",
"price:",
"price2:",
"status:",
"min:",
"opt:",
"category:",
"code z:"},
//strip off terminal colon (:) to create list of column headers
colNames = List.Transform(codeList, each Text.TrimEnd(_,":")),
//split on the code list, and name the new columns
split = Table.SplitColumn(#"Changed Type","Column1",
Splitter.SplitTextByEachDelimiter(codeList),List.Combine({{"Column1"},colNames})),
//remove the now empty column1
#"Removed Columns" = Table.RemoveColumns(split,{"Column1"}),
//could stop here if didn't have to merge the column header with the value
//To prefix each measurement with pattern word
#"Unpivoted Columns" = Table.UnpivotOtherColumns(#"Removed Columns", {}, "Attribute", "Value"),
#"Duplicated Column" = Table.DuplicateColumn(#"Unpivoted Columns", "Attribute", "Attribute - Copy"),
#"Merged Columns" = Table.CombineColumns(#"Duplicated Column",
{"Attribute", "Value"},Combiner.CombineTextByDelimiter(":", QuoteStyle.None),"Merged"),
rePivot = fnPivotAll(#"Merged Columns","Attribute - Copy","Merged")
in
rePivot
custom function
enter as blank query
rename fnPivotAll
//credit: Cam Wallace https://www.dingbatdata.com/2018/03/08/non-aggregate-pivot-with-multiple-rows-in-powerquery/
(Source as table,
ColToPivot as text,
ColForValues as text)=>
let
PivotColNames = List.Buffer(List.Distinct(Table.Column(Source,ColToPivot))),
#"Pivoted Column" = Table.Pivot(Source, PivotColNames, ColToPivot, ColForValues, each _),
TableFromRecordOfLists = (rec as record, fieldnames as list) =>
let
PartialRecord = Record.SelectFields(rec,fieldnames),
RecordToList = Record.ToList(PartialRecord),
Table = Table.FromColumns(RecordToList,fieldnames)
in
Table,
#"Added Custom" = Table.AddColumn(#"Pivoted Column", "Values", each TableFromRecordOfLists(_,PivotColNames)),
#"Removed Other Columns" = Table.RemoveColumns(#"Added Custom",PivotColNames),
#"Expanded Values" = Table.ExpandTableColumn(#"Removed Other Columns", "Values", PivotColNames)
in
#"Expanded Values"
Raw Data
Split Data
Alternative solution
Try the next function, please:
Function extractFrStr(strInit As String) As Variant
Dim arrStr, arrFin, i As Long, k As Long
Dim arr1, arr2, firstEl As String, secEl As String
arrStr = Split(strInit, ":") 'split the string by ":" character
ReDim arrFin(UBound(arrStr)) 'ReDim the final array at the same number of elements
For i = 0 To UBound(arrStr) - 1 'iterate between the array elements (except the last)
arr1 = Split(arrStr(i), " ") 'split the i element by space (" ")
arr2 = Split(arrStr(i + 1), " ") 'split the i + 1 element by space (" ")
If i = 0 Then 'for the first array element:
firstEl = arrStr(i) 'it receives the array elemen value
Else 'for the rest of array elements:
'extract firstEl (category) like first arr1 element, except the case of 'code z' which is extracted in a different way
firstEl = IIf(i = UBound(arrStr) - 1, arr1(UBound(arr1) - 1) & " " & arr1(UBound(arr1)), arr1(UBound(arr1)))
End If
'in order to remove array elements, the code transformes the one to be removed in "|||":
'it could be anything, but "|||" is difficult to suppose that it will be the text of a real element...
If arrStr(i + 1) <> "" Then arr2(UBound(arr2)) = "|||": If i = UBound(arrStr) - 2 Then arr2(UBound(arr2) - 1) = "|||"
'extract the secEl (the value) by joining the array after removed firstEl:
secEl = IIf(i = UBound(arrStr) - 1, arrStr(UBound(arrStr)), Join(Filter(arr2, "|||", False), " "))
arrFin(k) = firstEl & ":" & secEl: k = k + 1 'create the processed element of the array to keep the result
Next i
extractFrStr = arrFin
End Function
And use it to process your range, in the next way:
Sub SplitCategories()
Dim sh As Worksheet, c As Range, arr
Set sh = ActiveSheet
For Each c In sh.Range("E24:E33").cells
arr = extractFrStr(Trim(c.value))
c.Offset(0, 1).Resize(1, UBound(arr)).value = arr
Next c
End Sub
Well OK, that's mostly the code I gave you in a previous question/answer. In order to implement a split of cells in E24:E33, you just need to feed those cells to the SpecialSpread routine... all the qtest stuff is just testing routines.
So this adds a SpreadZone routine to act on that specific range:
Function SpecialSplit(Compound As String) As Variant
Dim pos1 As Long, pos2 As Long
Dim firstSplit As Variant
Dim elmts As Integer
Dim elmt As Integer
Dim oneel As String
firstSplit = Split(Compound, ":")
For elmt = LBound(firstSplit) To UBound(firstSplit) - 1
oneel = firstSplit(elmt + 1)
pos1 = InStr(oneel, " ")
pos2 = Len(oneel)
While pos1 > 0 And pos1 < Len(oneel) - 1
pos2 = pos1
pos1 = InStr(pos2 + 1, oneel, " ")
Wend
firstSplit(elmt) = firstSplit(elmt) & ":" & Left(oneel, pos2 - 1)
firstSplit(elmt + 1) = Right(oneel, Len(oneel) - pos2)
Next elmt
elmts = UBound(firstSplit) - LBound(firstSplit) ' one shorter
If elmts > 0 Then
oneel = firstSplit(UBound(firstSplit))
ReDim Preserve firstSplit(1 To elmts)
firstSplit(elmts) = firstSplit(elmts) & ":" & oneel
End If
SpecialSplit = firstSplit
End Function
Sub SpecialSpread(FromCell As Range)
Dim splitSet As Variant
splitSet = SpecialSplit(FromCell(1).Text)
If UBound(splitSet) >= 0 Then
FromCell(1).Offset(0, 1).Resize(1, UBound(splitSet) - LBound(splitSet) + 1).value = splitSet
End If
End Sub
Sub SpreadZone()
Dim ACell As Range
For Each ACell In Range("E24:E33")
Call SpecialSpread(ACell)
Next ACell
End Sub
Edit: tweaked SpecialSplit a little to improve last field & correct empty field handling

vba - sum unique with multiple conditions

I would like to create a macro that will sum the unit column base by Product & DC. It will populate out the same product name, code with dc code and code without. Base on the code given I try to modify the code but fail when I added the column Sales Person in front, Date & Country, as output I want to show the data with the latest date and country and the new data in the column.
Sub Button1_Click()
' Define constants.
Const sName As String = "Sheet1"
Const sFirstCellAddress As String = "A2"
Const dName As String = "Sheet2"
Const dFirstCellAddress As String = "A1"
' Define workbook.
Dim wb As Workbook: Set wb = ThisWorkbook
' Write values from Source Range to Data Array.
Dim LastRow As Long
Dim Data As Variant
With wb.Worksheets(sName).Range(sFirstCellAddress)
LastRow = .Worksheet.Cells(.Worksheet.Rows.Count, .Column).End(xlUp).Row
Data = .Resize(LastRow - .Row + 1, 8) 'modify
End With
' Write unique values from Data Array to Unique Dictionary.
Dim dict As Object: Set dict = CreateObject("Scripting.Dictionary")
dict.CompareMode = vbTextCompare
Dim arr(1 To 2) As Variant
Dim Key As Variant
Dim cArr As Variant
Dim r As Long
For r = 1 To UBound(Data, 1)
Key = Data(r, 1)
If Not IsError(Key) Then
If Not dict.Exists(Key) Then
dict.Add Key, arr
End If
If Data(r, 2) = 0 Then
cArr = dict(Key)
cArr(2) = cArr(2) + Data(r, 3)
dict(Key) = cArr
Else
cArr = dict(Key)
cArr(1) = cArr(1) + Data(r, 3)
dict(Key) = cArr
End If
End If
Key2 = Data(r, 4)
Key3 = Data(r, 5)
Key4 = Data(r, 6)
Key5 = Data(r, 7)
Key6 = Data(r, 8)
Next r
' Write values from Unique Dictionary to Result Array.
Dim rCount As Long: rCount = dict.Count + 1
Dim Result As Variant: ReDim Result(1 To rCount, 1 To 8) 'change from 3 to 8
Result(1, 1) = "SalesPerson"
Result(1, 2) = "Company"
Result(1, 3) = "Product"
Result(1, 4) = "Unit With DC Code"
Result(1, 5) = "Unit Without DC Code"
Result(1, 6) = "Date"
Result(1, 7) = "Country"
Result(1, 8) = "Grade"
If rCount > 1 Then
r = 1
For Each Key In dict.Keys
r = r + 1
Result(r, 1) = Key1
Result(r, 2) = Key2
Result(r, 3) = Key3
Result(r, 4) = CLng(dict(Key)(1))
Result(r, 5) = CLng(dict(Key)(2))
Result(r, 6) = Key4
Result(r, 7) = Key5
Result(r, 8) = Key6
Next Key
End If
' Write values from Result Array to Destination Range.
With wb.Worksheets(dName).Range(dFirstCellAddress).Resize(, 5)
.Resize(rCount).Value = Result
' Delete below.
'.Resize(.Worksheet.Rows.Count - .Row - rCount + 1) _
.Offset(rCount).ClearContents
End With
MsgBox ("Done")
End Sub
SalesPerson
Company
Product
DC
Unit
Date
Country
Grade
JOHN
TPP
ABC
2
12-Feb
MY
AA
JOHN
TPP
ABC
1234
4
13-Feb
MY
AA
JOHN
TPP
ABC
1234
4
14-Feb
US
AA
JOHN
PEN
DEF
5678
2
12-Feb
US
AA
JOHN
PEN
DEF
5678
2
18-Feb
MY
AA
JOHN
PKG
GHI
9012
2
16-Feb
UK
AA
I want to the output in another sheets as below:-
SalesPerson
Company
Product
Unit with DC Code
Unit Without DC Code
Date
Country
Grade
JOHN
TPP
ABC
6
2
14-Feb
US
AA
JOHN
PEN
DEF
4
18-Feb
MY
AA
JOHN
PKG
GHI
2
16-Feb
UK
AA
You need to build the dictionary key from the first 3 fields SalesPerson,Company,Product. I have concatenated them with tab character so they can be separated later for the results sheet.
Option Explicit
Sub Button1_Click()
' Define constants.
Const sName As String = "Sheet1"
Const sFirstCellAddress As String = "A2"
Const dName As String = "Sheet2"
Const dFirstCellAddress As String = "A1"
' Define workbook.
Dim wb As Workbook: Set wb = ThisWorkbook
' Write values from Source Range to Data Array.
Dim LastRow As Long
Dim Data As Variant
With wb.Worksheets(sName).Range(sFirstCellAddress)
LastRow = .Worksheet.Cells(.Worksheet.Rows.Count, .Column).End(xlUp).Row
Data = .Resize(LastRow - .Row + 1, 8) 'modify
End With
' Write unique values from Data Array to Unique Dictionary.
Dim dict As Object: Set dict = CreateObject("Scripting.Dictionary")
dict.CompareMode = vbTextCompare
Dim arr(3) As Variant, Key As String, tmp As Variant
Dim r As Long, i As Long
For r = 1 To UBound(Data, 1)
' composite key SalesPerson,Company,Product
Key = Data(r, 1) & vbTab & Data(r, 2) & vbTab & Data(r, 3)
If Not IsError(Key) Then
If Not dict.Exists(Key) Then
dict.Add Key, Array(0, 0, r)
End If
i = 0
If Len(Data(r, 4)) = 0 Then i = 1
' sum counts
tmp = dict(Key)
tmp(i) = tmp(i) + Data(r, 5)
tmp(2) = r ' store last row
dict(Key) = tmp
End If
Next r
' result
Dim k As Variant, wsOut As Worksheet, rngOut As Range
Set wsOut = wb.Worksheets(dName)
Set rngOut = wsOut.Range(dFirstCellAddress)
rngOut.Resize(, 8) = Array("SalesPerson", "Company", "Product", "Unit With DC Code", _
"Unit Without DC Code", "Date", "Country", "Grade")
For Each k In dict
Set rngOut = rngOut.Offset(1, 0)
tmp = Split(k, vbTab)
rngOut.Offset(0, 0) = tmp(0)
rngOut.Offset(0, 1) = tmp(1)
rngOut.Offset(0, 2) = tmp(2)
rngOut.Offset(0, 3) = dict(k)(0)
rngOut.Offset(0, 4) = dict(k)(1)
' Date Country Grade
r = dict(k)(2)
rngOut.Offset(0, 5) = Data(r, 6)
rngOut.Offset(0, 6) = Data(r, 7)
rngOut.Offset(0, 7) = Data(r, 8)
Next
MsgBox "Done", vbInformation
End Sub
In addition to a VBA solution (and I would probably use a dictionary with the SalesPerson as the key, and a class object containing the relevant information), I would suggest also looking at Power Query (available in Excel 2010+).
It may prove easier to maintain and modify once you get used to the language.
For the task at hand, you could start, for example, by
select a cell in the data table
Data => Get&Transform => from Table/Range
When the PQ UI opens, goto Home => Advanced Editor
Note the name of the table in line 2 of the code you see.
Then replace that code with the M-Code below, changing the table name to what your actual table name is.
You should be able to figure out what is happening by going through the applied steps window in the UI, and also reading the annotations.
To update, you merely Refresh the query. And you could program a button to do that.
M Code edited and shortened
let
//change table name in next line to the real table name in your workbook
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,
{{"SalesPerson", type text}, {"Company", type text}, {"Product", type text}, {"DC", Int64.Type},
{"Unit", Int64.Type}, {"Date", type date}, {"Country", type text}, {"Grade", type text}}),
//Group by SalesPerson, Company and Product
//Extract the needed information
#"Grouped Rows" = Table.Group(#"Changed Type", {"SalesPerson", "Company", "Product"},
{
{"Unit with DC Code",(t) => List.Sum(Table.Column(Table.SelectRows(t, each [DC] <> null),"Unit")), Int64.Type},
{"Unit without DC Code", (t) => List.Sum(Table.Column(Table.SelectRows(t, each [DC] = null),"Unit")),Int64.Type},
{"Date", each List.Max([Date]), type nullable date},
{"Country", (t)=> Table.Column(Table.SelectRows(t, each [Date] = List.Max(Table.Column(t,"Date"))),"Country"){0},type text},
{"Grade", (t)=> Table.Column(Table.SelectRows(t, each [Date] = List.Max(Table.Column(t,"Date"))),"Grade"){0}, type text}
})
in
#"Grouped Rows"

Splitting and replacing delimited strings into new rows using VBA in Excel

I have data in 2 columns. The data in column B is comma delimited. I need each instance to appear on a new row while retaining it's original ID from column A. I also need the data in 3 columns so Name is in B and Number in C. It appears as so:
A--------B
1--------Sam Jones, 1 hours, Chris Bacon, 2 hours
2--------John Jacob, 3 hours
3--------John Hancock, 4 hours, Brian Smith, .5 hours
I am able to get it as such using my code below:
A--------B
1--------Sam Jones, 1
1--------Chris Bacon, 2 hours
2--------John Jacob, 3 hours
3--------John Hancock, 4
3--------Brian Smith, .5 hours
I need it to be: (notice last value in string also has hours removed when added to new line)
A---------B------------------------C
1---------Sam Jones-----------1
1---------Chris Bacon----------2
2---------John Jacob-----------3
3---------John Hancock-------4
3---------Brian Smith----------.5
I have the following code started: (I can't manage to remove the "hours" from the last person in each delimited string and I can't get it into 3 columns)
Sub splitByColB()
Dim r As Range, i As Long, ar
Set r = Worksheets("Sheet1").Range("B2").End(xlDown)
Do While r.Row > 1
ar = Split(r.Value, " hours, ")
If UBound(ar) >= 0 Then r.Value = ar(0)
For i = UBound(ar) To 1 Step -1
r.EntireRow.Copy
r.Offset(1).EntireRow.Insert
r.Offset(1).Value = ar(i)
Next
Set r = r.Offset(-1)
Loop
End Sub
Something like this is what you're looking for:
Sub tgr()
Dim ws As Worksheet
Dim aData As Variant
Dim aTemp As Variant
Dim aResults(1 To 65000, 1 To 3) As Variant
Dim ResultIndex As Long
Dim i As Long, j As Long
Set ws = ActiveWorkbook.Sheets("Sheet1")
With ws.Range("B2", ws.Cells(ws.Rows.Count, "B").End(xlUp))
If .Row < 2 Then Exit Sub 'No data
aData = .Offset(, -1).Resize(, 2).Value
End With
For i = LBound(aData, 1) To UBound(aData, 1)
If Len(Trim(aData(i, 2))) = 0 Then
ResultIndex = ResultIndex + 1
aResults(ResultIndex, 1) = aData(i, 1)
Else
aTemp = Split(aData(i, 2), ",")
For j = LBound(aTemp) To UBound(aTemp) Step 2
ResultIndex = ResultIndex + 1
aResults(ResultIndex, 1) = aData(i, 1)
aResults(ResultIndex, 2) = Trim(aTemp(j))
aResults(ResultIndex, 3) = Trim(Replace(aTemp(j + 1), "hours", vbNullString, , , vbTextCompare))
Next j
End If
Next i
ws.Range("A2").Resize(ResultIndex, UBound(aResults, 2)).Value = aResults
End Sub
You can use Power Query. It is a free MS add-in in 2010, 2013 and included in 2016 where it is called Get & Transform
Split column 2 by delimiter custom --> hours,
Select the ID column and unpivot other columns
Select column 2 and split by delimiter = comma
Remove unnecessary column
Replace value "hours"
And if you add to the table, you can re-run the query
let
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"ID", Int64.Type}, {"Data", type text}}),
#"Split Column by Delimiter" = Table.SplitColumn(#"Changed Type", "Data", Splitter.SplitTextByDelimiter("hours,", QuoteStyle.Csv), {"Data.1", "Data.2"}),
#"Changed Type1" = Table.TransformColumnTypes(#"Split Column by Delimiter",{{"Data.1", type text}, {"Data.2", type text}}),
#"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Changed Type1", {"ID"}, "Attribute", "Value"),
#"Split Column by Delimiter1" = Table.SplitColumn(#"Unpivoted Other Columns", "Value", Splitter.SplitTextByDelimiter(",", QuoteStyle.Csv), {"Value.1", "Value.2"}),
#"Changed Type2" = Table.TransformColumnTypes(#"Split Column by Delimiter1",{{"Value.1", type text}, {"Value.2", type text}}),
#"Removed Columns" = Table.RemoveColumns(#"Changed Type2",{"Attribute"}),
#"Replaced Value" = Table.ReplaceValue(#"Removed Columns","hours","",Replacer.ReplaceText,{"Value.2"})
in
#"Replaced Value"
I would use a class with the name data
Option Explicit
Public Id As String
Public FullName As String
Public hours As String
and the following code
Option Explicit
Sub SplitIt()
Dim rg As Range
Dim col As New Collection
Dim dataLine As data
Set rg = Worksheets("Sheet1").Range("A1").CurrentRegion
Dim vDat As Variant
vDat = rg.Columns
Dim lDat As Variant
Dim i As Long, j As Long
For i = LBound(vDat) To UBound(vDat)
lDat = Split(vDat(i, 2), ",")
For j = LBound(lDat) To UBound(lDat) Step 2
Dim hDat As Variant
hDat = Split(Trim(lDat(j + 1)), " ")
Set dataLine = New data
dataLine.Id = vDat(i, 1)
dataLine.FullName = Trim(lDat(j))
dataLine.hours = hDat(0)
col.Add dataLine
Next j
Next i
' Print Out
For i = 1 To col.Count
Set dataLine = col(i)
rg.Cells(i, 1) = dataLine.Id
rg.Cells(i, 2) = dataLine.FullName
rg.Cells(i, 3) = dataLine.hours
Next i
End Sub
Why not split on hours to a) add a record delimiter and b) get rid of hours?
Option Explicit
Sub splitByColB()
Dim r As Long, i As Long, hrs As Variant, cms As Variant
With Worksheets("sheet1")
For r = .Cells(.Rows.Count, "B").End(xlUp).Row To 2 Step -1
hrs = Split(.Cells(r, "B").Value2 & ", ", " hours, ")
ReDim Preserve hrs(UBound(hrs) - 1)
If CBool(UBound(hrs)) Then _
.Cells(r, "A").Offset(1, 0).Resize(UBound(hrs), 1).EntireRow.Insert
For i = UBound(hrs) To LBound(hrs) Step -1
cms = Split(hrs(i), ", ")
.Cells(r, "A").Offset(i, 0) = .Cells(r, "A").Value
.Cells(r, "A").Offset(i, 1) = cms(0)
.Cells(r, "A").Offset(i, 2) = cms(1)
Next i
Next r
End With
End Sub

Resources