excel vba - VBA FormulaR1C1 adds parentheses when HLOOKUP referances another workbook -


i'm using following code variable workbook , retrieve data. enters formula cell range grabs data out of other workbook. problem if use r1c1 style in formula indicate range in other workbook want search, vba enters formula if a1 style. formula not return correct info.

but if leave portion of code in a1 style, vba adds parentheses range when enters cell , formula fails.

here line of code:

range("r8:r" & finalrow).formular1c1 = "=iferror(hlookup(if(right(rc[-13],2)=""b4"",index('[" & mynm & " " & mycc & " " & myunit & " evms.xlsm]" & myua _                 & "'!d:e,match(rc4,'[" & mynm & " " & mycc & " " & myunit & " evms.xlsm]" & myua & "'!e:e,0),1)," _                 & "index(indirect(""'[" & mynm & " " & mycc & " " & myunit & " evms.xlsm]""&rc5&""'!d:e"",true)," _                 & "match(rc4,indirect(""'[" & mynm & " " & mycc & " " & myunit & " evms.xlsm]""&rc5&""'!e:e"",true),0),1))" _                 & ",'[" & mynm & " " & mycc & " " & myunit & " evms.xlsm]sop'!r1c3:r4c41,4,false),""np"")" 

here result:

=iferror(hlookup(if(right(e13,2)="b4",index('[5p1b 4520 5015 evms.xlsm]ua1b'!d:(e),match($d13,'[5p1b 4520 5015 evms.xlsm]ua1b'!e:(e),0),1),index(indirect("'[5p1b 4520 5015 evms.xlsm]"&$e13&"'!d:e",true),match($d13,indirect("'[5p1b 4520 5015 evms.xlsm]"&$e13&"'!e:e",true),0),1)),'[5p1b 4520 5015 evms.xlsm]sop'!$c$1:$ao$4,4,false),"np")

i see you're using indirect in formula, , work if referenced workbook open. if open, can make whole formula indirect , rid of hassles way. can referencing address(row()-x,column()-y) within formulas work r1c1 style formula, , reference different rows/columns depending on own position though formula identical in every cell. example of simple formula works way this:
=indirect("'[my workbook.xlsx]sheet1'!"&address(row()-3,column()-1))

i believe need:

range("r8:r" & finalrow).formula = "=iferror(hlookup(if(right(indirect(address(row()-13,column()+2)))=""b4"",index('[" & mynm & " " & mycc & " " & myunit & " evms.xlsm]" & myua _             & "'!d:e,match(indirect(address(row(),column()+4)),'[" & mynm & " " & mycc & " " & myunit & " evms.xlsm]" & myua & "'!e:e,0),1)," _             & "index(indirect(""'[" & mynm & " " & mycc & " " & myunit & " evms.xlsm]""&rc5&""'!d:e"",true)," _             & "match(indirect(address(row(),column()+4)),indirect(""'[" & mynm & " " & mycc & " " & myunit & " evms.xlsm]""&rc5&""'!e:e"",true),0),1))" _             & ",'[" & mynm & " " & mycc & " " & myunit & " evms.xlsm]sop'!indirect(address(row()+1,column()+3 &":"& address(row()+4, column()+41)),4,false),""np"")" 

Comments

Popular posts from this blog

1111. appearing after print sequence - php -

java - WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/board/] in DispatcherServlet with name 'appServlet' -

Ruby on Rails, ActiveRecord, Postgres, UTF-8 and ASCII-8BIT encodings -