excel - Vlookup in a Loop Process to Change Save Filename VBA -


all - looking write loop can change filename , folder location depending on value runs in loop. example, if running macro cells g2:g7, when process moves g2 g3, want filename , folder location change according reference table (look image details). effectively, want filename , foldername lookups fund types.

public sub get_file()      dim sfiletype string     dim sfilename string     'save file name, if "" default     dim sfolder string       'save folder, if "" default     dim breplace boolean     'to replace existing file or not     dim surl string          'the url location extract information     dim cell, rng range     dim sheet worksheet      'initialize variables      set rng = range("i2:i10")     set sheet = activeworkbook.sheets("macro_button")      each cell in rng         if cell <> ""         sfiletype = cell.value         sfilename = sfiletype & "_" & format(date, "mmddyyyy")          sfolder = application.worksheetfunction.vlookup(sfiletype, sheet.range("g2:j10"), 4, false)         breplace = true         surl = "www.preqin.com"          'download using desired approach, xmlhttp / ie         call download_use_ie(surl, sfilename, sfolder, breplace)          else         exit sub         end if     next  end sub 

thanks input!

http://i.stack.imgur.com/m6gss.png

therein lies limitation of vlookup matches must align leftmost column , searches right. advised in popular web search, consider index/match replacement compares column column , returns value on matched row (in direction):

sfolder = application.worksheetfunction.index(sheet.range("g2:j10"), _               application.worksheetfunction.match(sfiletype, sheet.range("i2:i10"), 0), 4) 

if needing use vlookup(), need decrease lookup range:

sfolder = application.worksheetfunction.vlookup(sfiletype, sheet.range("i2:j10"), 2, false) 

Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -