Is there a way to make Query function and importrange works together? - google-sheets-api

=QUERY('SHIPMENT LIST'!3:4341,"SELECT A WHERE Q='"&E1&"'AND N='"&F1&"'")
I wrote this on a workbook and it works fine but when i try the same thing in different spreadsheet with importrange i got an error
Unable to parse query string for Function QUERY parameter 2: NO_COLUMN: A.
Is my syntax wrong or something?
=QUERY(IMPORTRANGE(B1,"SHIPMENT LIST!3:4341"),"SELECT A WHERE Q='"&C2&"'AND N='"&C4&"'")

Related

Parameters Randomly Being Transferred

I have an Access Database that is reading data from a SharePoint list,
then outputting the data into an excel spreadsheet There are many queries involved, about 40 total.
Here is the format of each query:
Set qdf20 = db.QueryDefs("<Query Name Here>")
qdf20!Param1 = Param
Set rst20 = qdf20.OpenRecordset()
qdf20.Close
rst20.Close
Set qdf20 = Nothing
Set rst20 = Nothing
This is in a 'For' loop, and the 'Param' changes with each iteration of the loop.
The problem: Some queries work, and some do not. The only thing that is changed
is the query name, the record set name and the query def name. The errors are
totally random. The parameters are in an array, and the for the queries that work,
all the parameters are successfully transferred.
Solutions I have tried are using the Parameters Collection Object, and using the SQL commands directly in VBA, but that didn't help. The error I receive is: "Too few parameters, expected 1". I have read the other posts concerning this same error, but they do not solve the problem.
I'm at my wits-end trying to find a solution. Can anyone offer any insight?
It would be very helpful and much appreciated.

How to unlink a query from a structured table AND deleting that query in VBA

