excel vba - VBA adding cell validation failing for single cell range -


i have vba macro gets data sheet range object , uses range object add validation on cell on current sheet this

with cells(c.row, colresource).validation     .delete     .add type:=xlvalidatelist, formula1:="=" & sheets(sheetresources).name & "!" & resrng.address     .incelldropdown = true end 

this works fine except when resrng object has 1 cell. can cause range multiple cells work single cell range fail?

any appreciated!

thanks!

the solution shows how bizarre excel can be.

firstly, @ being fed formula1 parameter.

if single cell selected:

$g$2 

if range of cells selected:

$g$2:$g$3 

infuriatingly, excel expecting start , end range.

the solution check range size, here example piece of code based on yours used testing can adjust needs.

sub add_validation()  dim rng range set rng = selection   cells(1, 1).validation     .delete     if selection.count = 1         .add type:=xlvalidatelist, formula1:="=" & activesheet.name & "!" & rng.address & ":" & rng.address     elseif selection.count > 1         .add type:=xlvalidatelist, formula1:="=" & activesheet.name & "!" & rng.address     end if     .incelldropdown = true end  end sub 

and code size check implemented:

with cells(c.row, colresource).validation     .delete     if resrng.count = 1         .add type:=xlvalidatelist, formula1:="=" & sheets(sheetresources).name & "!" & resrng.address & ":" & resrng.address     elseif resrng.count > 1         .add type:=xlvalidatelist, formula1:="=" & sheets(sheetresources).name & "!" & resrng.address     end if     .incelldropdown = true end 

Comments

Popular posts from this blog

java - Spring Data JPA: Why findOne(id) executing delete query internally? -

python - Mongodb How to add addtional information when aggregating? -

java - Incorrect order of records in M-M relationship in hibernate -