简体   繁体   English

到特定Cell Excel VBA休假计划的数据

[英]Data to particular Cell Excel VBA Leave plan

I am making a leave planner using one form where user choose his name than start date and end date and apply using VBA i am storing that data to Worksheet Column AB and C 我正在使用一种表格制作休假计划者,在该表格中,用户选择自己的姓名而不是开始日期和结束日期,然后使用VBA进行申请,我将该数据存储到工作表列AB和C

Dim irow As Long, _
wS As Worksheet, _
NextRow As Long, _
cF As Range
Set wS = Worksheets("Sheet1")
With wS
With .Range("A:A")

    Set cF = .Find(What:=Me.Combo.Value, _
                After:=.Cells(1, 1), _
                LookIn:=xlValues, _
                LookAt:=xlWhole, _
                SearchOrder:=xlByRows, _
                SearchDirection:=xlNext, _
                MatchCase:=False, _
                SearchFormat:=False)
    End With      
    If Not cF Is Nothing Then
    If cF.Offset(0, 1) <> vbNullString Then
        Set cF = cF.End(xlToRight).Offset(0, 1)
        cF.Value = Me.startdate.Value
        cF.Offset(0, 1).Value = Me.enddate.Value
    Else
        .Cells(cF.Row, "B").Value = Me.startdate.Value
        .Cells(cF.Row, "C").Value = Me.enddate.Value
    End If
Else
    NextRow = .Cells(.Rows.Count, "A").End(xlUp).Offset(1, 0).Row
    .Cells(NextRow, "A").Value = Me.Combo.Value
    .Cells(NextRow, "B").Value = Me.startdate.Value
    .Cells(NextRow, "C").Value = Me.enddate.Value
End If
End With

After Storing data i am using a formula to show those names in calendar 存储数据后,我正在使用公式在日历中显示这些名称

=IFERROR(INDEX($A$20:$A$151,MATCH(1,IF(DATE($D$1,ROWS($A$3:$A3),B$2)>=$B$20:$B$151,IF(DATE($D$1,ROWS($A$3:$A3),B$2)<=$C$20:$C$151,1)),0)),"") = IFERROR(INDEX($ A $ 20:$ A $ 151,MATCH(1,IF(DATE($ D $ 1,ROWS($ A $ 3:$ A3),B $ 2)> = $ B $ 20:$ B $ 151,IF( DATE($ D $ 1,ROWS($ A $ 3:$ A3),B $ 2)<= $ C $ 20:$ C $ 151,1)),0)),“”)

Is there some change possible in VBA(To avoid Formula) so it will store directly where it intend to show as per image MAX 3 people can go on leave same dates so max 3 initials in one cell as per date range eg VBA是否有一些可能的更改(避免公式),因此它将根据图像直接存储在打算显示的位置,最多3个人可以离开相同的日期,因此每个日期在一个单元格中最多可以有3个首字母缩写,例如

  • ABC(1 Jan to 5 Jan) DEF(3 Jan to 6 Jan) and XYZ(2 Jan to 7 Jan) ABC(1月1日至1月5日)DEF(1月3日至1月6日)和XYZ(1月2日至1月7日)
  • So In Cell for January 1 ABC So In Cell for 1月1日ABC
  • In Cell for January 2 ABC/XYZ 进入1月2日的单元格ABC / XYZ
  • In Cell for January 3 ABC/DEF/XYZ up to jan 5 进入1月3日的单元格,直到1月5日为ABC / DEF / XYZ
  • In Cell for January 6 DEF/XYZ 进入1月6日的单元格DEF / XYZ
  • In Cell for January 7 XYZ XYZ 1月7日在单元格中

Column width and height doesn't matter (big or small auto adjust) only want that data to be displayed on the calendar on respective dates range 列的宽度和高度无关紧要(自动调整大小),只希望该数据在各个日期范围内显示在日历上

卡

Here is a UDF which you would use as a formula on the sheet directly: 这是一个UDF,您可以将其直接用作图纸上的公式:

    Function vac(dt As Date, rngnme As Range, rngstrt As Range, rngend As Range) As String
    Application.Volatile
    Dim i&
    Dim temp As String
    temp = ""

    For i = 1 To rngnme.Rows.Count
        If rngnme.Cells(i, 1) <> "" Then
            If rngstrt.Cells(i, 1) <= dt And rngend.Cells(i, 1) >= dt Then
                temp = temp & rngnme.Cells(i, 1) & ","
            End If
        End If
    Next i

    If temp = "" Then
        vac = CVErr(xlErrNA)
    ElseIf Len(temp) - Len(Replace(temp, ",", "")) > 3 Then
        vac = "> 3"
    Else
        vac = Left(temp, Len(temp) - 1)
    End If

End Function

Post this in a module not sheet code or thisworkbook code. 将此发布到模块而不是工作表代码或本工作簿代码中。 Then you would call it by placing the following formula in B3: 然后,您可以通过在B3中放置以下公式来调用它:

=IFERROR(vac(DATE($D$1,ROWS($A$3:$A3),B$2),$A$20:$A$30,$B$20:$B$30,$C$20:$C$30),"")

Then copy over and down. 然后上下复制。

It will return "" if there is nobody assigned that day, or it will return the name separated by a comma, or if there are more than three it will return "> 3". 如果当天没有分配任何人,则返回“”,或者返回用逗号分隔的名称,如果超过三个,则返回“> 3”。

You could then apply a conditional format rule" =B3="> 3" and make it red or some other color to make it more visible. 然后,您可以应用条件格式规则“ =B3="> 3" ,并将其设置为红色或其他某种颜色以使其更加可见。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM