Excel VBA: How do I create variable data validation based on the column header of the current column? -


edit: code updated reflect advice presented in comment. getting 1004 error on "set reflist" line.

background

sheet 1 or "user picklist" contains header row (row 11) variable titles. headers vary based on client, values correspond each header (listed in same column beginning row 12).

sheet 2 or "user list" contains actual users uploaded our system. see users inputting values don't exist on picklist.

the objective

i want set flexible data validation on page 2 (user list) each cell references own column header (so range("c13") reference range("c11").value), search value in row 11 of page 1(user picklist). when finds value, use picklist in column list of values data validation.

so if range("c11").value on page 2 "location", .find on row 11 on page 1, , if finds location add data validation based on subsequent values in column.

the code

    'remove case sensitivity       option compare text      private sub worksheet_selectionchange(byval target range)      application.screenupdating = false      dim ws worksheet     dim refrng range, newrng range     set ws = thisworkbook.worksheets("user picklist")     dim reflist range     set refrng = ws.range("a12:t123")     set newrng = range("a12:t101")     dim c range  application.calculation = xlcalculationmanual  each c in newrng      dim var1 string     dim var2 string     var1 = cells(11, c.column).value     dim rngfind range     set rngfind = ws.range("a11:zz11").find(var1)      if not rngfind nothing      set reflist = range(rngfind.offset(1, 0), rngfind.offset(1, 0).end(xldown))      c.validation         .delete 'delete previous validation         .add type:=xlvalidatelist, alertstyle:=xlvalidalertstop, formula1:="=" & ws.name & "'!" & reflist.address    end     end if     activeworkbook.sheets("user list").calculate next  application.calculation = xlcalculationautomatic  application.screenupdating = true  end sub 

as of right crashing document every time, , guess it's because doing 1 cell @ time in loop inefficient. haven't been able come other way of being able evaluate header relative each cell in range though.

any advice appreciated! :)

this might little better-behaved:

private sub worksheet_selectionchange(byval target range)      application.screenupdating = false      dim ws worksheet     dim refrng range, rngfind range, newrng range, hdr     dim reflist range, c range, rngheaders range, msg      on error goto errhandling      set ws = thisworkbook.worksheets("user picklist")      'only deal selected cell(s)     set newrng = application.intersect(me.range("a12:t101"), target)     if not newrng nothing          set rngheaders = ws.range("a11:zz11")          each c in newrng             c.validation.delete 'delete previous validation             hdr = me.cells(11, c.column).value             if len(hdr) > 0                 set rngfind = rngheaders.find(hdr, , xlvalues, xlwhole)                 'matched header?                 if not rngfind nothing                      set reflist = ws.range(rngfind.offset(1, 0), _                                            rngfind.offset(1, 0).end(xldown))                      c.validation.add type:=xlvalidatelist, _                                      alertstyle:=xlvalidalertstop, _                                      formula1:="='" & ws.name & "'!" & reflist.address                  end if 'matched header             end if 'has header          next c     end if 'in required range  here:     application.screenupdating = true     exit sub  errhandling:     if err.number <> 0         msg = "error # " & str(err.number) & " generated " & _             err.source & chr(13) & "error line: " & erl & chr(13) & err.description         debug.print msg, , "error", err.helpfile, err.helpcontext     end if     resume here end sub 

Comments

Popular posts from this blog

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

python Tkinter Capturing keyboard events save as one single string -

sql server - Why does Linq-to-SQL add unnecessary COUNT()? -