If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > PC based Database Applications > Microsoft Excel > Can I use Variable in VBA to Create Validation?

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 02-09-04, 19:25
JerryDal JerryDal is offline
Registered User
 
Join Date: Jan 2002
Location: Bay Area
Posts: 473
Question Can I use Variable in VBA to Create Validation?

I am trying to automate putting validation in a cell that allows selecting from a list of months, starting with the previous month. Using VBA and DateAdd, I can assign the string of months "Jan 2004,Dec 2003,Nov 2003...etc." to the variable AllDates (Dim AllDates as Variant) .

After running the VBA code, I get #VALUE! displayed in the cell when I use the validation dropdown arrow. The code is:

For i = 1 To 36
DateString = CStr(Format(DateAdd("m", -i, Now()), "mmm yyyy")) & ","
AllDates = AllDates & DateString
Next i

'remove the ending comma
AllDates = Mid(AllDates, 1, (Len(AllDates) - 1))

With Selection.Validation
.Delete
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
xlBetween, Formula1:=AllDates
.InCellDropdown = True
.InputTitle = ""
.ErrorTitle = ""
.InputMessage = ""
.ErrorMessage = ""
.ShowInput = True
.ShowError = True
End With

It seems that Formula1 does not work with Validation and variables.

Any solutions?

Thanks.
Jerry
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On