vba - Select cells in Excel based on cell content -
i'm working excel trying write macro in vb applications , use help.
my goal:
export 1000s of records toad excel, , run macro place records on sheets based on cell content.
example of data:
(sorry, i'd post image inline, don't have enough reputation)
explanation of data:
privilege - different roles of users
user_name - user name of each user
sort_name - last_name, first_name of each user
what i'd macro do:
in privilege column, select records same priv , corresponding details, cut them, , paste them on new sheet. repeat privilege through end of data.
example:
select admin rows along user_name , sort_name. cut selected rows, paste on sheet2.
select moderator rows along user_name , sort_name. cut selected rows, paste on sheet3.
select generaluser rows along user_name , sort_name. cut selected rows, paste on sheet4.
additional info:
in actual data, there thousands of rows 60 different privileges. so, when run on real data, result in workbook 60 different sheets.
privileges in order. admins together. there won't 10 admin's cell a2-a9 , more a67-a100. they're sequential.
the first sheet raw data in going deleted @ end of process, it's formatting not matter.
i've tried:
sub test() ' ' test macro ' ' range("a2:c9").select selection.cut sheets("sheet2").select activesheet.paste sheets("sheet1").select range("a10:c14").select selection.cut sheets("sheet3").select activesheet.paste sheets("sheet1").select range("a15:c25").select selection.cut sheets("sheet3").select sheets.add after:=sheets(sheets.count) activesheet.paste end sub
but selects generic cells, not cells based on text written. means it's not coded variable length.
any appreciated!
try this, below assumes: sheets not created privs, data sorted column a, activesheet sheet data
values_to_find need changed privs have, comma seperated list
it not delete original sheet
sub t() dim start_rng range values_to_find = "admin,moderator,generaluser" sp = split(values_to_find, ",") activesheet = 0 ubound(sp) found = 0 set start_rng = .range("a1") each cell in .range("a2:" & .range("a2").end(xldown).offset(1, 0).address) if found = 1 , cell.value <> sp(i) 'find end set end_rng = cell exit end if if cell.value = sp(i) , start_rng.address = "$a$1" 'find start set start_rng = cell found = 1 end if next set ws = sheets.add ws.name = sp(i) .range(start_rng.address & ":" & range(end_rng.address).offset(-1, 3).address).copy ws.range("a1").pastespecial next end end sub
Comments
Post a Comment