简体   繁体   中英

Using vlookup while using web external data

Good afternoon,

I am pulling a fuel database from gasbuddy and trying to use the VLOOKUP function to grab address, prices and times last updated. However, all I am getting are #N/A (value not available) results. My code is below:

=VLOOKUP(
  N5,
  GasPriceSearch.aspx?typ_adv_fuel_limit_36,
  COLUMN(B5),
  FALSE)

I tried converting this information into a table and the formula works but I run into issues where new fuel prices are reported and it messes up the entire table. I would rather do it from the initial data without a table.

在此处输入图像描述

Thank you, Jesse

There are several ways to achieve this. However, based on your data structure, a regular VLOOKUP won't work because VLOOKUP only searches to the right of the lookup value (ie, the address) and, for example, the price is to the left of it. Thus, one possibility would be to restructure your data so that you have address, price, update, and so on. This would also circumvent the problem of having the price on a different row than the lookup value.

Another way to work with this data structure is to use the INDEX function:

=INDEX(array, row_num, [column_num])

Your array reflects the range where your data is stored that you want to look up; in your case, considering only price and station A:B . Since you've hidden some columns, it's hard to see what else is in this datasheet. But just adjust it to your needs of the data.

The row number can be hard coded. However, since you want to look up a specific value (ie, address), you can use the MATCH function:

=MATCH(lookup_value, lookup_array, [match_type])

In this step you want to look up your address from column N in column B, where all stations are listed. You also need to specify the column you want to return. Again, you can either hard code it or use the MATCH function. With the match_type you can specify, for example, that you only want an exact match, ie, match_type = 0 .

To return the value "update" to the left of the address, your formula looks as follows:

=INDEX($A:$B,MATCH(N6,$B:$B,0),1)

To return the address itself (which is pretty much useless, since you already have it in column N), use the following formula with the adjusted column value from 1 to 2:

=INDEX($A:$B,MATCH(N6,$B:$B,0),2)

Lastly, your price information is one row above the address, you need to adjust the number of rows by -1 accordingly. That is, if your address is, for example, 4123 Town [...], you want to return column 1 of row 6 and not row 7:

=INDEX($A:$B,MATCH(N6,$B:$B,0)-1,1)

An alternative strategy would be to use the XLOOKUP function (only available in newer versions of Excel), which allows you to find values to the left of the lookup value. But also with this strategy you would have to deal with the problem of row differences. The OFFSET function could prove useful in this context, but it does not make the approach much easier than the INDEX function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM