简体   繁体   English

如何使用excel vba宏将变量值插入到getpivotdata公式中

[英]How to insert variable value into getpivotdata formula using excel vba macro

So I've learned pivot tables are tricky, so are string variable syntax, and so is the use of quotations in VBA. 所以我学习了数据透视表很棘手,字符串变量语法也是如此,在VBA中使用引号也是如此。 I'm trying (and failing) to use all three! 我正在尝试(并且失败)使用这三个!

I am trying to collect one piece of data from a very large/complex pivot table for a large number of job numbers. 我正在尝试从一个非常大/复杂的数据透视表中收集一个数据来获取大量的工作号。 I want to select a job number in A1, and have the pivot table formula automatically update with the selected job number to return a number result. 我想在A1中选择一个作业号,并使用所选的作业号自动更新数据透视表公式以返回一个数字结果。

I am currently having two problems: 1) the syntax to get the job number in quotations for the 'getpivotdata' formula isn't working and 2)it is dropping off the zeros in the job number when it executes the code. 我目前遇到两个问题:1)获取'getpivotdata'公式的引号中的作业号的语法不起作用; 2)它在执行代码时从作业号中删除零。

My code is listed below, when I execute it I am hoping to see this formula populate in A2: 我的代码列在下面,当我执行它时,我希望看到这个公式填充在A2中:

=GETPIVOTDATA("Part Number",' Parts Status '!$A$8,"CHAR_FIELD3","11-008","MATERIAL STATUS MASTER","Avail")

but instead get: 而是得到:

=GETPIVOTDATA("Part Number",' Parts Status '!$A$8,"CHAR_FIELD3",11-8,"MATERIAL STATUS MASTER","Avail")

My code is here 我的代码在这里

Sub Macro1()

Dim jobnumber As String

jobnumber = Worksheets("Macros test").Cells(1, "A").Value

Sheets("Macros test").Select
    Range("A2").Select
    ActiveCell.FormulaR1C1 = _
        "=GETPIVOTDATA(""Part Number"",' Parts Status '!R8C1,""CHAR_FIELD3""," & jobnumber & ",""MATERIAL STATUS MASTER"",""Avail"")"

If you need the quotes there, change it to this: 如果您需要那里的引号,请将其更改为:

"=GETPIVOTDATA(""Part Number"",' Parts Status '!R8C1,""CHAR_FIELD3"",""" & jobnumber & """,""MATERIAL STATUS MASTER"",""Avail"")"

Also, you can add a temporary msgbox after setting jobnumber to make sure it is what you think it is: 此外,您可以在设置jobnumber后添加临时msgbox,以确保它符合您的想法:

MsgBox(jobnumber)

I've always used the Chr() function to deal with quotes in VB. 我总是使用Chr()函数来处理VB中的引号。 Double quote is Chr(34) and single quote is Chr(39). 双引号是Chr(34),单引号是Chr(39)。 The problem you experienced was that while jobnumber is defined as a string you didn't have the double quotes around it. 您遇到的问题是,虽然jobnumber被定义为字符串,但您没有双引号。 The result is Excel removed the leading zeros on the formula 11-008, which Excel saw as Eleven minus eight. 结果是Excel删除了公式11-008上的前导零,Excel将其视为Eleven减去8。 Adding the double quotes (Chr(34)) around jobnumber solved the problem. 在jobnumber周围添加双引号(Chr(34))解决了这个问题。

I always break these long strings into smaller pieces so that I can see what I've typed. 我总是将这些长弦打成小块,这样我才能看到我输入的内容。 The code below uses a little private function to build your pivot string. 下面的代码使用一个小的私有函数来构建您的枢轴字符串。

Public Sub PivotTest()

    Dim jobnumber As String

    jobnumber = Worksheets("Macros test").Cells(1, "A").Value

    Sheets("Macros test").Select
    Range("A2").Select
    ActiveCell.FormulaR1C1 = BuildPivotString(jobnumber)

End Sub

Private Function BuildPivotString(ByRef jobNum As Variant) As String
    Dim retVal As String

    retVal = "=GETPIVOTDATA("
    retVal = retVal & Chr(34) & "Part Number" & Chr(34)
    retVal = retVal & ",' Parts Status '!R8C1,"
    retVal = retVal & Chr(34) & "CHAR_FIELD3" & Chr(34) & ","
    retVal = retVal & Chr(34) & jobNum & Chr(34) & ","
    retVal = retVal & Chr(34) & "MATERIAL STATUS MASTER" & Chr(34) & ","
    retVal = retVal & Chr(34) & "Avail" & Chr(34) & ")"

    BuildPivotString = retVal
End Function

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

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