excel - VBA prevent user changing cell value with reference to only the initial cell value -
i'm trying stop fields being changed user. don't know columns fields in, value contain.
my current approach this:
private sub workbook_sheetchange(byval sh object, byval target range) dim columnheaderrange range set shtdata = worksheets("data") set columnheaderrange = union(shtdata.columns(columnnumber(5, "example1")), _ shtdata.columns(columnnumber(5, "example2")), _ shtdata.columns(columnnumber(5, "example3"))) set columnheaderrange = application.intersect(target, columnheaderrange) elseif not (columnheaderrange nothing) application .enableevents = false .undo msgbox "change not possible.", 16 .enableevents = true end else exit sub end if
my columnnumber function in above code takes row , field value parameters , returns column number. since i'm using fixed field values though, fails if field has been changed union call fails.
is there way have code run upon user attempting change value of cell before actual value of cell changed? alternatively can suggest better approach?
further comments
example 1
create sheet called list
, store values. best part method not have amend code every time want add/delete items list.
see screenshot
and let's main sheet
paste code in sheet code area
dim rnglist range, acell range dim rowar() long private sub worksheet_change(byval target range) dim long on error goto whoa application.enableevents = false each acell in target if acell.row = 5 application = lbound(rowar) ubound(rowar) if rowar(i) = acell.column msgbox "change not possible." .undo goto letscontinue end if next end end if next letscontinue: application.enableevents = true exit sub whoa: msgbox err.description resume letscontinue end sub private sub worksheet_selectionchange(byval target range) dim wslist worksheet dim n long, lrow long set wslist = thisworkbook.sheets("list") wslist lrow = .range("a" & .rows.count).end(xlup).row set rnglist = .range("a1:a" & lrow) end n = 0 redim rowar(n) each acell in range("5:5") if len(trim(acell.value)) <> 0 if application.worksheetfunction.countif(rnglist, acell.value) > 0 n = n + 1 redim preserve rowar(n) rowar(n) = acell.column debug.print acell.column end if end if next end sub
code in action
example 2
this uses hardcoded list.
option explicit dim rowar() long, acell range private sub worksheet_selectionchange(byval target range) dim mylist string, myar() string dim n long, long '~~> list mylist = "header 1,header 2" myar = split(mylist, ",") n = 0 redim rowar(n) each acell in range("5:5") if len(trim(acell.value)) <> 0 = lbound(myar) ubound(myar) if acell.value = myar(i) n = n + 1 redim preserve rowar(n) rowar(n) = acell.column end if next end if next end sub private sub worksheet_change(byval target range) dim long on error goto whoa application.enableevents = false each acell in target if acell.row = 5 application = lbound(rowar) ubound(rowar) if rowar(i) = acell.column msgbox "change not possible." .undo goto letscontinue end if next end end if next letscontinue: application.enableevents = true exit sub whoa: msgbox err.description resume letscontinue end sub
Comments
Post a Comment