Using LEFT, FIND In VBA Query

Joined
Aug 19, 2011
Messages
1
I'll try to make this as clear as possible given my lack of knowledge in the area.

I'm trying to take the value of a cell, which will be a variable as it is pulled from another query and depending on that value another cell will be populated. This is done with another query.

The value of this first cell is made up of a code, and a decode, so there is a comma "," in place or "-", it doesn't really matter.

I am able to create a formula in Excel to pull the desired result, but I would like to use VBA, as the cell value of the new cell is the formula and my query is looking for a value, not a formula.

=LEFT(C29,((FIND(",",C29))-1))

For instance the value of cell C29 is: "X,XXXX Text"

so this would pull back X before the ","

Sometimes this value will be 1, 2 or 3 digits before the "," so it will not be fixed.

I have gotten it to work if I hard code the value (see code below where I commented it out), I'm just not sure how to use a variable in junction with a LEFT command in the query.

Here is a part of the query in the VBA code:



Public Function FuncAuthorities1()
Province = Sheets("Sheet1").Cells(2, 2)
CodeClass = Sheets("Sheet1").Cells(29, 3)
query = "SELECT DISTINCT a.code || ', ' || a.code_two || ', ' || name "
query = query + "From schema.province_code_class a, schema.class_decode b, schema.code_decode c "
query = query + "WHERE a.code = b.code "
query = query + "AND a.class = c.class "
query = query + "AND a.province_code = '" & Province & "' "
query = query + "AND a.code = '" & CodeClass & "' "
'query = query + "AND a.code = '7' "
query = query + "ORDER BY 1 asc "
Sheets("Sheet1").Activate
Row = 30
While Sheets("Sheet1").Cells(Row, 1) <> ""
Row = Row + 1
Wend

RunQuery Sheets("Sheet1").Range("T2"), query 'pastes query result in cell ("")
Sheets("Sheet1").Cells(2, 20).Select
Selection.Delete


End Function

Thanks in advance for any help you can give.
 
Last edited:

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"

Forum statistics

Threads
1,224,584
Messages
6,179,693
Members
452,938
Latest member
babeneker

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top