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
Related
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.
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.
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...
I have charts in excel that I copy and pass to an external picture manager with flags by the Shell command to save each chart as a gif. In Excel 2003 it works fine, but in 2016 it fails. I have tried wrapping the string with double quotes even there is no space in it, no luck. However, if I print out the string with debug.print ShComd and input that, then it works in 2016, like:
ShComd="C:\IrfanView\i_view32.exe ... =C:\Test\Sheet1.gif /killmesoftly"
So this fails only in 2016 in row PID = ... with wrong argument
ShComd = i_view_Path & "\i_view32.exe /clippaste /convert=" _
& SavePath & "\" & Sht.Name & ".gif /killmesoftly"
For Each Chrt In Sht.ChartObjects
Chrt.Copy
DoEvents
Sleep (250) 'start up irfanview to avoid "Can't Load"
PID = Shell(ShComd) 'fails here in 2016
Next Chrt
How come?
Billy, I'd first reference the library, Windows Script Host Object Model. Then, use the Run method. And, most important for your concern, separate the multiple commands with " && "
You can see here.
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.