简体   繁体   English

在日期中搜索一行,确定开始日期和结束日期,合并从开始日期到结束日期的行中的单元格

[英]Search a row a dates, identify a start date and end date, merge cells in row from start date to end date

I have searched high and low for examples to guide me in the development of a calendar spreadsheet. 我搜索了很多示例,以指导我开发日历电子表格。 The customer has very specific guidelines. 客户有非常具体的准则。

  1. First 11 columns contain event details are setup as filters 前11列包含事件详细信息,将其设置为过滤器
  2. Row 1 is currently blank, but will be used for heading 第1行目前为空白,但将用于标题
  3. Row 2 from column K to infinity (BXY) is the year 从K列到无穷(BXY)的第2行是年份
  4. Row 3 from column K to BXY is the month 从K列到BXY的第3行是月份
  5. Row 4 from column K to BXY is the day 从K列到BXY的第4行是一天

Other notes: Cell K6 has the Date 1-Jul-18. 其他说明:单元格K6的日期为18年7月1日。 All cells in row 4 continue as =K$6+1..etc. 第4行中的所有单元格继续为= K $ 6 + 1..etc。 The week is calculated with the formula: 使用以下公式计算周数:

=CONCATENATE("WEEK ", IF((IF(MONTH(K6)>=10, WEEKNUM(K6)-WEEKNUM(DATE(YEAR(K6),10,1))+1, 53-WEEKNUM(DATE(YEAR(K6)-1,10,1))+WEEKNUM(K6)))=53,1,(IF(MONTH(K6)>=10, WEEKNUM(K6)-WEEKNUM(DATE(YEAR(K6),10,1))+1,53-WEEKNUM(DATE(YEAR(K6)-1,10,1))+WEEKNUM(K6)))))

All cells in row 2 are calculated: 计算第2行中的所有像元:

=YEAR($K$6)

Now to my dilemma. 现在到我的困境。 I am working on a userform for the user to input all data that populates the next empty row in the spreadsheet. 我正在为一个用户窗体工作,以便用户输入填充电子表格中下一个空行的所有数据。 I can not post the code that does that, as it is on a proprietary computer system. 我无法发布执行此操作的代码,因为它在专用计算机系统上。 But, that code that populates the filter range A through J works fine. 但是,填充过滤器范围A到J的代码可以正常工作。 This code finds the next empty row: 此代码查找下一个空行:

lastRow = ws.Cells.Find(What:="*", LookIn=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row + 1

As part of the form the user inputs the start date and end date of an event. 作为表单的一部分,用户输入事件的开始日期和结束日期。 I am trying to search row 4 for the start date and end date, merge all cells between the start date and end date and insert the event title into the merged cell. 我正在尝试在第4行中搜索开始日期和结束日期,合并开始日期和结束日期之间的所有单元格,然后将事件标题插入合并的单元格中。

This is my code so far for searching Row 4: 到目前为止,这是我用于搜索第4行的代码:

For Each eventDate In .Range("K$4:BXY$4)
If IsDate(eventDate.Value) = IsDate(Me.tbStartDate.Value) Then
.Cells(lastRow, eventDate.Address(RowAbsolute:=True, ColumnAbsolute:=False)).Value = Me.tbEventName.Value
End If
Next eventDate

I am new to excel programming, and really only a hobbyist programmer. 我是excel编程的新手,实际上只是一个业余程序员。 I was given this task at work and have been reading and researching examples for what I am trying to do...to no avail. 在工作中,我被赋予了这项任务,并且一直在阅读和研究我要尝试做的事的例子……无济于事。

I am looking at modifying this snippet to work: 我正在修改此代码段以使其工作:

For iCounter = myLastRow To myFirstRow Step -1
If .Cells(iCounter, myCriteriaColumn).Value = myCriteria Then 
.Range(.Cells(iCounter, myFirstColumn), .Cells(iCounter, myLastColumn)).Merge
Next iCounter

with this: 有了这个:

LastCol = sh.Cells.Find(What:="*", After:=sh.Range("A1"), Lookat:=xlPart, LookIn:=xlFormulas, SearchOrder:=xlByColumns, SearchDirection:=xlPrevious, MatchCase:=False).Column

Any assistance and guidance on how to accomplish this will be much appreciated. 任何有关如何完成此工作的帮助和指导将不胜感激。

v/r Dusty v / r尘土飞扬

I figured it out, probably not the best solution, but it works: 我想通了,可能不是最好的解决方案,但是它可行:

lastRow = .Cells.Find(What:="*",_
    LookIn:=xlFormulas,_
    LookAt:=xlPart,_
    SearchOrder:=xlByRows,_
    SearchDirection:=xlPrevious).Row + 1
eventName = Me.tbID.Value + " " +_
    Me.tbOpName + " " +_
    Me.tbStartDate.Value + "-" +_
    Me.tbEndDate.Value
startDate = Format(Me.tbStartDate.Value, "dd-mmm-yyyy;@")
endDate = Format(Me.tbEndDate.Value, "dd-mmm-yyyy;@")

For startCol = 14 to 959
    startDateColumn = Format(.Cells(6, startCol).Value, "dd-mmm-yyyy;@")

    If StrComp(startDate, startDateColumn, vbTextCompare) = 0 Then
        For endCol = 14 to 959
            endDateColumn = Format(.Cells(6, endCol).Value, "dd-mmm-yyyy;@")
            If StrComp(endDate, endDateColumn, vbTextCompare) = 0 Then
                .Range(.Cells(lastRow, startCol), .Cells(lastRow, endCol)).Merge
                .Cells(lastRow, startCol).Value = eventName
                Exit For
            End If
        Next endCol
     End If
Next startCol

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

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