简体   繁体   中英

Easier Code for Taxonomy in VBA

My Bigcommerce site has Categories that have Parent/Child setups (for exmaple):

Food>

     Jam>

     Coffee>

Clothes>

     Shirts>

           Dress>

           T-Shirts>

When I pull the Categories from the API, it gives me the Category name, and each Categories Parent Category in an excel sheet:

ID Parent Name

101 0 Food

102 101 Jam

103 101 Coffee

I am trying to create the taxonomy so I get

Food

Food>Jam

Food>Coffee

Clothes

Clothes>Shirts

Clothes>Shirts>Dress

I have been struggling to find a way using loops in VBA to get this end result, without success.

Not looking for you to write my code, but any guidance on the best way to approach this?

Your structure is similar to a paragraph numbering structure or a WBS (work breakdown structure)

In a paragraph numbering structure the ID for the sub or sub-subparagraph is the right-most number:

1.1.2.3  Whatever

the 3 is the ID for the paragraph. In your case, it appears that the item ID is the left-most number.


I would go through the data in two passes:

  • in the first pass build up a translate table associating the Name of an item with its Number
  • in the second pass, replace the numbers with the associated name and format properly the right-to-left nature of each record

Once you have firmed-up your approach and written some code, we can help you further.

this is some code to get you in the right direction

input 输入

output 输出

my code which is looping through the special cells and concat them and add the results to new sheet

Sub ashTax()

Dim wsCopyFrom As Worksheet
Dim wsCopyTo As Worksheet
Dim rng As Range
Dim cell As Range
Dim str As String

Set wsCopyFrom = ThisWorkbook.ActiveSheet
ThisWorkbook.Worksheets.Add after:=wsCopyFrom
Set wsCopyTo = ThisWorkbook.ActiveSheet

Set rng = wsCopyFrom.UsedRange.SpecialCells(xlCellTypeConstants)


wsCopyTo.[a1].Value = "Results"

For Each cell In rng
str = str + cell.Value
Cells((Cells(Rows.Count, "a").End(xlUp).Row) + 1, "a").Value = str
Next

End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM