Using Different Parameters in Same excel Workbook for SQL - sql

I have an excel Workbook with 2 sheets.
In each sheet I have a Query that extracts data from an external Server through SQLserver.
in Sheet1, Cell A1, I have the value 1000
and starting from Cell B2 I need the Query to get all the Students that have score more than 1000
I write the code in Excel in Data > Connection Properties > Definition > Command text
My code goes as follows:
SELECT Results.ID, Results.Score
FROM Results
Where Results.Score > ?
When I press OK, I am prompted to select a cell to be used as a parameter instead of the '?' Character
and then I get all Students ID & Score of Students with score more than 1000
and when I change the Cell A1 in Sheet1
Now in Sheet2, Cell A1, I have the Value 5
and starting from Cell B2 I need the Query to get all students in Grade 5
My code goes as follows:
SELECT Students.ID, Students.Grade
FROM Students
WHERE Students.Grade = ?
and When I press ok, the query runs without asking me to assign cell to hold the '?' Parameter , and it uses the parameter from the first query
How can I tell excel that I need a different parameter for each Query
Thanks in advance

Well, I searched a lot for anyone who have the same problem as me and I could't find So I thought it must be something I am doing to cause the problem
To facilitate creating the second query, I used to make a replicate of the 1st sheet and change the SQL command of the 2nd Query in the 2nd Sheet, which I found causes the replication of the parameter used
So the solution is simply create new query for each new Sheet

Related

How to transform excel index match to Access - joining 4 tables

I am new to Access and I would like to ask you for help. I have data in Excel (about 50 thousand rows). On first sheet, there are my codes (case sensitive) and on other sheets(2 3 4) I have data that I "vlookup" to first sheet. The unique code is on each sheet but on sheet1 there is more codes - not all codes from sheet1 have additional info from other sheets.
Excel isn't case sensitive so I do it with combination of index and match. But 50 thousand rows is too much for Excel and I need to put it into MS Access.
On sheet1 there is unique code that is randomly generated by another program and it is combination of numbers and small and big letters. I need to display all of them and display also additional information from other sheets.
So I want to display something like this for 50 thousand rows.
Code(from sheet1) | FirstName(from sheet2) | LastName(from sheet3)| Adress(from sheet4)
5kj7tfR |Jane | White | London 3
Here are my data:
The problem is that if for example the code on sheet 4 is missing (I don't have address) then Access shows me just code from sheet1 and additional info like first name is missing even if it exists on sheet2.
I tried this but when something is blank in Excel Access doesn´t show me any data from other sheets. Let's say on sheet1 are case sensitive codes, on sheet2 first names, on sheet 3 last name and on sheet4 address. I need Access to show me all codes and info from all other sheets.
select sheet1.code, Tab.FirstName, Tab.LastName, Tab.Adress
from Sheet1
left join
(
select sheet1.code, sheet2.FirstName, sheet3.LastName, sheet4.Adress
from ((sheet1
left join sheet2 on (sheet2.code=sheet1.code) and (StrComp(sheet1.code,sheet2.code,0)=0))
left join sheet3 on (sheet3.code=sheet1.code) and (StrComp(sheet1.code,sheet3.code,0)=0))
left join sheet4 on (sheet4.code=sheet1.code) and (StrComp(sheet1.code,sheet4.code,0)=0)
) as Tab
on Sheet1.ID = Tab.ID --...these IDs made Access while importing Excel to Access.
How can I show all codes with all data even if some cell is empty?
Thank you!
My research indicates cannot force case sensitivity on linking fields in Access. StrComp() is used to apply filter criteria and force case-INsensitive matches between two tables to be rejected. I can get a UNION to show all case-sensitive matches but cannot group on code values because grouping is not case sensitive. Only option I can see involves VBA converting text to HEX and using that value to join, sort, group.
Place this function in a general module (found at Case-Sensitive Sort).
Function StrToHex(S As Variant) As Variant
'
' Converts a string to a series of hexadecimal digits.
' For example, StrToHex(Chr(9) & "A~") returns 09417E.
'
Dim Temp As String, I As Integer
If VarType(S) <> 8 Then
StrToHex = S
Else
Temp = ""
For I = 1 To Len(S)
Temp = Temp & Format(Hex(Asc(Mid(S, I, 1))), "00")
Next I
StrToHex = Temp
End If
End Function
Calling that function in query returns desired output. Copy/paste or type into SQLView of query builder. I initially built query with joins on code fields then switched to SQLView and added in function calls. DO NOT OPEN QUERY IN DESIGN VIEW. Access query designer cannot resolve these joins. Would have to join nested subqueries to enable Design View.
Note I have spelled address with two d's, modify as you see fit.
SELECT Sheet1.code, [time], [type of error], FirstName, LastName, age, address
FROM Sheet4 RIGHT JOIN (Sheet3 RIGHT JOIN (Sheet2 RIGHT JOIN Sheet1
ON StrToHex([Sheet2].[code]) = StrToHex([Sheet1].[code]))
ON StrToHex([Sheet3].[code]) = StrToHex([Sheet1].[code]))
ON StrToHex([Sheet4].[code]) = StrToHex([Sheet1].[code]);

VBA macro for excel. Mark row if cells in column A are the same but different in column C

I do not have any code yet as I dont know VBA that much or at all.
I got excell sheet with 4 columns. Column A is main group and C is subgroups within groups from column A.
i need to mark somehow rows where within same JobID, WFID is the same and where WFID is different within same JobID.
JobID TaskID WFID
39822 913914 Complete
39822 913915 no complete
37941 905439 Complete
37941 905440 Complete
Would you be able to help pleasE?
You do not really need macros to identify your rows.
Simply put this formula in a new column
=COUNTIFS($A$1:$A$8,A1,$C$1:$C$8,"<>"&C1)
Wherever there are different values in col C for same value of col A it will throw a 1 else 0
If you want to highlight, I recommend using conditional formatting based on the value of your new column with (0/1) values.
Hope this answers your query.

Excel find row of given string in range and copy to another tab

I have a column of data that looks like this:
The above is in the 'NEW PLANNER WORKBOOK' tab. Tabs are shown below:
I'm trying to find a way to get the values for the rows where "Switched" occur (in this case 13 and 16 but won't always be the same and could be more or less than 2 instances) and take the information from columns B, F, and U associated to those rows and put them into the "Switched Info" tab.
For example in the above, row 13 has the following in columns B, F, and U:
So I would like to move values 'BBMK01150', '201T1ZFPMBHFLFEDC0PMBHFLFETMD', and 'WCA' to the 'Switched Info' tab which looks like this:
The end result looking something like this:
Is there a way to do this only using formulas?
Thanks in advance for the help!
A pivot table would be best. But if a formula solution is wanted then:
These are all array formulas and must be confirmed with Ctrl-Shift-Enter.
In A4 put:
=IFERROR(INDEX('NEW PLANNER WORKBOOK'!$B:$B,MATCH(SMALL(IF('NEW PLANNER WORKBOOK'!$A:$A="Switched",'NEW PLANNER WORKBOOK'!$B:$B),COUNTA($A$3:$A3)),'NEW PLANNER WORKBOOK'!$B:$B,0)),"")
In B4:
=IFERROR(INDEX('NEW PLANNER WORKBOOK'!$F:$F,MATCH(SMALL(IF('NEW PLANNER WORKBOOK'!$A:$A="Switched",'NEW PLANNER WORKBOOK'!$B:$B),COUNTA($A$3:$A3)),'NEW PLANNER WORKBOOK'!$B:$B,0)),"")
In C4:
=IFERROR(INDEX('NEW PLANNER WORKBOOK'!$U:$U,MATCH(SMALL(IF('NEW PLANNER WORKBOOK'!$A:$A="Switched",'NEW PLANNER WORKBOOK'!$B:$B),COUNTA($A$3:$A3)),'NEW PLANNER WORKBOOK'!$B:$B,0)),"")
Again Each needs to be confirmed with Ctrl-Shift-Enter. Then they can be copied down for as many rows as desired.
One Note. This will be slow. To speed up, Change all the full column references to the specific area, ie $A:$A to $A$6:$A$1000, if 1000 was the last row of data.
You can definitely do this with code, but I think you could just use pivot tables if you didn't want to use a macro. Use the table in the New Planner Workbook worksheet as your data source and then make a pivot table on a new sheet and filter the data.

