Export Excel Function as a SQL request result - sql

I am facing a weird behavior while exporting an output coming from oracle SQL. I am launching the below query:
select '=sum(1,2)' from dual
Then I right click on the output data and I export it to excel.
I am getting the following result in excel:
The function is presented as a string (the result in the cell is not "3", but rather "=sum(1,2)".
In order to fix this I had to click on the function then click on the Enter button on the keyboard.
excel file picture
Is there a way to force excel to directly understand that the string is a function ?

(I'm assuing you are on SQL Developer here)
There might be other ways, but a little trick I've used in the past is rather tahn Right Click => Export, simply do: Select-All, Copy. Then just Paste into Excel and that seems to preserve the formula definition.
A little animated GIF showing that here
https://imgur.com/a/SJDFjuz

Using the following Macro will fix it. Its a work around to this behavior
for each c in activesheet.usedrange.columns("AD").cells: c.formula=c.formula: next

Related

VBA Timestamp or other tab not working

I have a macro that I am running to paste data into an excelsheet. When I paste the data I want to add the time I did that so I can see when I last updated the data.
I found an easy solution for that:
[F2] = Now
looks simple and works like a charm. But then there's a problem. This only works if the cell I want the data in is on the same tab as I am running the macro from, but i want that timestamp on another tab. I tried to change the "Overview"tab and back to "Orders" but it doesnt work:
Sheets("Overview").Select
[F2] = Now
Sheets("Orders").Select
It just gives a 400 error with no explanation. Does anybody know the solution?
Ignore the Select completely, it is not needed. Probably the error is somewhere in the logic of the other code. However, this should work:
Worksheets("Overview").Range("F2") = Now
or with different syntactic sugar:
Worksheets("Overview").[F2] = Now
How to avoid using Select in Excel VBA

Run-Time Error 1004: MS Excel is refreshing some data. Please try again later using Query

I have a dynamic query in a sheet that uses a parameter in cell C1. So, In column A there are codes, and in column B are those code descriptions and based on the value in cell C1, the query will refresh to display codes related to the value in C1. When the value in C1 changes, I get a "Run-Time Error 1004: MS Excel is refreshing some data. Please try again later", every time. When I select "End" it goes away and refreshes the query successfully, after a couple seconds, Does anyone know the reason behind this? Can I get rid of it?
There is a search feature. when the user clicks "Search" to search for the codes they're looking for, the search button is assigned a macro that essentially just navigates to the sheet which displays the codes. However, in that macro, I added this bit of code:
Sheets("department_lookup").Cells(1, 3).Value = Sheets("lookup").Cells(2, 2).Value
With Sheets("department_lookup") .Range(.Cells(1, 1), .Cells(LR, LC)).AutoFilter field:=3, Criteria1:=.Cells(1, 3).Value, VisibleDropDown:=True End With
I found this occurs when you are using a combination of Excel native queries and VBA DB code. For me, I found I couldn't access the database using an OLEDBConnection when Excel was refreshing using it's native refresh function.
SOLUTION:
I had to turn BackgroundQuery = False on the native queries.
(This can be done from the Query properties using the GUI)
EXPLANATION:
BackgroundQuery allows you to use the spreadsheet while it is refreshing data. This is okay for a user who wants to use a spreadsheet but not for a VBA macro that wants to talk to that same database. When set to false it causes Excel to hang during a refresh. This stops your operations from colliding.
If this isn't your exact scenario look for similar collisions where 2 queries are stepping upon eachother.
For people like me who knows a little bit about ODBC, from Excel, go to Data tab. Click on Queries & Connections. Then, right-click each connection tile, click Properties. Under usage, untick Enable background refresh. That should do the trick. At least, for me, that worked. :D

Selenium VBA code to paste data in browser

I am trying to stimulate the control+v option in a text box in chrome using selenium vba wrapper. I tried using the context click but that function seems to click at a random position based on the cursor position. I tried using the send key function but i am not getting the desired result.
selenium.SendKeys (key.Control & "v")
Please any advise or leads would be much appreciated.
You want to target the text box before using sendkeys. Let's say, hypothetically, that there is an ID called textbox1 for identifying the element then:
driver.FindElementById("textbox1").SendKeys("yourString")
Or
driver.FindElementById("textbox1").SendKeys Keys.Control, "v"
The latter assuming info is already in the clipboard with, for example, driver.SetClipBoard "yourString"
I think you'll be better off using a variable to hold your data rather than the clipboard. Then you can use sendkeys to type it in:
myData= "your data here"
selenium.sendkeys myData
hth

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

Import Data Wizard Does Not Like Data Type I Choose For A Column

Does anybody else have this same problem, when you import data from Excel file to MSSQL Server 2005, if some column contains mostly numeric data, but even if you set the column type to varchar, the wizard fails to import those fields that fail to parse as numbers?
Try this (Note: These instructions are based on Excel 2007)...
The following steps should force Excel to treat the column as text:
Open your spreadsheet with Excel.
Select the whole column that contains your "mostly numeric data" by clicking on the column header.
Click on the Data tab on the ribbon menu.
Select Text to Columns. This will bring up the Convert Text to Columns Wizard.
-On Step 1: Click Next
-On Step 2: Click Next
-On Step 3: Select Text and click Finish
Save your Excel sheet.
Retry the import using the SQL Server 2005 Import Data Wizard.
You need to edit the connection string that SSIS uses. Add this to the end of the string "IMEX=1;" (No quotes) That tells SSIS/jet to not try and figure out what the data types are. Just import them.
You might try tweaking Jet settings for importing Excel data. You can force the Jet engine to sample the entire sheet when trying to determine the column type during a given import. Change the following registry key (after making a backup first) and see if that doesn't do it:
HKLM\Software\Microsoft\Jet\4.0\Engines\Excel
Or on x64
HKLM\Software\Wow6432Node\Microsoft\Jet\4.0\Engines\Excel
Set the value TypeGuessRows equal to zero. This will force Jet to sample all rows to determine the column type.
Create a new column that is an apostophe + the contents of the column you want to import. This will force it to be a string
="'"&E2
Adding IMEX=1 BEFORE the HDR setting worked for me.
This is the best solution:
Click File on the ribbon menu, and then click on Options.
Click Advanced, and then under When calculating this workbook, select the Set precision as displayed check box, and then click OK.
Click OK.
In the worksheet, select the cells that you want to format.
On the Home tab, click the Dialog Box Launcher Button image next to
Number.
In the Category box, click Number.
In the Decimal places box, enter the number of decimal places that
you want to display.