Using VBA instead of power query

jharding

New Member
Joined
May 30, 2018
Messages
3
i had created an excel 2016 sheet that took 4 columns of data (week, meal time, meal name, food item) and transposed and grouped that data using power query to create a list of food items, by week (columns) and grouped by meal time/name as rows.


unfortunately, this was developed for a mac, where Excel for mac wont recognize power query or pivot tools and scripts


I'm trying to think if I could write VBA code to perform the same function:
1) Find unique week numbers (column a) and list each as column in result tab
2.) list meal time/meal names (column b and c, concatenated) as rows
3.) group food items (column d) into each week column and meal row.


Columns A-D are in a defined table (Table1), so I can use listobject class as my reference


Where would I start with this?


Thank you for your help
 

ttray33y

Board Regular
Joined
Nov 10, 2015
Messages
246
Try.
Code:
Sub test()
    
    Dim w1 As Worksheet
    Dim w2 As Worksheet
    Dim Target As Variant, wDest As Variant
    Dim fROW As Long, i As Long, cnt As Long


    
    Set w1 = ActiveWorkbook.Sheets("Sheet1") [COLOR=#00ff00]'modify as per worksheet name[/COLOR]
    Set w2 = ActiveWorkbook.Sheets("Result")[COLOR=#00ff00] 'modify as per worksheet name, this is your destination or result worksheet[/COLOR]
    
    w1.Select
    w2.UsedRange.ClearContents
    
    Target = w1.UsedRange
    
        w1.UsedRange.Copy Destination:=w2.Cells(1, 1)
    
    wDest = w2.UsedRange
    
    fROW = 2
    cnt = 2: i = fROW
        Do  [COLOR=#00ff00]'start loop[/COLOR]
            fROW = fROW + 1
                If wDest(fROW, 1) = wDest(i, 1) Then
                    cnt = cnt + 1
                        If cnt > UBound(wDest, 2) Then ReDim Preserve wDest(1 To UBound(wDest, 1), 1 To cnt)
                            wDest(i, cnt) = wDest(fROW, 2)
        Else
            cnt = 2
            i = i + 1
        End If
                If i <> fROW Then
                    wDest(i, 1) = wDest(fROW, 1): wDest(fROW, 1) = ""
                        wDest(i, 2) = wDest(fROW, 2): wDest(fROW, 2) = ""
        End If
        
        Loop Until fROW = UBound(wDest, 1)
        w2.UsedRange.Resize(, UBound(wDest, 2)).Value = wDest


End Sub
 
Last edited:

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
32,270
Office Version
365
Platform
Windows
Cross posted https://www.excelforum.com/excel-programming-vba-macros/1232547-using-vba-instead-of-power-query.html

Cross-Posting
While we do not prohibit Cross-Posting on this site, we do ask that you please mention you are doing so and provide links in each of the threads pointing to the other thread (see rule 13 here along with the explanation: Forum Rules).
This way, other members can see what has already been done in regards to a question, and do not waste time working on a question that may already be answered.
 

jharding

New Member
Joined
May 30, 2018
Messages
3
I used a combination of the code sample here and another one to formulate my final solution. Thank you for your help with this challenge
 

Forum statistics

Threads
1,082,126
Messages
5,363,321
Members
400,725
Latest member
excelingtolearn

Some videos you may like

This Week's Hot Topics

  • populate from drop list with multiple tables
    Hi All, i have a drop list that displays data, what i want is when i select one of those from the list to populate text from different tables on...
  • Find list of words from sheet2 in sheet1 before a comma and extract text vba
    Hi Friends, Trying to find the solution on my task. But did not find suitable one to the need. Here is my query and sample file with details...
  • Dynamic Formula entry - VBA code sought
    Hello, really hope one of you experts can help with this - i've spent hours on this and getting no-where. .I have a set of data (more rows than...
  • Listbox Header
    Have a named range called "AccidentsHeader" Within my code I have: [CODE]Private Sub CommandButton1_Click() ListBox1.RowSource =...
  • Complex Heat Map using conditional formatting
    Good day excel world. I have a concern. Below link have a list of countries that carries each country unique data. [URL...
  • Conditional formatting
    Hi good morning, hope you can help me please, I have cells P4:P54 and if this cell is equal to 1 then i want row O to say "Fully Utilised" and to...
Top