excel - How to use Vlookup inside VBA with a variable worksheet name -
i need use vlookup inside vba worksheetname changes can referenced activeworkbook.worksheet(1) , activeworkbook.worksheet(2). used dim selection can refer ws1 , ws2 of course inside vlookup formula kind of script doesn't work. hope can rewrite vlookup formula lines.
it regards last 5 lines activecell.formular1c1 lines need solution 'ws2'! help.
dim ws1 worksheet dim ws2 worksheet set ws1 = activeworkbook.worksheets(1) set ws2 = activeworkbook.worksheets(2) ws1.select selection.autofilter range("g2").select activecell.formular1c1 = "web sales" range("h2").select activecell.formular1c1 = "web stock" range("i2").select activecell.formular1c1 = "total sales" range("j2").select activecell.formular1c1 = "total stock" range("f2:f71").select selection.copy activewindow.smallscroll down:=-102 range("g2:j71").select activewindow.smallscroll down:=-66 selection.pastespecial paste:=xlpasteformats, operation:=xlnone, _ skipblanks:=false, transpose:=false columns("h:h").entirecolumn.autofit range("i5").select application.cutcopymode = false range("d3:d150").select ws2.select range("d3:d150").select ws1.select activewindow.smallscroll down:=-66 range("g3").select activecell.formular1c1 = _ "=vlookup(rc[-3],'ws2'!rc[-3]:r[42]c[-1],3,0)" activewindow.smallscroll down:=-30 activecell.formular1c1 = _ "=vlookup(rc[-3],ws2!r3c4:r45c6,3,0)"
below 2 examples original code showing how include name of worksheet formula:
activecell.formular1c1 = "=vlookup(rc[-3]," & ws2.name & "!rc[-3]:r[42]c[-1],3,0)" activecell.formular1c1 = "=vlookup(rc[-3]," & ws2.name & "!r3c4:r45c6,3,0)"
Comments
Post a Comment