excel - VBA Go to the next filtered cell -
i using following code:
sub sendemail() dim objoutlook object dim objmail object dim rowscount integer dim index integer dim recipients string dim category string dim cellreference integer set objoutlook = createobject("outlook.application") set objmail = objoutlook.createitem(0) if activesheet.filtermode = true rowscount = activesheet.autofilter.range.columns(1).specialcells(xlcelltypevisible).cells.count - 1 elseif activesheet.filtermode = false rowscount = application.counta(range("a2:a" & rows.count)) - 1 end if ' in range("i1") there job category user wants email category = range("i1") if category = range("s2") ' cellreference amount of columns right of column a, ie column 0 cellreference below j - column location of email address according category cellreference = 10 elseif category = range("s3") cellreference = 14 elseif category = range("s4") cellreference = 18 elseif category = range("s5") cellreference = 16 end if index = 0 while index < rowscount set emailadrs = activesheet.autofilter.range.offset(1).specialcells(xlcelltypevisible).cells(1, cellreference).offset(0 + index, 0) recipients = recipients & emailadrs.value & ";" index = index + 1 wend objmail .to = recipients .subject = "this subject" .display end set objoutlook = nothing set objmail = nothing end sub
this code checks see if filter has been applied , counts amounts of rows if there 1 or isn't one, checks see should emailed (the 'category' in i1
job position of different individuals) , gets email addresses of required, issue i'm having have following data (this example of want do):
column column b column c smith male 123@123.co.uk jones male abc@abc.co.uk smith female 456@123.co.uk jones female def@abc.co.uk smith male 789@123.co.uk smith female 101112@123.co.uk smith female 141516@123.co.uk jones female ghi@abc.co.uk
and filter on jones
in column , female
in column b 2 rows returned, rather getting email addresses def@abc.co.uk
, ghi@abc.co.uk
email addresses def@abc.co.uk
, 789@123.co.uk
because finds first row filter applied goes next cell disregarding filter.
is there way can fix gets filtered cells?
it important point out filter may not same, won't both column , column b, might column or column b.
replace bottom section of code this:
if activesheet.filtermode = true activesheet.autofilter.range each in .offset(1).resize(.rows.count).specialcells(xlcelltypevisible).areas recipients = recipients & a(1, cellreference) & ";" next end msgbox replace(recipients, ";;", vbnullstring) end if
Comments
Post a Comment