简体   繁体   English

VBA基于单元格值自动执行Power Query

[英]VBA automating Power Query based on cell values

I have a question about VBA mainly in this. 我对VBA的问题主要在于此。 I recorded a macro, when creating a query connection to an SQL server. 在创建与SQL服务器的查询连接时,我记录了一个宏。

In my Sheet (Sheet1) I have listed a couple of values, which tells the name of the SQL Server, the Database, the Table name and the year value (being 2019). 在我的Sheet(Sheet1)中,我列出了几个值,它们告诉SQL Server的名称,数据库,表名和年份值(2019年)。

It works almost great, but I get some issues with the Source coding. 它的工作效果非常好,但是我对Source编码有些疑问。 See below: 见下文:

    Sub Macro1()
Dim sht As Worksheet
Set sht = Sheets("Sheet1")
Dim SQLSRV, DBase, YValue, Table As String

SQLSRV = sht.Range("B:B").Find("SQL Server").Offset(0, 1).Value
DBase = sht.Range("B:B").Find("Database").Offset(0, 1).Value
YValue = sht.Range("B:B").Find("Year").Offset(0, 1).Value
Table = sht.Range("B:B").Find("Acccount").Offset(0, 1).Value


    ActiveWorkbook.Queries.Add Name:="VAT Sales", Formula:= _
        "let" & Chr(13) & "" & Chr(10) & "    Source = Sql.Database(" & SQLSRV & ", " & DBase & ", [Query=""select [Posting Date]#(lf)" & _
        "" & "        ,[G_L Account No_]#(lf)        ,[Document No_]#(lf)        ,[Description]#(lf)        ,[Amount]#(lf)        ,[VAT Amount]#(lf)" & _
        "" & "        ,[Source Code]#(lf)        ,[VAT Bus_ Posting Group]#(lf)        ,[VAT Prod_ Posting Group]#(lf)        ,[Gen_ Posting Type]#(lf)" & _
        "" & "        ,[External Document No_]#(lf)        ,[Source No_]#(lf)        ,[User ID]#(lf)        ,[Unique Document No_]#(lf)        ,[Open Kvik]#(lf)" & _
        "" & "        ,[Remaining Amount Kvik]#(lf) from [" & DBase & "].[dbo].[" & Table & "$G_L Entry]#(lf)where [Posting Date] >= '01-01-" & YValue & "'""])" & _
        "" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & "    Source"
    End Sub

My issue comes at the section " Source = Sql.Database(" & SQLSRV & ", " & DBase & ", … 我的问题出现在“Source = Sql.Database(”&SQLSRV&“,”&DBase&“,...
For Power Query to read this correctly I need to be able to insert a quote at the beginning, and at the end of the variable, and still be able to transfer the value of the variable. 要使Power Query正确读取,我需要能够在变量的开头和结尾处插入引号,并且仍然能够传输变量的值。

Is there some kind of Chr() that I'm not aware of? 是否有某种我不知道的Chr()?

Thank you in advance :) 先感谢您 :)

后来我发现Chr(34)做了诀窍:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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