Excel VBA adding table to Pivot Table Data model - excel
Good day,
I'm trying to insert a pivot table from a table but also add it to a data model via VBA.
I've recorded the process manually and also followed the example of this link: https://powerpivotpro.com/2014/07/adding-tables-to-a-model-from-vba-in-excel-2013/
I change the variables for my data path and workbook name that has been set previously in the code. When I run the macro I get the following error on the first line: "Run-Time error 5: Invalid procedure call or argument".
I've never added a pivot table to a data model via VBA before, thus not sure how the code should look (except what I've seen online and mine seem to adhere to the example)
MainWB.Connections.Add2 "WorksheetConnection_" & MainWB.Name & "!Table_SDCdata", "", _
"WORKSHEET;" & DataPath & "\" & MainWB.Name & ", " & MainWB.Name & "!Table_SDCdata", 7, True, False
MainWB.PivotCaches.Create(SourceType:=xlExternal, SourceData:=MainWB.Connections("WorksheetConnection_" & MainWB.Name & "!Table_SDCdata" _
), Version:=6).CreatePivotTable TableDestination:=wsPivotModel.Name & "!R1C1", TableName:="PvtSDCmodel", DefaultVersion:=6
The variables are as follows: (** added to replace for security in this question, both variables are correct and used many times in the code without problems)
MainWB.name: **2791 SDC Inland 12.08.2020.xlsbCOMBINED.xls
DataPath: C:_Store\Simone_d_drive\Desktop*\2019\Macros*\raw data**\2791\2020.08.16
Any help or point in the right direction will be great thank you!
I managed to find my errors:
Updated code:
MainWB.Connections.Add2 "WorksheetConnection_" & MainWB.Name & "!Table_SDCdata", "", _
"WORKSHEET;" & DataPath & "\" & MainWB.Name, MainWB.Name & "!Table_SDCdata", 7, True, False
I had to remove 2 of the quotation marks, one at the end of the connection string and the 2nd one at the beginning of the command text.
Related
How to get a specific statement from a PDF using selenium with VBA
This is a continuity question of the below link. https://stackoverflow.com/a/56649098?noredirect=1 I need to do two things 1. Copy all text from PDF and paste it to the excel 2. Copy multiple lines and run a loop to find the text I need The background of my project - I am opening a webpage with user credentials, after couples of clicks a PDF is opened in the browser. So now I need to get a particular statement from it. The problem is that the PDF is dynamic and the statement I require keeps changing (the line sometimes it is in 6th and sometimes in 9th or 10th and 11th) so the 2 things which I mentioned above will help me I am aware both sounds the same but it is different. Below is the code I used to get a particular statement, but how do I create a loop through all the statement or get multiple statements. Const statext As String = _ "addEventListener('message',function(e){" & _ " if(e.data.type=='getSelectedTextReply'){" & _ " var txt=e.data.selectedText;" & _ " callback(txt && txt.match(/[^\r\n]+/g)[7]);" & _ " }" & _ "});" & _ "plugin.postMessage({type:'initialize'},'*');" & _ "plugin.postMessage({type:'selectAll'},'*');" & _ "plugin.postMessage({type:'getSelectedText'},'*');" Casestatus = bot.ExecuteAsyncScript(statext) I am very new to programming and automation so I may be missing a basic thing. Kindly pardon me.
Expression.Error when dynamically passing in Teradata SQL query (with column aliases) to ODBC query
I have a macro that prompts me for a SQL query (unless it was called by another Sub, in which case it uses the argument that was passed into its optional string parameter as the query) and then executes the query against my Teradata SQL database. It works fine, unless there's a column alias containing a space in the query. Example query: SELECT 2 + 2 AS "Query Result"; Error: Run-time error '1004': [Expression.Error] The name 'Source' wasn't recognized. Make sure it's spelled correctly. The line of code which I believe is the culprit is as follows (my apologies for the readability-- I recorded the macro, modified it just enough to get it to work somewhat dynamically and then haven't touched it since). ActiveWorkbook.Queries.Add Name:=queryName, formula:= _ "let" & Chr(13) & "" & Chr(10) & " Source = Odbc.Query(""dsn=my-server-name"", " & Chr(34) & code & Chr(34) & ")" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & " Source" I assume it has to do with the fact that the example query above has double quotes for the alias, which are confusing the syntax when trying to be interpolated. Any help would be greatly appreciated!
Here's what the string for the formula is set to in this line: ActiveWorkbook.Queries.Add Name:=queryName, formula:=<string here> after all the chr() and concatenation are done: let Source = Odbc.Query("dsn=my-server-name", "<code>") in Source That token <code> is replaced by whatever is in your variable code. So I suspect you are correct in that this formula would need to have it's double quotes escaped fully. In other words this string you are building form Formula is going to be evaluated as code itself, and even in that evaluation it will be passing more code (your SQL) onto the Teradata server to be evaluated there. You are in code inception. VBA code writing powerquery code writing Teradata code. Understanding that and guessing a bit here, I'm thinking your current code variable looks something like: code="SELECT 2 + 2 AS ""Query Result"";" Your double quotes are already escaped for VBA. BUT because you have to survive another round of eval in powerquery you need to escape once again. Instead: code="SELECT 2 + 2 AS """"Query Result"""";" *I think...
currency conversion:number to words excel
I would like to convert the the Kuwait currency which has three decimal places into words in an Excel Document.The formula i used is as follows: =if(or(isBlank($E$89),not(isNumber($E$89)),$E$89>=power(10,15)),ifError(1/0),trim(arrayFormula(concatenate(if(trunc(mod($E$89,power(10,15,12,9,6,3}))/power(10,{12,9,6,3,0}))<100,"",switch(int(trunc(mod($E$89,power(10,{15,12,9,6,3}))/power(10,{12,9,6,3,0}))/100),1," one",2," two",3," three",4," four",5," five",6," six",7," seven",8," eight",9," nine") & " hundred") & if(mod(trunc(mod($E$89,power(10,{15,12,9,6,3}))/power(10,{12,9,6,3,0})),100)<>0,if(trunc(mod($E$89,power(10,{15,12,9,6,3}))/power(10,{12,9,6,3,0}))>100," and",if($E$89>power(10,{15,12,9,6,3}),switch({1,2,3,4,5},5," and"),"")),"") & if(mod(trunc(mod($E$89,power(10,{15,12,9,6,3}))/power(10,12,9,6,3,0})),100)=0,"",if(mod(trunc(mod($E$89,power(10,{15,12,9,6,3}))/power(10,{12,9,6,3,0})),100)<20,IFERROR(switch(mod(trunc(mod($E$89,power(10,{15,12,9,6,3}))/power(10,{12,9,6,3,0})),100),1," one",2," two",3," three",4," four",5," five",6," six",7," seven",8," eight",9," nine",10," ten",11," eleven",12," twelve",13," thirteen",14," fourteen",15," fifteen",16," sixteen",17," seventeen",18," eighteen",19," nineteen")),IFERROR(switch(int(mod(trunc(mod($E$89,power(10,{15,12,9,6,3}))/power(10,{12,9,6,3,0})),100)/10),2," twenty",3," thirty",4," forty",5," fifty",6," sixty",7," seventy",8," eighty",9," ninety")) & if(mod(mod(trunc(mod($E$89,power(10,{15,12,9,6,3}))/power(10,12,9,6,3,0})),100),10)=0,"","-" & IFERROR(switch(mod(mod(trunc(mod($E$89,power(10,{15,12,9,6,3}))/power(10,{12,9,6,3,0})),100),10),1,"one",2,"two",3,"three",4,"four",5,"five",6,"six",7,"seven",8,"eight",9,"nine"))))) & if(trunc(mod($E$89,power(10,{15,12,9,6,3}))/power(10,{12,9,6,3,0}))=0,"",IFERROR(switch({1,2,3,4,5},1," trillion",2," billion",3," million",4," thousand"))))) & if($E$89>=2," Dinar only",if($E$89>=1," Dinar only","")) & if((round($E$89-trunc($E$89),3)*1000=0)+($E$89<1),""," and") & switch(trunc(round($E$89-trunc($E$89),3)*1000,-2)/100,1," one-hundred",2," two",3,-hundred" three-hundred",4," four-hundred",5," five-hundred",6," six-hundred",7," seven-hundred",8," eight-hundred",9," nine-hundred"& if(round($E$89-trunc($E$89),3)*1000-trunc(round($E$89-trunc($E$89),3)*1000,-2)<20,switch(round($E$89-trunc($E$89),3)*1000-trunc(round($E$89-trunc($E$89),3)*1000,-1),0,"",1," one",2," two",3," three",4," four",5," five",6," six",7," seven",8," eight",9," nine",10," ten",11," eleven",12," twelve",13," thirteen",14," fourteen",15," fifteen",16," sixteen",17," seventeen",18," eighteen",19," nineteen"),switch((round($E$89-trunc($E$89),3)*1000-trunc(round($E$89-trunc($E$89),3)*1000,-2))/10,2," twenty",3," thirty",4," forty",5," fifty",6," sixty",7," seventy",8," eighty",9," ninety") & if(mod(round($E$89-trunc($E$89),3)*1000,10)=0,"","-" & switch(round($E$89-trunc($E$89),3)*1000-trunc(round($E$89-trunc($E$89),3)*1000,-1),0"",1,"one",2,"two",3,"three",4,"four",5,"five",6,"six",7,"seven",8,"eight",9,"nine"))) & " fils only"))) The error shown is Formula parse error For a given input: 111.250 i would expect to get the output as : One hundred and eleven Dinar and two hundred fifty fils only.
Try looking for JavaScript solutions to use then as custom function. I've found is this code, provided by #Vilas Shetkar: https://stackoverflow.com/a/37409158/5372400 I've tested this code and it works in sheets, so you may adopt this or other similar solutions to your needs.
VBA SaveAs Method
After browsing forums for well over an hour, I can't seem to figure out why my save as code isn't working. My goal is to save a new copy of the workbook under a different file type. (The current file type is .csv.) I'm not trying to save in a new or different location, the current directory is where I want it to save to. I've tried more variations of the following code than I can remember, so I'll just post my current syntax: CurrentDir = CurDir() dateVal = Date ActiveWorkbook.SaveAs Filename:="" & CurrentDir & "ALS Week of " & dateVal - 4 & ".xlsx", FileFormat:=51 I've looked at examples of how to open a dialog box wherein the user inputs information in order to save. But I'm hoping for a hands-off approach. If anyone can see where my error lies, please let me know! EDIT: The error that I get is "Method 'SaveAs' of object '_Workbook' failed
Your date has illegal characters in it. Format your date with dashes and not slashes and this won't happen. The following reserved characters: < (less than) > (greater than) : (colon) " (double quote) / (forward slash) \ (backslash) | (vertical bar or pipe) ? (question mark) * (asterisk)
You forgot a backslash: ActiveWorkbook.SaveAs Filename:= CurDir() & "\ALS Week of " & (Date - 4) & ".xlsx", FileFormat:=51
Downloading excel file name based on date Macro
I'm trying to write a macro that downloads data into a file, where the file name contains yesterdays date. "TEXT;http://www.mydomainname.co.uk/price_spiders_competitors_prices_gb/GBPS AEG " & Format(Today() - 1, "yyyy-mm-dd") & " higher_than.csv" _ , Destination:=Range("$A$1")) This isn't working "TEXT;http://www.mydomainname.co.uk/price_spiders_competitors_prices_gb/GBPS AEG 2013-02-06 higher_than.csv" _ , Destination:=Range("$A$1")) This does! Any ideas, I have a feeling the dashes are causing an issue but it also doesn't seem to like my Today -1! Thanks for any help
Try function 'Now()' instead of 'Today()' to get the current date and time. As far as i know, excel doesn't have the function 'today()' in VBA. You can use 'today()' in a formula but not in VBA.