I have an Excel file with a userform where users can insert exchange rates and different parameters for financial report.
I set up a DB file and I want to select rows based on the values in BV column (book value) and it should be higher than 100.000EUR.
The problem is that I have three other currencies besides EUR and I need to calculate EUR amount based on exchange rate.
I made an exchange rate field in my userform (BVAL is EUR default, and I also have BVGBP, BVPLN and BVRSD, which are 100.000EUR divided by each exchange rate).
This works:
SqlQuery = " SELECT * FROM Sheet1 where (bv>=" & BVAL & " and Currency = 'EUR')";"
But when I try to combine multiple results, syntax is not working. I tried other solutions found online.
SqlQuery = " SELECT * FROM Sheet1 where (bv>=" & BVAL & " and Currency='EUR') or (bv>=" & BVGBP & " and Currency='GBP') _
or (bv>=" & BVPLN & " and Currency='PLN') or (bv>=" & BVRSD & " and Currency='RSD');"
You SQL syntax is correct, but your VBA is not. Also, for better readability, break up the long lines:
SqlQuery = " SELECT * FROM Sheet1 where " & _
"(bv>=" & BVAL & " and Currency='EUR') or " & _
"(bv>=" & BVGBP & " and Currency='GBP') or " & _
"(bv>=" & BVPLN & " and Currency='PLN') or " & _
"(bv>=" & BVRSD & " and Currency='RSD');"
Related
I need help fixing my conditions. I have 3 columns (Case #, Analyst & Status). I need to display a certain Analyst plus all status that aren't closed.
Dim c As String
Dim use As String
use = LabelUser.Caption
c = "Closed"
SQL = "SELECT * FROM tbl_Raw WHERE Analyst = '" & use & "' OR Status <> '" & c & "'"
this statement displays all cases under the analyst, including closed (c).
I tried this but it displays nothing.
SQL = "SELECT * FROM tbl_Raw WHERE Analyst = '" & use & "' AND Status <> '" & c & "'"
It's been a long time since this question has been made, however, it still worth answering.
Whenever you use SQL statement within ADO connection, you must assign brackets around and dollar sign at the end of the Table's name. E.g: "SELECT * FROM [tbl_Raw$] WHERE Analyst = '" & use & "' OR Status <> '" & c & "'"
I have looked around SO and haven't found an answer to this type of question. It is probably something trivial that I am doing wrong, but I have tried several different formats of the below with no success. I have a very long VBA script which uses calculations to determine which fields to include in the pivot. I then take the variable fields returned and turn them into variables (pivot1, pivot2, etc.). I can't name the fields specifically in the addition of the calculated field, as I don't know what they will be. The second segment of code throws off the error "Unable to get the PivotFields property of the PivotTable class". Any help with this would be much appreciated.
ActiveSheet.PivotTables("Pivot Table 1").CalculatedFields.Add "Total", "= pivot1 _
& " + " & pivot2 & " + " & pivot3 & " + " & pivot4 & " + " & pivot5 & " + " & pivot6 _
& " + " & pivot7 & " + " & pivot8 & " + " & pivot9 & " + " & pivot10 & " + " & pivot11 _
& " + "& pivot12", True
ActiveSheet.PivotTables("Pivot Table 1").AddDataField ActiveSheet.PivotTables _
("Pivot Table 1").PivotFields("Total"), "Grand Total", xlSum
I have a database in access where every data has a date and time in two different columns.
I need to to run a query in excel using VBA which would fetch me data between two specific dates and times (eg: 01/05/2016 13:15 and 03/05/2016 10:11)
My query is as follows:
SQL = "SELECT * FROM " & database & " WHERE symbol='" & companyName & _
"' AND AdmitDate BETWEEN " & (fromDate + fromTime) & " AND " & (toDate + toTime) & ""
However, it gives me a syntax error which says:
'Missing operator in query expression'
I am not able to figure out where I have gone wrong.
Please help!
You need the right string expressions for the date/time values. Format can create these:
SQL = "SELECT * FROM " & database & " WHERE symbol='" & companyName & "' AND AdmitDate BETWEEN #" & Format(fromDate + fromTime, "yyyy\/mm\/dd hh\:nn\:ss") & "# AND #" & Format(toDate + toTime, "yyyy\/mm\/dd hh\:nn\:ss") & "#"
I am quite new to VBA so I am sorry if my question might seems very trivial.
I would love to write a function in VBA which helps to estimate weighted average rate (for loan portfolio, for instance). I wrote the following VBA code:
Function WAIRS(Amount As Range, InterestRate As Range, MatchRange As Range, Match1)
WAIRS = Evaluate("=SUMPRODUCT(--(""" & MatchRange & """ = """ & Match1 & """),""" & Amount & """, """ & InterestRate & """)") /Application.WorksheetFunction.SumIfs(Amount, MatchRange, Match1)
End Function
The problem is that when I run this function in Excel by adding respective function criterias I get an "#VALUE#". I have tried a lot but cannot find out what is wrong. I Would highly appreciate if you can help me.
Thank you in advance.
Best,
Jeyhun
The string you build for Evaluate should (in this case) not include literal double quotes. Instead of quoting the result of a range value
"""" & MatchRange & """"
...you should retrieve the address notation of that range, and use that without wrapping it in quotes:
MatchRange.Address()
If you apply that consistently, it would make the Evaluate part of the formula look like this:
"=SUMPRODUCT(--(" & MatchRange.Address() & " = " & Match1.Address() & "), " & _
Amount.Address() & ", " & InterestRate.Address() & ")"
When range is another sheet:
The above will not work if your ranges are on another sheet. In that case, I would suggest to create this function:
Public Function fullAddr(range As Range)
fullAddr = "'" & range.Parent.Name & "'!" & _
range.Address(External:=False)
End Function
And then in your formula:
"=SUMPRODUCT(--(" & fullAddr(MatchRange) & " = " & fullAddr(Match1) & "), " & _
fullAddr(Amount) & ", " & fullAddr(InterestRate) & ")"
I did a little bit of reading on what I am about to ask, but I couldn't find a specific answer.
I am writing a program in VBScript that will read an excel file and then update an Access Database.
It works great, but the problem I can foresee running into is thus:
What happens when the excel file has a blank on a specified field? I don't want the script file to update a "blank" to the database, I want it to be left unchanged in the database ONLY if there is a blank in the excel file. If not, proceed as normal.
Currently, it will read this as a blank and insert the blank into the row in my database.
Is this possible? For the script to basically ignore fields it reads (in Excel) that are blank while only updating (in the database) the fields that actually have data (in Excel) in them?
Currently, it says no in the field in the database. This is just a brief code example. In the Excel sheet, it says yes in the correct column.
Do Until objExcel.Cells(intRow,1).Value = ""
asset_Tag = objExcel.Cells(intRow, 1).Value
ebay = objExcel.Cells(intRow, 2).Value
intRow = intRow + 1
objRecordSet.Open "UPDATE Test SET Listed_Ebay = '" & ebay & "' WHERE Asset_Tag = '" & asset_Tag & "'", _
objConnection
Loop
If you are reluctant to add 20 If statements to your code, surely you could put Excel's native COUNTA functionality to make sure that there are 20 values before the update operation.
With objExcel
Do Until .Cells(intRow,1).Value = ""
If .Application.CountA(.Cells(intRow, 1).Resize(1, 20)) = 20 Then
asset_Tag = .Cells(intRow, 1).Value
ebay = .Cells(intRow, 2).Value
objRecordSet.Open "UPDATE Test SET Listed_Ebay = '" & ebay & "' WHERE Asset_Tag = '" & asset_Tag & "'", objConnection
End if
intRow = intRow + 1
Loop
End With
I had a guess a bit as to which 20 values were intended to be filled but I think you can get the idea.
EDIT NOTEĀ¹: reordered the code lines to maintain the intRow according to the loop.
EDIT NOTEĀ²: You may want to conditionally pass the original value back into the UPDATE query if the LEN of the new value is zero.
Dim qry as String
qry = "UPDATE Test SET " & _
"Listed_Ebay = Iif(CBool(Len('" & ebay & "')), '" & ebay & "', Listed_Ebay), " & _
"Listed_Amazon = Iif(CBool(Len('" & amzon& "')), '" & amzon & "', Listed_Amazon), " & _
"Listed_POS = Iif(CBool(Len('" & pos& "')), '" & pos & "', Listed_POS) " & _
"WHERE Asset_Tag = '" & asset_Tag & "'"
objRecordSet.Open qry, objConnection
I'm just making stuff up past the original one field example you provided but you should be able to transcribe this for your own 8 fields. I'm using LEN here on text values but comparing numbers to zero (or even their LEN would be appropriate.