excel - How to loop through rows, save these as variables and use them as variables VBA -
i'm trying store values in sheets variable, , go on reference sheet using variable use filter by.
this looped through until program reaches first empty cell.
the relevant code have far is:
sub program() dim integer = 2 until isempty(cells(i, 1)) debug.print sheets("button").activate dim first string first = cells(i, 1).value debug.print first dim second string second = cells(i, 2).value debug.print second 'filters data sheet , copies data sheets("data").activate sheets("data").range("a1").autofilter _ field:=2, _ criteria1:=first 'filters relevant organisation sheets("data").range("a1").autofilter _ field:=6, _ criteria1:="=" 'filters no response sheets("data").range("a1:h6040").copy 'this should loop through each separate group sheets(cstr(second)).select range("a1").select activesheet.paste selection.pastespecial paste:=xlpastevalues, operation:=xlnone, skipblanks _ :=false, transpose:=false = + 1 loop worksheets("data").autofiltermode = false end sub
i have changed program trying add notation such 'cstr' there error @ line: sheets(cstr(second)).select
when used sheets(second)).select
and debug.print's see if working isn't logging immediate window.
additionally, when run it, no error comes nothing seems happen.
not sure else add, or else try. cheers!
as first remark, using (at least first) sheet activation within loop seems unnecessary, because start of loop determines sheet being used control flow of loop.
furthermore, argue better remove sheet activation altogether, re: discussion .select
(the cases aren't same, solution discussed herein works better both .select
, .activate
in instances): how avoid using select in excel vba macros.
let's see if can refer table in "data" sheet in more direct manner, errorchecking.
my suggestion:
sub program() dim integer dim first, second string dim secondws worksheet dim datatbl listobject = 2 set datatbl = worksheets("data").range("a1").listobject.name ' above can done more elegantly if supply name of table sheets("data").activate until isempty(cells(i, 1)) debug.print first = sheets("button").cells(i, 1).value debug.print first second = sheets("button").cells(i, 2).value debug.print second 'filters data sheet , copies data datatbl.autofilter _ field:=2, _ criteria1:=first 'filters relevant organisation datatbl.autofilter _ field:=6, _ criteria1:="=" 'filters no response sheets("data").range("a1:h6040").copy 'this should loop through each separate group on error resume next set secondws = worksheets(second) on error goto 0 if not secondws nothing secondws.range("a1").pastespecial paste:=xlpastevalues else debug.print "sheet name second not found" end if = + 1 loop worksheets("data").autofiltermode = false end sub
if errors, please state line appears on , error message is.
Comments
Post a Comment