Results 1 to 6 of 6

Populating Hierarchical Data via VBA with named table.

Threaded View

  1. #1
    Registered User
    Join Date
    01-09-2013
    Location
    Cincinnati, OH
    MS-Off Ver
    MSO 365
    Posts
    3

    Populating Hierarchical Data via VBA with named table.

    Hai. Long time lurker, first time poster.

    I have a report coming of hierarchical data, coming out of SAP, that I need to use to extrapolate metrics, etc. This data isn't very useful as a pivot table as each level requires data manipulation in order to populate all of the required fields so that Excel knows how to organize it.

    I've created a very rudamentary script that auto-populates the data on command but it runs extremely slow. This data amounts to over 5000 rows and will continue to grow. Yesterday it took over two hours to complete so there must be a quicker way to accomplish what I'm asking.

    In a nutshell, the data looks like this:
    Capture_zpse2617b69.jpg

    and the code looks like this:
    Sub Populate_Hierarchy()
        Dim i, x As Integer
    
    x = Cells.Find(What:="*", SearchOrder:=xlRows, SearchDirection:=xlPrevious, LookIn:=xlValues).Row
    
    Application.EnableEvents = False
    Application.ScreenUpdating = False
    
        For i = 2 To x
            Application.StatusBar = "Progress: " & i & " of " & x & ": " & Format(i / x, "0%")
    
            If Not IsEmpty(Range("$G$" & i)) Then If IsEmpty(Range("$F$" & i)) Then Range("$F$" & i).Value = Range("$F$" & i - 1).Value
            If Not IsEmpty(Range("$F$" & i)) Then If IsEmpty(Range("$E$" & i)) Then Range("$E$" & i).Value = Range("$E$" & i - 1).Value
            If Not IsEmpty(Range("$E$" & i)) Then If IsEmpty(Range("$D$" & i)) Then Range("$D$" & i).Value = Range("$D$" & i - 1).Value
            If Not IsEmpty(Range("$D$" & i)) Then If IsEmpty(Range("$C$" & i)) Then Range("$C$" & i).Value = Range("$C$" & i - 1).Value
            If Not IsEmpty(Range("$C$" & i)) Then If IsEmpty(Range("$B$" & i)) Then Range("$B$" & i).Value = Range("$B$" & i - 1).Value
        Next
    
    Application.StatusBar = False
    Application.ScreenUpdating = True
    Application.EnableEvents = True
    
    End Sub
    Also, for your amusement, I have attached a dummy worksheet with 1200 lines of data for testing (and the code).

    The work has to exist in VBA as the data will be replaced (copy-paste) on regular intervals, so anything in the cells will be wiped out each time (SAP creates a new .xls file after running the report) and I run this on-demand as opposed to an event so not as to slow down the other tabs within the worksheet.

    The logic behind the code is simple. If the lowest level doesn't have any data, the next lowest won't repeat (pivot table assignment), and so on.

    Example:
    Capture_zps6894f26c.jpg

    Unfortunately, I can't think of a better method to do this. I thought about using the autofill, but wasn't sure the best way to identify where "level 6" started and where "level 5" should stop. Really, I was completely thrown off on how long my code took to actually complete.

    Thanks.
    Attached Files Attached Files

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1