I know that since Excel 2016 I can simply delete a query like this:
ActiveWorkbook.Queries("aaa").delete
However this will leave an "orphaned" query connection. So if I refresh the remaining structured table, I will see the error
Query xxx was not found
I know that I can avoid this by first unlinking the query like this:
ActiveSheet.ListObjects("aaa").Unlink
How can I combine these 2 codes into a macro that:
Takes the query name as input (name in the Power Query Editor list)
Remove the link to the structured table
Delete that query
I prefer a method that works in the Queries collection of ActiveWorkbook. Because ListObjects is Sheet-dependent, and sheet name may change. Something like this:
ActiveWorkbook.Queries("aaa").[something].unlink
ActiveWorkbook.Queries("aaa").delete
Regarding ListObject.Unlink method, documentation (https://learn.microsoft.com/en-us/office/vba/api/excel.listobject.unlink) suggests:
Removes the link to a Microsoft SharePoint Foundation site from a list. Returns Nothing.
Doesn't seem relevant, as you don't mention SharePoint. Perhaps the code below is what you want.
Option Explicit
Private Sub DeleteQueryAndConnection()
Dim nameOfQueryToDelete As String
nameOfQueryToDelete = "someQuery"
With ThisWorkbook
.Queries(nameOfQueryToDelete).Delete
.Connections("Query - " & nameOfQueryToDelete).Delete
End With
End Sub
I didn't get the "Query xxx was not found" error after running this code.

Excel 2010 VBA: Invalid procedure call when adding data field to pivot table

I want to create multiple pivot tables by encapsulating the complexity into functions and procedures (I did already manage to create pivots with VBA, but one module for each pivot, meaning lots of repetitive code...)
I managed to create procedures and functions to create the pivot, add a filter, and a row field. But when it comes to adding a data field, I get an invalid procedure call; the strange thing however is that I get that error only when I use variables to pass info: looking at the line prodicung the error, the first line works perfectly fine, whereas I cannot get the second line running (the variables contain the correct values):
pivotName.addDataField pivotName.PivotFields("sdID"), "SD ID number", xlCount 'works fine
pivotName.addDataField pivotName.PivotFields(fieldName), fieldDescription, calcMethod 'produces Invalid procedure call error
As I am running out of ideas, any help would be highly appreciated!
Thank you very much,
Alexander
The reason for the error is due to calcMethod being declared earlier in the code as a string.
The .addDataField method accepts the following parameters:
.AddDataField(Field, Caption, Function)
The parameter Function is of the XLConsolidationFunction enum and therefore should be declared and assigned like below:
Dim calcMethod As XlConsolidationFunction
calcMethod = xlCount
Once declared and assigned as above, you can use it within your method in the following way where fieldName and fieldDescription are both strings:
pivotName.addDataField pivotName.PivotFields(fieldName), fieldDescription, calcMethod
As you can see in the comment from Gareth, the problem was to declare calcMethod (optional function) as string, not as XLConsolidatedFunction.
Thank you once again!

Access Call Macro from Query (Opening a Form) Run-time error '2486': You can't carry out this action at the present time

I have an Access Query that requiers a value to be set in a combo-box within a form in order to work
Criteria: Forms![_SelectCustomer]![CmbSelectCustomer]
So far so good, however, I would like the query to open, read and close this form programatically when it is run using a macro.
I have been following #David-W-Fenton's answer in THIS similar stack overflow question and have come up with the following code:
Public Function rtnSelectCustomer() As Variant
DoCmd.OpenForm "_SelectCustomer", , , , , acDialog
With Forms![_SelectCustomer]
If .Tag <> "Cancel" Then
rtnSelectCustomer = Nz(!CmbSelectCustomer, "*")
Else
rtnSelectCustomer = "*"
End If
End With
Close acForm, "_SelectCustomer"
End Function
I call this function from within the criteria field of the property I want to filter by in the Query:
Like rtnSelectCustomer()
At this point I run into several problems:
The first being, I'm not sure where to place the actual code: I can't seem to create a specific class or module for my query within the "Microsoft Access Class Objects" folder so I have resorted to creating my own module within the Modules folder. (Is this the correct approach?)
The second issue is that when I run the query with the code in the current module I have created I get the following error:
Run-time error '2486':
You can't carry out this action at the present time.
Any advice would be much appreciated
Edit:
I should clarify that after further testing the line that seems to cause the Run-time error is the following:
DoCmd.OpenForm "_SelectCustomer", , , , , acDialog
The function is actually called as replacing the internal code with the following does actually work (although is admittedly useless)
Public Function rtnSelectCustomer() As Variant
rtnSelectCustomer
End Function
Generally, I hate things that are "pre-programmed" by Microsoft, I'd rather do them myself. It seems this is your case as well...
I would do this in 2 steps.
Step1: Show things to the user as if the query was running (without actually running it) and store the values the user picks.
Step2: Use the values to parameterize the query
If your function works well, then simply remember what the user picks and then do:
set qdf = new QueryDef
' set the qdf and add all parameters to it
DoCmd.Execute qdf
for further reference on how QueryDef works I would use this msdn site

what mistake Am I doing?

sheet1.activate
activesheet.range(cells(2,"Q"),cells(40,"K")).select 'here the error occurs
selection.copy
The above code works for somtimes and for sometimes throws an Error that with object variable not set what does that mean ? why the above code is not working and if i reopen the file it works again and sometimes dont. But i dont know the reason why it happens. Am i making any mistake in the syntax, If so please let me know the right way to do it. And is there any more efficient way to do to select a set of range ? other than the above ? Thank you in advance
The cause of this error is usually the omission of a Set keyword, e.g.
Dim w as Worksheet
w = Sheet1 ' throws error 91
' Correct syntax:
' Set w = Sheet1
If you tell us on what line the error occurs, then a more specific answer will be forthcoming. The syntax of your code sample looks fine, but the error may be due to another line in your code, perhaps not shown in your question.
Is there any more "efficient" way to do to select a range? Personally, I don't like the method you show, with strings as the arguments for Cells. Strings are messy, and often lead to mistakes and therefore a loss of work efficiency. Why not:
Sheet1.Range(Cells(2,18),Cells(40,11)).Select
' or
Sheet1.Cells(2,11).Resize(39,8).Select
' or
Sheet1.Range("K2").Resize(39,8).Select
Or, define K2:Q40 as a named range in your sheet, calling it e.g. "myRange", and do this:
Sheet1.Range("myRange").Select ' My personal favourite
But why are you using Select? This is usually not necessary. Just write:
Sheet1.Range("myRange").Copy
But then I could ask, why are you using Copy, which is a messy method in its own right?...