When I use WinSQL to run a SQL statement, the result is 20100803000001812. However, when I incorporate the SQL as a macro, the result is 2.01008E+16. What should I do in the macro in order to maintain the result as 20100803000001812 ?
Thanks,
Bob
According to this article ActiveCell.NumberFormat = "#" should do the trick.
ActiveCell.NumberFormat = "0" works for me (not what I expected, but so it goes)
You might want to throw in a Cells.Columns.AutoFit to resize the columns as necessary.
Related
I have the following line of code in VBA:
Range("A40:A64").Formula = "=IF(index(optional_processes,row()-39)=0;"";optional_processes,row()-39))"
Without the if-statement, some cells display 0. I want these 0to be blanks. That's what the if-statement should do. I think it gives the application or object-defined error because of the double quotation marks in the statement. How can I this get blank cells instead of 0?
Even if you local Excel uses ; (semicolon) as parameter separator in formulas, when writing a formula with VBA, you have to use , (comma)
Beside this, there are some more issues with the formula:
You are missing a index at the else-part
Use """" rather than "" like Kresimir suggests
Use an ISERROR like Kiran writes in his first suggestion
I think this will work:
Range("A40:A64").Formula = "=IF(isError(index(optional_processes,row()-39)),"""",index(optional_processes,row()-39))"
Try this:
Range("A40:A64").Formula = "=IF(ISERROR(index(optional_processes,row()-39)),'',index(optional_processes,row()-39))"
OR
Range("A40:A64").Formula = "=IF(index(optional_processes,row()-39)=0,"""",index(optional_processes,row()-39))"
I need to create a VBA code that computes whether the data in the query that I have created matches or not. I am finding it difficult to begin to write the code.
what I started doing is the following:
Private Sub Analyse_Click()
Dim row As Long
With Query("qry_test1").Range((qry_test1Range))
For row = 1 To .rows.Count
If .Cells(row, AccountingPeriodCol).Value Like AccountingPeriodCriteria
Debug.Print .Cells(row, ValueCol)
End If
End Sub
its wrong as the error wrong number of arguments or invalid property assignment appears.
Could some please be so kind of explaining the problem please
What you got in your code is Excel-VBA, apart from you replacing Sheets("WorksheetName") with Query("qry_test1").
This won't work. For stuff like that, you want to use SQL.
Read a tutorial on the basic syntax.
Create → Query Design → SQL View.
Write your SQL. Somethin along the lines of
SELECT *
FROM qry_Test
WHERE AccountingPeriod = 'your Criteria'`
should do what you want.
Optionally, wrap that into VBA
I did a search on this site, but can't find an answer to my problem. I am using VB 2010 Express. I'm trying to write formulas into a Excel spreadsheet.
oWorkSheet = oBook.Sheets(STRName)
'my variables are all declared strings
'this section works
FMLAValue1 = "=-L8"
FMLAValue2 = "=SUM($I$7;$F$8:$H$8)"
FMLAValue3 = "=SUM(M8:U8)"
oWorkSheet.Range("H8").Select()
oWorkSheet.Range("H8").Formula = FMLAValue1
oWorkSheet.Range("H8").AutoFill (oWorkSheet.Range("H8:H40"))
'up to here
'now this is the problem code below: if I paste the formula into a
'excel sheet cell, it works.
oWorkSheet.Range("I8").Select()
oWorkSheet.Range("I8").Formula = FMLAValue2
'**the line above gives an "Exception from HRESULT: 0x800A03EC "**
oWorkSheet.Range("I8").AutoFill (oWorkSheet.Range("I8:I40"))
'this part also works
oWorkSheet.Range("L8").Select()
oWorkSheet.Range("L8").Formula = FMLAValue3
oWorkSheet.Range("L8").AutoFill (oWorkSheet.Range("L8:L40"))
oWorkSheet.Range("J7").Select()
oWorkSheet.Range("J7").Formula = "=I7"
The formula stored in your FMLAValue2 variable doesn't look correct, you shouldn't have a ; (semi-colon) to separate the numbers, you should use a , (comma).
Use this instead:
FMLAValue2 = "=SUM($I$7,$F$8:$H$8)"
and you should hopefully be ok, unless there are other issues somewhere that I haven't noticed.
To test whether a formula is correct, use FormulaDesk. It will immediately tell you if there is an error, and pinpoint exactly where it is in your formula. You can then paste the formula into your VBA code, knowing that it works.
The image below shows how it can pinpoint exactly where your error is within the formula. So in your case, paste the non-working error into a cell in Excel, then view the error report in FormulaDesk to find out the exact cause.
[Disclosure: I am the author of FormulaDesk. I think it will definitely help the poster find any errors]
The excel 2007 format i am using is: #,##0.00,,,_);[Red](#,##0.00,,,);"-"
if is is possitive, i want the number to display in billions; if negative, in billions and in parentahis; if missing, "-".
It works fine with excel 2007. But when i tried to apply the format in vba, it did not work.
Please use the following numbers as example:
-11467478
224785.66
-5579046
1904770.9
-14916968
The data type i used is variant. shall i use long?
my initial code is something like:
......
with worksheet
'cells(1,1) would be any of the above numbers
.Cells(1, 1).NumberFormat = "#,##0.00,,,_);[Red](#,##0.00,,,);" - ""
end with
.....
I got an erros message run-time error 13, type mismatch
I even tried to decompose the format. but it still did not work.
i am quite new to vba. could anyone help me?
......
This will also work, the dash is one of the characters that don't need to be escaped...
"#,##0.00,,,_);[Red](#,##0.00,,,);-"
You need to use double " for the minus sign. I tested your values and I think it should be:
.NumberFormat = "#,##0.00,,,_);[Red](#,##0.00,,,);""-"""
###.value = "=LOOKUP(LEFT(W2),{"C","A","B"},{"Pick Up","Collect","Prepaid"})"
I want VBA to do this simple formula but getting "Expected: end of Statement" error.
It seems that I need to define something as VBA doesn't recognize character "{}" the brackets.
Assuming that ### actually symbolizes a cell object (otherwise you would get a compile error):
###.Value = "=LOOKUP(LEFT(W2),{""C"",""A"",""B""},{""Pick Up"",""Collect"",""Prepaid""})"
Also, I thought that you would have to change .Value to .Formula, but I tested and both ways work.
It might be requiring you to end the script like this
###.value = "=LOOKUP(LEFT(W2),{"C","A","B"},{"Pick Up","Collect","Prepaid"});"
OR
###.value = "=LOOKUP(LEFT(W2),{"C","A","B"},{"Pick Up","Collect","Prepaid"})";
NOTICE: the Semi-colon at the end ';'.
I'm not a VBA user for a long time. but just try. Delete this answer if its not good enough.