Excel VBA: Repeatedly Add Outside Borders -
i need add outside borders ranges in multiple worksheets, have written following test code (which works):
sub testfun() dim wb workbook dim ws worksheet set wb = workbooks("testbook.xlsm") wb.worksheets("sheet1").select range("b2:d10").select addoutsideborders wb.worksheets("sheet2").select range("b2:d10").select addoutsideborders wb.worksheets("sheet3").select range("b2:d10").select addoutsideborders end sub sub addoutsideborders() selection .borders(xledgeleft).linestyle = xlcontinuous .borders(xledgetop).linestyle = xlcontinuous .borders(xledgebottom).linestyle = xlcontinuous .borders(xledgeright).linestyle = xlcontinuous end end sub
however, real workbook contains many more sheets , need perform more complicated tasks. question is, how add borders without selecting range first? example want neat (it doesn't work):
sub testfun() dim wb workbook dim ws worksheet set wb = workbooks("testbook.xlsm") wb.worksheets("sheet1").range("b2:d10").addoutsideborders wb.worksheets("sheet2").range("b2:d10").addoutsideborders wb.worksheets("sheet3").range("b2:d10").addoutsideborders end sub sub addoutsideborders(rng range) rng .borders(xledgeleft).linestyle = xlcontinuous .borders(xledgetop).linestyle = xlcontinuous .borders(xledgebottom).linestyle = xlcontinuous .borders(xledgeright).linestyle = xlcontinuous end end sub
more generally, how call function defined on range within sub?
the method attempted valid (and on trying avoid select), called procedure in incorrect way. instead, should called as:
call addoutsideborders(wb.worksheets("sheet1").range("b2:d10")) call addoutsideborders(wb.worksheets("sheet2").range("b2:d10")) call addoutsideborders(wb.worksheets("sheet3").range("b2:d10"))
Comments
Post a Comment