vba - How to set active sheet as variable in below recorded macro for excel 2013? -
i recorded macro in excel. have excel generate report every week. position , number of columns remains same. but, number of rows change every week. same way file name , sheet name changes.
this recorded macro recorded sheet named "2017_08_13". have change sheet name manually in macro every time. same way cell selection goes row number 101. everytime have manually enter last row number working. can me make 2 things work every sheet automatically. 1) run on activesheet so, instead of sheet name, can make go active sheet. 2) should go upto last cell select. in line 17 ( guess) pointing go row 101.
sub macro1() ' ' macro1 macro ' ' columns("e:e").select selection.insert shift:=xltoright, copyorigin:=xlformatfromleftorabove range("e1").select activecell.formular1c1 = "decimal" range("e2").select activecell.formular1c1 = "=rc[-1]*24" range("e2").select selection.copy range("d2").select selection.end(xldown).select range("e101").select range(selection, selection.end(xlup)).select activesheet.paste activewindow.smallscroll down:=-105 application.cutcopymode = false selection.numberformat = "general" activewindow.smallscroll down:=-126 range("a1").select range(selection, selection.end(xltoright)).select range(selection, selection.end(xldown)).select selection.borders(xldiagonaldown).linestyle = xlnone selection.borders(xldiagonalup).linestyle = xlnone selection.borders(xledgeleft) .linestyle = xlcontinuous .colorindex = 0 .tintandshade = 0 .weight = xlthin end with selection.borders(xledgetop) .linestyle = xlcontinuous .colorindex = 0 .tintandshade = 0 .weight = xlthin end with selection.borders(xledgebottom) .linestyle = xlcontinuous .colorindex = 0 .tintandshade = 0 .weight = xlthin end with selection.borders(xledgeright) .linestyle = xlcontinuous .colorindex = 0 .tintandshade = 0 .weight = xlthin end with selection.borders(xlinsidevertical) .linestyle = xlcontinuous .colorindex = 0 .tintandshade = 0 .weight = xlthin end with selection.borders(xlinsidehorizontal) .linestyle = xlcontinuous .colorindex = 0 .tintandshade = 0 .weight = xlthin end selection.borders(xldiagonaldown).linestyle = xlnone selection.borders(xldiagonalup).linestyle = xlnone selection.borders(xledgeleft) .linestyle = xlcontinuous .colorindex = 0 .tintandshade = 0 .weight = xlmedium end with selection.borders(xledgetop) .linestyle = xlcontinuous .colorindex = 0 .tintandshade = 0 .weight = xlmedium end with selection.borders(xledgebottom) .linestyle = xlcontinuous .colorindex = 0 .tintandshade = 0 .weight = xlmedium end with selection.borders(xledgeright) .linestyle = xlcontinuous .colorindex = 0 .tintandshade = 0 .weight = xlmedium end with selection.borders(xlinsidevertical) .linestyle = xlcontinuous .colorindex = 0 .tintandshade = 0 .weight = xlthin end with selection.borders(xlinsidehorizontal) .linestyle = xlcontinuous .colorindex = 0 .tintandshade = 0 .weight = xlthin end range("a1:f1").select selection.borders(xldiagonaldown).linestyle = xlnone selection.borders(xldiagonalup).linestyle = xlnone selection.borders(xledgeleft) .linestyle = xlcontinuous .colorindex = 0 .tintandshade = 0 .weight = xlmedium end with selection.borders(xledgetop) .linestyle = xlcontinuous .colorindex = 0 .tintandshade = 0 .weight = xlmedium end with selection.borders(xledgebottom) .linestyle = xlcontinuous .colorindex = 0 .tintandshade = 0 .weight = xlmedium end with selection.borders(xledgeright) .linestyle = xlcontinuous .colorindex = 0 .tintandshade = 0 .weight = xlmedium end with selection.borders(xlinsidevertical) .linestyle = xlcontinuous .colorindex = 0 .tintandshade = 0 .weight = xlthin end selection.borders(xlinsidehorizontal).linestyle = xlnone columns("f:f").entirecolumn.autofit range("h2").select activeworkbook.pivotcaches.create(sourcetype:=xldatabase, sourcedata:= _ "2017_08_13!r1c1:r101c6", version:=xlpivottableversion15). _ createpivottable tabledestination:="2017_08_13!r2c8", _ tablename:="pivottable1", defaultversion:=xlpivottableversion15 sheets("2017_08_13").select cells(2, 8).select activesheet.pivottables("pivottable1").pivotfields("activity") .orientation = xlrowfield .position = 1 end activesheet.pivottables("pivottable1").adddatafield activesheet.pivottables( _ "pivottable1").pivotfields("decimal"), "sum of decimal", xlsum activeworkbook.showpivottablefieldlist = false activewindow.smallscroll down:=-105 end sub
i not programming guy. thank you
if want loop through sheets, without using names, can use:
dim integer, j integer, lr long j = sheets.count = 1 j sheets(i) lr=.cells(.rows.count, "a").end(xlup).row 'allows have dynamic row count 'your code here, should see .columns("e:e") rather columns("e:e") make use of statement end next
Comments
Post a Comment