Retain Specific values in Excel sheet and remove others and get count of how many times repeated vbscript

I want to remove all in excel data except for rows 'containing' the name "abc".I am trying to update the excel sheet and then get the count of how many times each duplicate has repeated.The output should be another excel sheet in which i have the condensed duplicate values along with the count i got previously.
What I have done: To get the count of duplicate value I used this:
=COUNTIF($A$1:$A$8, A1
But how can i remove the other values and put the remaining in a new Excel Sheet.
Add a "RowNo" column with the row number or any other Row Id. Next use Microsoft Query in the output Excel file e.g.:
SELECT S1.Val, COUNT(S1.Val) FROM `C:\Book1.xls`.`Sheet1$` as S1
INNER JOIN `C:\Book1.xls`.`Sheet1$` as S2
WHERE S1.Val = S2.Val and S1.RowNo > S2.RowNo
GROUP BY S1.Val
Additionally to remove data not containing e.g. 'abc' you can add another condition to the WHERE clause: S1.Val NOT LIKE "abc". See below:
SELECT S1.Val, COUNT(S1.Val) FROM `C:\Book1.xls`.`Sheet1$` as S1
INNER JOIN `C:\Book1.xls`.`Sheet1$` as S2
WHERE S1.Val = S2.Val and S1.RowNo > S2.RowNo AND S1.Val NOT LIKE "*abc*"
GROUP BY S1.Val
Feel free to test different SQL with my AddIn: link.
To set this up via VBscript:
Step 1: Create an Excel with this Microsoft Query
Step 2: Create a VBscript that will refresh the Microsoft Query in the Excel file (See an example here of how to connect to an Excel file from VBscript link).

Cross worksheet Sql query taking time?

I have around 10 thousand records(rows) in one sheet A and around a thousand in sheet B. Both have 5 columns. I need to filter sheet A using column filters and then find everything which is not on sheet B using the ID in both the sheets. Eg: Sheet A has 10 thousand , apply filter and it comes to 1200. Now select the records which are not in Sheet B. I have written a query for it.
rs.Open "SELECT * FROM [A$] WHERE ([Class]<>'Other' OR...) AND ([Sub Class]='Good' OR...) AND [ID] NOT IN (SELECT [B$].ID FROM [B$])"
Query runs extremely slow and I don't know if I will even get any answer waiting hours, may be it is wrong too. I checked and found that if I exclude the last AND , the query executes in a second. Any help is appreciated. Thanks.
Why would you use a SQL query within an Excel workbook if you're not querying a database? Wouldn't VLOOKUP be much simpler?