vba - Hide column in excel by date YYYY MM for the rest of this year plus three more -


i'm trying hide columns in excel vba.

so far have hide after 36 months told needed rest of forecasted year plus 3 years hide months in year 4 , 5 except december.

i added picture of spreadsheet cells except input, cells c10 thru bj12 , row 6, come second page of workbook. in row 6 trying take year row 9 , use way, can hard coded in must dynamic because startyear changes second page depending on scenario , job selected.

'hide months except december after 36 months range("am:bj")     .entirecolumn.hidden = true         each cell in range("am7:bj7" & activecell.specialcells(xlcelltypelastcell).column)             select case cell.value             case = "currentyear"             case = "12"             cell.entirecolumn.hidden = false         end select     next cell end 

image of spreadsheet looks like

try this! code should keep columns unhidden until 3 years after starting year. should hide columns except december months.

'grab starting info startingaddress = "c9" 'set first date location startingyear = left(range(startingaddress).value, 4) 'grab first 4 characters starting year  'loop through dates , determine hide/unhide = 0 application.columns.count - 2     if range(startingaddress).offset(0, i).value = vbnullstring         exit 'saves time because doesn't go through entire loop can if needed     else         'hides column if year 3 years greater starting year , month not dec.         if cint(left(range(startingaddress).offset(0, i).value, 4)) > (3 + startingyear) , right(range(startingaddress).offset(0, i).value, 2) <> "12"             range(startingaddress).offset(0, i).entirecolumn.hidden = true         else             range(startingaddress).offset(0, i).entirecolumn.hidden = false 'just in case columns previous run hidden         end if     end if next 

example: (starting date = 2015 10) code keep columns unhidden until 2019 keep december months. if wanted start hiding @ 2018 change

if cint(left(range(startingaddress).offset(0, i).value, 4)) > (3 + startingyear) , right(range(startingaddress).offset(0, i).value, 2) <> "12" 

to

if cint(left(range(startingaddress).offset(0, i).value, 4)) >= (3 + startingyear) , right(range(startingaddress).offset(0, i).value, 2) <> "12" 

Comments

Popular posts from this blog

ruby - Trying to change last to "x"s to 23 -

jquery - Clone last and append item to closest class -

c - Unrecognised emulation mode: elf_i386 on MinGW32 -