jkluio668
12/9/2017 - 2:55 PM

Validation_date

数据有效性

Sub add1()
    ’从选中单元格的行开始,向下填充数据有效性along个行
    a1 = Selection.row
    b1 = Selection.Column
    along = 60 'fill nums
    beginday1 = 171206
    '----------
    arr1 = get_data_arr("data_1", 1, ActiveSheet.Name)
    arr2 = get_data_arr("data_1", 2, ActiveSheet.Name)
    str1 = Join(arr1, ",")
    str2 = Join(arr2, ",")
    For i = a1 To a1 + along - 1
        strdate = datearr1(beginday1, i - a1 + 1) '获取日期序列作为数据有效性的序列,获取days1个,默认15个
        'Validation---
        Range("a" & i).Select
        With Selection.Validation
            .Delete
            .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
                xlBetween, Formula1:=strdate
            .IgnoreBlank = True
            .InCellDropdown = True
            .IMEMode = xlIMEModeNoControl
            .ShowInput = True
            .ShowError = False
        End With
        Range(fc_s("period") & i).Select
        With Selection.Validation
            .Delete
            .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
                xlBetween, Formula1:=str1
            .IgnoreBlank = True
            .InCellDropdown = True
            .IMEMode = xlIMEModeNoControl
            .ShowInput = True
            .ShowError = False
        End With
        Range(fc_s("position") & i).Select
        With Selection.Validation
            .Delete
            .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
                xlBetween, Formula1:=str2
            .IgnoreBlank = True
            .InCellDropdown = True
            .IMEMode = xlIMEModeNoControl
            .ShowInput = True
            .ShowError = False
        End With
        '---
    Next
    cells(a1, b1).Select
End Sub
Function datearr1(begindaystr, index, Optional repeat1 = 2, Optional days1 = 15)
Dim arr1()
s = "20" & Left(begindaystr, 2) & "/" & Mid(begindaystr, 3, 2) & "/" & Mid(begindaystr, 5, 2)
b = CVDate(s)
beginday1 = DateSerial(Year(b), Month(b), Day(b) - 1 + Int(index / repeat1))
For i = 1 To days1
    ReDim Preserve arr1(1 To i)
    daystr = Format(DateSerial(Year(beginday1), Month(beginday1), Day(beginday1) - 1 + i), "yymmdd")
    'daystr = Format(DateAdd("d", i - 1, beginday1), "yymmdd")
    arr1(i) = daystr
Next
datearr1 = Join(arr1, ",")
End Function