SendTQ2Excel function naming sheet

Eljefegeneo

Still trying to learn
Local time
Today, 04:26
Joined
Jan 10, 2011
Messages
902
I know this must have been beaten to death many times, but I cannot figure out how to name the Excel Workbook using the SendTQ2Excel function.
It comes out exactly how I want it; formatting is just great, but I cannot figure out the last part:
Code:
  [FONT=&quot]Public Function SendTQ2Excel(strTQName As String, Optional strSheetName As String)[/FONT]
Using SendTQ2Excel (myQry) gives me the desired results but names the workbook Book1.

I have tried the following with the results:
SendTQ2Excel (myQry, myfile) give me "Compile Error, Expected: ="
SendTQ2Excel myQry, myfile gives me "Compile Error, Syntax Error"
SendTQ2Excel (myQry, "ABCDE") gives me "Compile Error, Expected: ="
SendTQ2Excel (myQry, "ABCDE.XLsx") gives me "Compile Error, Expected: ="
SendTQ2Excel myQry, "ABCDE.xlsx" gives me "By Ref Argument type Mismatch"

I have researched this extensively and have tried just about all the variations to name the Workbook to no avail. Is there something else I should be doing?

[FONT=&quot]The function for those not familiar with it is on:[/FONT]
http://btabdevelopment.com/export-a-table-or-query-to-excel/
 
Use the TransferSpreadsheet command.
The sheet name is the last parameter.
 
Both arguments are strings so must have double quotes around each of them.

Parentheses are required around the arguments of a function if it is used with a return value.

Code:
x = myfunction("y", "z")
If you use the function like a sub, discarding the return value, the sub syntax must be used. Hence no parentheses.

Code:
myfunction "y", "z"
 
The Functions needs the name of your query and the name of the Worksheet within the Workbook, not the Workbook name, i.e.

Code:
Call SendTQ2Excel("MyQuery", "WorksheetName")
 
Last edited:
Code:
SendTQ2Excel ("MyQuery", "WorksheetName")

That syntax is incorrect. When discarding the return value, use either:

Call SendTQ2Excel("MyQuery", "WorksheetName")

or

SendTQ2Excel "MyQuery", "WorksheetName"
 
I suppose I misread the function and it cannot name the Excel Workbook, only the Worksheet. And then I suppose that naming it means that I have saved it.
The bigger question is how do I get the result named as I want, i.e. "TransferBilling.xlsx" when it opens? Or rather, it would seem that I need to open it, save it (with the name I want) formatted the way I want it. For this I will have to do additional research.



Thanks to all.
 
Okay, so after you drop the data on to Sheet1 you want to save the Workbook. Something like...

Code:
xlWBk.SaveAs "TheDrive\YourFolder\NameOfYourFile.xlsx", 51

At the end should work.
 
Thank you. I was a little confused at first as to where to put this last bit of code, but finally it occurred to me that it should go in the module and Voila!

Much appreciated.
 
Yeah, I probably should have mentioned the Module... glad it worked for you!
 

Users who are viewing this thread

Back
Top Bottom