vba - Copying single cell value from different closed workbooks in a sharepoint -
i have 1 master workbook , 20 other workbooks in sharepoint. using following code retrive single cell value closed workbook working fine.
sub example() dim wbpath string, wbname string dim wsname string, cellref string dim ret string wbpath = "http://*****/2014/" wbname = "overview 2014.xlsm" wsname = "sheet1" cellref = "e2" ret = "'" & wbpath & "[" & wbname & "]" & _ wsname & "'!" & range(cellref).address(true, true, -4150) activeworkbook.worksheets("sheet1").range("a4").value = executeexcel4macro(ret) end sub
now wanted same cell value needs copied workbooks sharepoint master workbook ranges a5, a6, a7, a8.... etc.
can 1 please me or give me hint on how copy same cell values different closed workbooks same sharepoint location?
i have tried following code other workbooks , working fine, wanted know there other smarter way reduce number of lines in code?
wbpath = "http://*****/2014/" wbname = "overview 2014.xlsm" wsname = "sheet1" cellref = "e2" ret = "'" & wbpath & "[" & wbname & "]" & _ wsname & "'!" & range(cellref).address(true, true, xlr1c1) activeworkbook.worksheets("sheet1").range("a4").value = executeexcel4macro(ret) wbname = "workbook2.xlsm" wsname = "sheet1" cellref = "e2" ret = "'" & wbpath & "[" & wbname & "]" & _ wsname & "'!" & range(cellref).address(true, true, -4150) activeworkbook.worksheets("sheet1").range("a5").value = executeexcel4macro(ret) wbname = "workbook3.xlsm" wsname = "sheet1" cellref = "e2" ret = "'" & wbpath & "[" & wbname & "]" & _ wsname & "'!" & range(cellref).address(true, true, -4150) activeworkbook.worksheets("sheet1").range("a6").value = executeexcel4macro(ret) wbname = "workbook4.xlsm" wsname = "sheet1" cellref = "e2" ret = "'" & wbpath & "[" & wbname & "]" & _ wsname & "'!" & range(cellref).address(true, true, -4150) activeworkbook.worksheets("sheet1").range("a7").value = executeexcel4macro(ret) wbname = "workbook5.xlsm" wsname = "sheet1" cellref = "e2" ret = "'" & wbpath & "[" & wbname & "]" & _ wsname & "'!" & range(cellref).address(true, true, -4150) activeworkbook.worksheets("sheet1").range("a8").value = executeexcel4macro(ret) wbname = "workbook6.xlsm" wsname = "sheet1" cellref = "e2" ret = "'" & wbpath & "[" & wbname & "]" & _ wsname & "'!" & range(cellref).address(true, true, -4150) activeworkbook.worksheets("sheet1").range("a9").value = executeexcel4macro(ret) wbname = "workbook7.xlsm" wsname = "sheet1" cellref = "e2" ret = "'" & wbpath & "[" & wbname & "]" & _ wsname & "'!" & range(cellref).address(true, true, -4150) activeworkbook.worksheets("sheet1").range("a10").value = executeexcel4macro(ret) wbname = "workbook8.xlsm" wsname = "sheet1" cellref = "e2" ret = "'" & wbpath & "[" & wbname & "]" & _ wsname & "'!" & range(cellref).address(true, true, -4150) activeworkbook.worksheets("sheet1").range("a11").value = executeexcel4macro(ret) wbname = "workbook9.xlsm" wsname = "sheet1" cellref = "e2" ret = "'" & wbpath & "[" & wbname & "]" & _ wsname & "'!" & range(cellref).address(true, true, -4150) activeworkbook.worksheets("sheet1").range("a13").value = executeexcel4macro(ret) wbname = "workbook10.xlsm" wsname = "sheet1" cellref = "e2" ret = "'" & wbpath & "[" & wbname & "]" & _ wsname & "'!" & range(cellref).address(true, true, -4150) activeworkbook.worksheets("sheet1").range("a14").value = executeexcel4macro(ret)
to reduce code use additional function:
function getvalue(wbpath string, wbname string, wsname string, cellref string) dim ret string ret = "'" & wbpath & "[" & wbname & "]" & _ wsname & "'!" & range(cellref).address(true, true, -4150) getvalue = executeexcel4macro(ret) end function
and call this:
sub test() dim integer, wbs wbs = array("overview 2014.xlsm", "workbook2.xlsm", _ "workbook3.xlsm", "workbook4.xlsm", _ "workbook5.xlsm", "workbook6.xlsm", _ "workbook7.xlsm", "workbook8.xlsm", _ "workbook9.xlsm", "workbook10.xlsm") ' lbound(wbs) = 0 = lbound(wbs) ubound(wbs) activeworkbook.worksheets("sheet1").range("a4").offset(i).value = _ getvalue("http://*****/2014/", cstr(wbs(i)), "sheet1", "e2") next end sub
Comments
Post a Comment