Excel 2010: Best use of CASE or IF to define SAVEAS location (VBA) -


program: excel 2010
experience: basic

question
wanting save workbook sheet (and generated .pdf) in locations dependent on cell value, rather writing 5 subs, i want write 1 using either if or case. have static save location (dropbox), need save duplicates in respective cell locations.

i can't syntax correct either working.

sub savemanid()     dim sdb string     dim smdocs string     dim smbus string     dim sname string     dim ssel string     dim sman string      'define file name     sname = sheets("statement").range("b52").text      'define location name     sdb = "e:\location dropbox\"     smdocs = "d:\my documents\"     smbus = "d:\location alt\"      '---- either if or case define saveas location ----'       if sheets("statement").range("j2").text = "3"         sman = "g:\location\folder3\"      if sheets("statement").range("j2").text = "4"         sman = "g:\location\folder4\"      '---- end ----'         activeworkbook.saveas filename:=sman & sname & format(date, "yyyymmdd") & ".xls",_        fileformat:=52, readonlyrecommended:=false, createbackup:=false          sheets("statement").range("a1:g49").exportasfixedformat type:=xltypepdf, filename:=sman & sname & ".pdf", _         quality:=xlqualitystandard, includedocproperties:=true, ignoreprintareas:=false, openafterpublish:=true           end if     end if end sub   

i need range("j2") define location cell value & need create name based on cell value.

the sub runs fine if exclude if, means have have code duplicated , button assigned each value.

to clarify - cell value range("j2") determine file saves to, in example above in if statement, not work.

to fix have written try moving end if before common code this:

if sheets("statement").range("j2").text = "3"     sman = "g:\location\folder3\" end if  if sheets("statement").range("j2").text = "4"     sman = "g:\location\folder4\" end if  activeworkbook.saveas filename:=sman & sname & format(date, "yyyymmdd") & ".xls",_    fileformat:=52, readonlyrecommended:=false, createbackup:=false      sheets("statement").range("a1:g49").exportasfixedformat type:=xltypepdf, filename:=sman & sname & ".pdf", _     quality:=xlqualitystandard, includedocproperties:=true, ignoreprintareas:=false, openafterpublish:=true 

or this:

if sheets("statement").range("j2").text = "3"     sman = "g:\location\folder3\" elseif sheets("statement").range("j2").text = "4"     sman = "g:\location\folder4\" end if 

or this:

myval = sheets("statement").range("j2").text if myval = "3"     sman = "g:\location\folder3\" elseif myval = "4"     sman = "g:\location\folder4\" end if 

to use select case re-write this:

select case sheets("statement").range("j2").text       case "3"            sman = "g:\location\folder3\"        case "4"            sman = "g:\location\folder4\"        case else            'set sman default dropbox location here end select 

Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -