SQL query to link data in 2 Excel sheets - sql

I want to link 2 excel tables on different excel sheets. I do this for years without problems. Both excel sheets contain a table with the same reference number so i can link them and job done.
The Excel sheet i got this time from the vendor has little different reference numbers. They all have a character in front of the reference numbers.
Sheet 1: Reference number: 12455630098
Sheet 2: Reference number: F12455630098
The problem here is that the data is a little different now, so the link can't be made with the code i used previously.
SELECT `tab1$`.Référencenumber, `tab1$`.Price, `tab2$`.PARTNO
FROM C:\xxxxxxxxx.file.xlsx
WHERE `tab1$`.Référencenumber = `tab2$`.PARTNO
I tried to experiment with the wildcard character to get the job done, but without succes.
This is my SQL code so far.
SELECT `tab1$`.Référencenumber, `tab1$`.Price, `tab2$`.PARTNO
FROM C:\xxxxxxxxx.file.xlsx
WHERE `tab1$`.Référencenumber LIKE '%' + `tab2$`.PARTNO
Whats wrong here?

Why not
SELECT `tab1$`.Référencenumber, `tab1$`.Price, `tab2$`.PARTNO
FROM C:\xxxxxxxxx.file.xlsx
WHERE `tab1$`.Référencenumber = substring(`tab2$`.PARTNO,2,11)

The easiest way is to run a replace command.
Press CTRL+H and when the replace dialog opens search for letter F and replace it with nothing.
Ok, in your previous question you did not made that specific, didn't use SQL in MS producst for a long time, but if I'm right than you there isn't substring function in MS office, instead you have LEFT RIGHT and MID so you should try something like:
SELECT `tab1$`.Référencenumber, `tab1$`.Price, `tab2$`.PARTNO
FROM C:\xxxxxxxxx.file.xlsx
WHERE `tab1$`.Référencenumber = MID(`tab2$`.PARTNO,2,LEN(`tab2$`.PARTNO)-1)
Not 100% sure about the keywords...

Related

How do I get more information from an additional column if the first one does not have it?

