Link to home
Start Free TrialLog in
Avatar of rolandkg1001
rolandkg1001Flag for Austria

asked on

How can I add in excel vba a range.name to formula1 of a validationlist if range inludes more lines

Please I need help in excel vba with validationlist of dropdowns

I use the code below. I create a range, give the range a name and put it to a validation list of a cell.
This works fine as long as the range consists only of one line.

If I write in my code:
.Range(.Cells(1, 2), .Cells(1, 255)).Name = strBereichsName

the code works fine

If i write in my code a range with more than one lines per example
.Range(.Cells(1, 2), .Cells(2, 255)).Name = strBereichsName

I get an error when the debugger is on following point:
With AktiveZelle.Validation 'Selection.Validation
            .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
            xlBetween, Formula1:="=" & strBereichsName

I want to use a range for strBereichsName wich can have more than only one line.
Please help me to get over this (bug?). Is this a bug, or is this a limitation? How can I add a validation list to an active cell using a range name, when the range has more than one line?

Thanks a lot in advance for any help!
Dim myRange As Range
 
With shtSheet
        .Range(.Cells(1, 2), .Cells(1, 255)).Name = strBereichsName
           
End With
      
        ' Hide the new sheet.
'        shtSheet.Visible = xlSheetHidden
        
        'Hide the new range
        ActiveWorkbook.Names(strBereichsName).Visible = False
        ' End major changes to code *********************************
     
        On Error GoTo errdaten
        With AktiveZelle.Validation 'Selection.Validation
            .Delete
            .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
            xlBetween, Formula1:="=" & strBereichsName
            .IgnoreBlank = True
            .InCellDropdown = True
            .InputTitle = ""
            .ErrorTitle = ""
            .InputMessage = ""
            .ErrorMessage = ""
            .ShowInput = True
            .ShowError = True
        
        End With

Open in new window

Avatar of Calvin Brine
Calvin Brine
Flag of Canada image

Sorry,  Was doing my real job:-).
I've created a summary for you based on the information you gave me, but the numbers from your sample don't seem to make any sense to me, so I'm hoping it just the test data....Hope they do for you.  Let me know.

DESIRED-FORMAT-1-.xls
Crap...posted to wrong thread!!!  Sorry.
 
I was working on yours, and I'm not able to recreate the error on my copy of excel.  Can you post the error message you are recieving?
 
Cal
One thing I would to know, is what type of object is AktiveZelle?
Is it a worksheet or a range object?  If a worksheet, that's where your problem might be.  Validation needs to be on a range.
 
Cal
ASKER CERTIFIED SOLUTION
Avatar of Rory Archibald
Rory Archibald
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Rory,
  Good catch, I missed that entirely!!!  I replaced his range with my own test range, which of course only had one column, so I didn't encounter it in my testing!
Luckily, I'm on my eighth cup of coffee this morning, so I'm quite alert for once! :)
No excuses for me, I was hopped up on about 6 cups of coffee yesterday, so the caffiene levels were all topped up. Just started going off in the wrong direction, charging like a bull.  One of these days I will starting reading the entire post:-)
Avatar of rolandkg1001

ASKER

Thanks, as far as I understand: no way because of limitation.