conditionally auto name excel file using VBA -
i have following vb code uses cell values (used fixed location) of db default file name current worksheet , save in specific folder use of activex button. however, column locations started varying in original file , vb ranges no longer pick correct data auto name file. created formulas (further below) find correct values need use auto name file. how insert formulas in vb code job?
private sub commandbutton1_click() dim path string dim filename1 string dim filename2 string dim filename3 string dim str string, strleft string str = range("a7") strleft = left(str, 9) filename1 = strleft filename2 = repch(range("b7").value) filename3 = repch(range("c7").value) path = "…" activeworkbook.saveas filename:=path & filename1 & "_" & filename2 & "_" & filename3 & "_" & ".xls", fileformat:=xlcsv end sub
formulae:
=left(index($a$7:$az$7,match("departure",$a$6:$az$6,0)), search("",index($a$7:$az$7,match("departure",$a$6:$az$6,0)),9)) =index($a$7:$az$7,match("vessel name",$a$6:$az$6,0)) =index($a$7:$az$7,match("voyage number",$a$6:$az$6,0))
there's more efficient ways mine, write formulas string variable, , save off string variable.
however when have "
, need split formula multiple strings , concatenate &
each time have quotation mark. (i.e - replace every "
chr(34)
)
as example top formula:
dim vformula1 string dim vformula2 string dim vformula3 string 'set formula string variable vformula1 = "=left(index($a$7:$az$7,match(" & chr(34) & "departure" & chr(34) & ",$a$6:$az$6,0)), search(" & chr(34) & chr(34) & ",index($a$7:$az$7,match(" & chr(34) & "departure" & chr(34) & ",$a$6:$az$6,0)),9))" vformula2 = "=index($a$7:$az$7,match(" & chr(34) & "vessel name" & chr(34) & ",$a$6:$az$6,0))" vformula3 = "=index($a$7:$az$7,match(" & chr(34) & "voyage number" & chr(34) & ",$a$6:$az$6,0))" 'then put formula cell range 'set sheet range here if needed range("a1") = vformula1 range("a2") = vformula2 range("a3") = vformula3 'then save code goes here 'blah blah blah save files.
Comments
Post a Comment