I would just like to say that I'm very new to VBA and more complicated formulas so all the help will be appreciated! Thank you!
To clarify a bit more with the title. I currently have a Macro that is reading a formula to give me information from another worksheet. I'll give an example of the formula that is working:
=IF(A2 = ""No Specific Program"", A2,IF(F2 = """",""No PIN"",IFERROR(VLOOKUP(CONCATENATE(A2,F2),....!$C:$I,4,FALSE),""NO DATA"")))
This is the original formula that I'm using to get the information, column A is my Parts owned by Program, And my column B is the actual Program. So when I run the Macro it does give me most of the information, but when it runs into "No Specific Program" even when there is something for Column B showing what program the part is in, it will give me No specific Program.
Also for reference the F2 it is Concatenating is a PIN number which will help determine who owns the part.
I've been stumped on this trying to get the code to work and I've tried place THEN and ELSE within the statement and it just gives a FALSE statement
EDIT:
The Code above works, it's when I use this version of the Code:
=IF(A2 = ""No Specific Program"",THEN,IF(F2 = """",""No PIN"",IFERROR(VLOOKUP(CONCATENATE(A2,F2),'.....'!$C:$I,4,FALSE),""NO DATA"",Else,IF(A2 = ""No Specific Program"",THEN,IF(F2 = """",""No PIN"",IFERROR(VLOOKUP(CONCATENATE(A2,F2),'.....'!$C:$I,4,FALSE),""NO DATA"")))))
I get the False or errors when I try different variations. Here is an example of the columns. Column A is where I have the original formula reading from, but then it says No Specific Program, while Column B shows the Program. So I'm trying to get the formula to read that as well as column A to capture all the information I need :
Columns Example
EDIT:
It starts breaking after the ELSE statement.
Edit:
=IF(A2 = ""No Specific Program"",
IF(F2 = """",""No PIN"",IFERROR(VLOOKUP(CONCATENATE(B2,F2),'\NW\Data\TechIntegration\Sustaining
Team\Data
Mining\DataMining[GAD_PIN_TABLE.xlsx]Sheet1'!$C:$I,5,FALSE),
""NO GAD
DATA"",
IF(F2 = """",""No
PIN"",IFERROR(VLOOKUP(CONCATENATE(A2,F2),'\NW\Data\TechIntegration\Sustaining
Team\Data
Mining\DataMining[GAD_PIN_TABLE.xlsx]Sheet1'!$C:$I,5,FALSE),""NO GAD
DATA"")))))
Just trying to make it easier to see the formula.
I'd split this down to make it simpler to follow, use a holding cell then refer to that in your top formula (The one you know works already)
Stick this in another column, say Z for example, then everywhere you refer to A2 in your working formula, change it to Z2
=IF(A2=""No Specific Program"",IF(B2=""No Specific Program"",""No Specific Program"",B2),A2)
This will only give you "No specific program" if both a2 and b2 contain "No specific program" which I think is what you're after. In your second example in the columns example link, it will return NG

Today function equivalent in VBA in combination with countifs

I am having some problem with using a countifs formula in Excel / VBA. I have got the formula working perfect in Excel but ideally I want to use this in VBA with my form. Here is the formula in Excel which works a treat:
=COUNTIFS(Sheet1!A:A,"Place",Sheet1!K:K,"<"&TODAY())
will count the names places that are now in the past
=COUNTIFS(Sheet1!A:A,"place",Sheet1!K:K,">"&TODAY())
will count the names places that are current
I have five different Places in column A and hundreds of different dates in column K. The above formulas work well in Excel and return the correct values. I have spent hours trying to get this to work in VBA with my userform but keep getting various errors. The first part is not the problem but as soon as I get to the &today function it falls apart. From what I can see the &today function is not available in VBA and the &Date seems to be the recommendation. I have tried this but still get no where. I'm missing a trick (or several) here and I would really like to get this working in VBA rather than using the current formulas in Excel. The returned results are then displayed in textboxes on my form.
All ideas and feedback much welcome!
Second edit
================================
Thanks for the quick replies! Here is the actual code I am playing about with in VBA
'Count events by area'
Dim ListLondon As Long
ListLondon = .CountIf(Range("a1:a1998"), "London"), ("Sheet1!K1:K1998"), "<" & Date)
End With
Me.TextBox1 = ListLondon
I know the second part of the count if is all wrong regards the date - that's how I've left it for now. I am really hoping to use the current layout and a working Date / Today code at the end. Please show me what I've done wrong here!
====
oops - can see a mistake already - but the initial problem remains around the date issue. I should of used countifs as using multiple criteria.
You have to read the values of the cells to your VBA code. I recommend you to use Excel.Range object to do that. It can interpret the range like the edit line of the Excel, something like
Dim foo as Excel.Range
set foo = yourworksheet.Range("A1:B3")
Read the Date type data into VBA Date type variable by iterating through the cells.
Examine relation between the read data and the current date. Current date can be read by using the DateTime.Now function.
Increment a variable based on a decision

How to get defined table name in Excel?

I have an excel file with several sheets with assigned to them alliances. One of value on one sheet is calculating by using code below:
VLOOKUP(D10; lst_table_col; 4;00);
It doesn't matter that VLOOKUP function do, that matters is what exactly sheet behind lst_table_col. The problem is that this excel file doesn't contain any lst_table_col sheet. I'm looking for any solution: by just mouse, vba - whatever.
I believe my question is very easy, but this situation confuse me.
You must have a table called lst_table_col
Because if it was a direct reference to an Excel Range, you would see something like this in the formula :
SheetName!R1C1:R10C4
or
A1:D10
so check in Formula Tabs -> Name Manager to find it! ;)
Here is the link for the tutorial provided by #Rocketq : https://support.office.com/en-ca/article/Define-and-use-names-in-formulas-4d0f13ac-53b7-422e-afd2-abd7ff379c64#bmmanage_names_by_using_the_name_manage

MS ACCESS SQL query Excel sheet

SELECT f.card_serial_num, count(1) AS CardRxCnt
FROM [Excel 8.0;HDR=Yes;Database=C:\Users\Mike\Desktop\er.xls].[er$] AS f
WHERE f.location_name not like 'PREPACK'
and f.card_type not in ('PRN','sequential')
and (f.card_due_date = #9/15/2014# or f.card_due_date = #10/1/2014#)
GROUP BY f.card_serial_num
HAVING CardRxCnt >2
I have a problem with HAVING CardRxCnt >2. If I take out, I get my query returned.
But if I have it in, it somehow prompts for an input and so when I just put 1 it
returns nothing. Actually without CardRxCnt, sometimes the query prompts
for an input at which I enter 1 and the query executes. Yet other times it would just go through. So my question is two part:
what could be causing this random behavior of "asking"? I know to prompt for a user input, I have to surround a value with []. Could it be the Excel part?
CardRxCnt (in SELECT and HAVING): I don't see what's wrong with it but when I add this, query does not work.
Please help. And I can't do VBA/macro here so if you're gonna say why don't you query with VBA it's not a solution for me.

Double quotes from excel query when copy/paste from Excel into SQL Server 2012

I have a query Excel in each of my rows, about 2000. When I copy paste all queries into SQL Server roughly 100 rows are showing a error. Might not even be an error but the query will paste into SQL Server with double quotes in front of entire query.
I have error checking in place so it has something to do with the data in my Excel sheet. My query pasted in SQL Server for instance:
"insert into
I cannot paste the entire query or any data, sorry.
Thank you for looking at this
Actually a lot easier then all of the posts here,
in another column do the following formula:
=CLEAN(MyCell)
This occurs when a line has an embedded carriage return, it may happen in other situations as well. I just do a find/replace for double quotes in the resulting query, but you could also first paste it into Word then copy from there and it will paste without the quotes.
Alternatively, if it's just from embedded returns you could wrap your formula in a pair of SUBSTITUTE() functions to remove CHAR(13) and CHAR(10) if formula was in A1:
=SUBSTITUTE(SUBSTITUTE(A1,CHAR(13),""),CHAR(10),"")
When you are typing your SQL query in the Excel cell never hit Enter, just keep typing all in a single line regardless of how long it is.
If you have queries already entered, double-click on the cell to go to the edit mode and start removing any new lines you have already entered.
The simplest way is to copy from the Formula Bar instead of from the cell.
When you have multiple lines in the cell, Excel will fill the clipboard with a CSV-representation of the cell. That means: double quotes added around, and the double quotes inside the string are escaped with yet another double quote.
Not: select the cell, Ctrl-C
But instead: select the cell; in the formula bar, highlight the complete text (cursor at start, press shift, press arrow-down until the last character is highlighted), Ctrl-C.
and paste in the target application (Notepad, or browser-entry form, or...)
An easier way to select all the text from inside the formula bar would be nice...
Paste the MS Excel content into MS Word with "Keep Source Formatting" pasting option and then copy and paste it to SQL/XML.
I have found that a tab in an excel file will also cause this behavior.
STEPS TO CREATE PROBLEM CITED BY OP
Find a table create script where all the fields are tabbed in 1 tab
copy that to excel Do some sql generation macro in a second column
(Col E, because A-D were consumed by the text to col operation ) Copy
Col E Paste into Visual Studio
You just reproduced the problem op has.
FIX
Go
to the table create script Hightlight all the columns
SHIFT-TAB until they are all at column 0
Paste that list of fields back into Excel.
Redo the TEXT TO COL operation
(the macro should still be good, check it to be sure)
Recopy the COL E to Visual
Studio
You should have just solved the problem.
I have to say that "Spacez" give the best answer. he give the road to find the real answer.
Sub CopyCellContents()
'create a reference in the VBE to Microsft Forms 2.0 Lib
' do this by (in VBA editor) clicking tools - > references and then ticking
"Microsoft Forms 2.0 Library"
Dim objData As New DataObject
Dim strTemp As String
strTemp = ActiveCell.Value
objData.SetText (strTemp)
objData.PutInClipboard
End Sub
Although this question is quite old, I still found this link when googling. So I think it should be good to leave the thread I've found and works good for me.
Leave out quotes when copying from cell