79286521

Date: 2024-12-17 02:18:46
Score: 0.5
Natty:
Report link

help me dubug this code at With outputSheet.ListObjects.Add..

Sub AutomatePowerQueryWithMCode() Dim wb As Workbook Dim pqQuery As WorkbookQuery Dim mCode As String Dim outputSheet As Worksheet Dim tblName As String

' Set workbook and output details
Set wb = ThisWorkbook
Set outputSheet = wb.Sheets("TransformedData") ' Change to your desired output sheet
tblName = "TransformedTable" ' Desired output table name

' Power Query M code
mCode = _
    "let" & vbCrLf & _
    "    Source = Excel.Workbook(File.Contents(""/Users/deepankarrijal/Downloads/Excel Work/PR 2024.11.27 Ready to post.xlsm""), null, true)," & vbCrLf & _
    "    #""Navigation 1"" = Source{[Item = ""Summary"", Kind = ""Sheet""]}[Data]," & vbCrLf & _
    "    #""Changed column type"" = Table.TransformColumnTypes(#""Navigation 1"", {{""Column4"", type text}}, ""en-US"")," & vbCrLf & _
    "    #""Removed top rows"" = Table.Skip(#""Changed column type"", 3)," & vbCrLf & _
    "    #""Choose columns"" = Table.SelectColumns(#""Removed top rows"", {""Column1"", ""Column2"", ""Column3"", ""Column4"", ""Column5""})," & vbCrLf & _
    "    #""Promoted headers"" = Table.PromoteHeaders(#""Choose columns"", [PromoteAllScalars = true])" & vbCrLf & _
    "in" & vbCrLf & _
    "    #""Promoted headers"""

' Add or update the Power Query
On Error Resume Next
Set pqQuery = wb.Queries("MyQuery")
If pqQuery Is Nothing Then
    Set pqQuery = wb.Queries.Add("MyQuery", mCode)
Else
    pqQuery.Formula = mCode
End If
On Error GoTo 0

' Load the query result into the worksheet
With outputSheet.ListObjects.Add(SourceType:=xlSrcQuery, Source:="Query - MyQuery", Destination:=outputSheet.Range("A1"))
    .Name = tblName
    .TableStyle = "TableStyleLight9"
MsgBox "Power Query transformation applied successfully!", vbInformation

End Sub

Reasons:
  • Blacklisted phrase (1): help me
  • Long answer (-1):
  • Has code block (-0.5):
  • Low reputation (1):
Posted by: Deepankar Rijal