VBA Timestamp or other tab not working - vba

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

Related

Export Excel Function as a SQL request result

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

Is there a way to use VBA for Excel to output a formula to a specific cell in an Excel worksheet?

I am fairly new at programming. I feel like this should be a simple fix but I cannot find anything that works. I am trying to use a Select Case structure to write a different formula to a cell depending on the selected case. If I type:
Sheet1.Range("g10").Value = "=IF(SUM(F10)>0,SUM((F10-15)),"")"
I get an error:
Run-time error '1004'
Application-defined or object-defined error.
I can get it to work if I include spaces like so:
Sheet1.Range("g10").Value = " =IF(SUM(F10)>0,SUM((F10-15)),"") "
but then Excel puts it into the cell like text, not as a formula (it doesn't do anything).
I have tried a few different methods but have run into the same issue. Is it possible to do this?
I apologize if this has been asked and answered, but I wasn't able to find anyone referencing this specific problem.
You don't need to sum a single cell ie SUM(F10) Also SUM((F10-15)) is the same as F10-15.
Try this: Sheet1.Range("g10").Formula = "=IF(F10>0,F10-15,"""")" Also note, you needed to double the double quotes towards the end.
Note, you can do this on a range too like this:
Sheet1.Range("g10:g20").Formula = "=IF(F10>0,F10-15,"""")" and it is smart enough to increment the references for you for the 10 cells.
Wrong property. You need .formula not .value
worksheets("sheet1").range("g10").formula = "=IF(SUM(F10)>0,SUM((F10-15)),"")"
You should be able to type out the formula and then turn on the Macro Recorder and double-click on the cell with the forums, then turn off the Macro recorder and examine your code. That should give you what you want.

SQL query to link data in 2 Excel sheets

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...

Excel VBA: Get the right date into a cell using DatePicker

I have a UserForm with a DatePicker control in it.
It works fine, except when copying the selected date to the spreadsheet.
This is the code:
Range("A1").Value = UserForm1.DTPicker1.Value
Which returns:
00:00:00
In cell A1, no matter what date has been selected.
You cell formatting might be set to Time instead of Date.
Try changing that to see if it works.
Also, make formatting 'General' AFTER this step, to see if anything has been pasted in "A1".
You should need an intermediate variable to get it, like below:
t = Me.DTPicker1.Value
ws.Range("A1") = t
Make sure that the command to transfer the data is located in the same form or page of a multipage form as the DTPicker itself.
For some reason it wont work when the two are separated and will display a zero in the target cell. That zero is the "time" part of the date which has been switched off. Although the "Date" part refuses to transfer over, for some reason the time does, and so that zero time is read in the cell as time zero which is mid-day.
So in summary:
Keep the DTPicker and transfer control on the same page and that should solve the problem.
The code is pretty simple. It's
Sheet1.Range("AA9") = Me.DTPicker1.Value

vba write numeric data as text into excel

I am writing data into an excel spreadsheet. The data comes from an Access Database. Code like the line below works:
xl.cells(5,5)="joe"
but code like the line below does not:
xl.cells(5,5)="1/2CD438"
I get an error like "Application defined or object defined error"
I think Excel is trying to do a calculation, and the calculation fails. All I really want is the text string.
I've tried formating the column like
columns(1).NumberFormat="#"
, but it doesn't work either.
Any ideas?
Thanks for all the input. I had tried several of your ideas without success. As it turns out I was using variables to determine the exact row, column as:
xl.cells(NextRow, Col) = ars!PartNo
By simply replacing NextRow with an integer, the code worked. Replaced the integer in the test with NextRow, and the code ran. I suspect a misspelling or something, but couldn't see it looking at the code.
Thanks for the suggestions.