Using VBA instead of power query

jharding

New Member
Joined
May 30, 2018
Messages
9
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
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
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:
Upvote 0
Cross posted https://www.excelforum.com/excel-pr...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.
 
Upvote 0
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
 
Upvote 0

Forum statistics

Threads
1,213,494
Messages
6,113,972
Members
448,537
Latest member
Et_Cetera

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top