给定2个日期时查找单元格范围

时间:2019-09-30 14:31:57

标签: excel vba

我有一张数字从1到10的表格(从D2到M2)

假设在 A1 中有 2019/03/09

并且 B1 中有 2019/09/09

C1 中,有你好

第A列中,我有多个单词,从 A3到A10

以下是Excel表的示例

enter image description here

我想做的是:列中搜索学生一词,当我找到它时,从 A1-> 3  和 A2-> 6 ,并在找到的单词 Student < / strong>

所以我的输出将是:

enter image description here

到目前为止,这是我的代码:

Dim Cell As Range
Columns("A:A").Select 
Set Cell = Selection.Find(What:="Student", After:=ActiveCell, LookIn:=xlFormulas, _
    LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
    MatchCase:=False, SearchFormat:=False)

If Cell Is Nothing Then
    MsgBox "Word not found"

Else
    MsgBox "Word found"
End If

基本上我可以找到学生一词,但不知道如何在 3到6 之间的单元格中写你好 / p>

3 个答案:

答案 0 :(得分:5)

有关以下代码的一些注释(未经测试!)。

1)使用VBA时,请始终尝试使用工作表限定符。这样可以使代码更简洁,减少不必要错误的空间

2)使用library(data.table) library(lubridate) charger_id <- c(1, 1, 2, 3, 4, 4) start_time <- as_datetime(c("2019-06-13 10:56:36", "2019-06-13 15:56:36", "2019-06-13 17:55:56", "2019-06-13 08:55:27", "2019-06-13 04:25:56", "2019-06-13 12:45:47")) end_time <- as_datetime(c("2019-06-13 14:08:10", "2019-06-14 10:15:46", "2019-06-13 23:20:31", "2019-06-13 17:50:51", "2019-06-13 09:20:16", "2019-06-13 19:20:06")) df <- data.table("charger_id" = charger_id, "start_time" = start_time, "end_time" = end_time) setkey(df, start_time, end_time) start <- as_datetime('2019-06-12') end <- as_datetime('2019-06-14') times <- seq.POSIXt(start, end, by = 'hour') df_times <- data.table(start = times[-length(times)], end = times[-1]) setkey(df_times, start, end) df <- foverlaps(df, df_times, by.x = c('start_time', 'end_time'), by.y = c('start', 'end')) df[, time_occupied := as.double(difftime(min(.SD[,c(end, end_time)]), max(.SD[,c(start, start_time)]), units = 'mins')), .(start, charger_id)] df[order(charger_id), .(charger_id, start, time_occupied)] #> charger_id start time_occupied #> 1: 1 2019-06-13 10:00:00 3.400000 #> 2: 1 2019-06-13 11:00:00 60.000000 #> 3: 1 2019-06-13 12:00:00 60.000000 #> 4: 1 2019-06-13 13:00:00 60.000000 #> 5: 1 2019-06-13 14:00:00 8.166667 #> 6: 1 2019-06-13 15:00:00 3.400000 #> 7: 1 2019-06-13 16:00:00 60.000000 #> 8: 1 2019-06-13 17:00:00 60.000000 #> 9: 1 2019-06-13 18:00:00 60.000000 #> 10: 1 2019-06-13 19:00:00 60.000000 #> 11: 1 2019-06-13 20:00:00 60.000000 #> 12: 1 2019-06-13 21:00:00 60.000000 #> 13: 1 2019-06-13 22:00:00 60.000000 #> 14: 1 2019-06-13 23:00:00 60.000000 #> 15: 2 2019-06-13 17:00:00 4.066667 #> 16: 2 2019-06-13 18:00:00 60.000000 #> 17: 2 2019-06-13 19:00:00 60.000000 #> 18: 2 2019-06-13 20:00:00 60.000000 #> 19: 2 2019-06-13 21:00:00 60.000000 #> 20: 2 2019-06-13 22:00:00 60.000000 #> 21: 2 2019-06-13 23:00:00 20.516667 #> 22: 3 2019-06-13 08:00:00 4.550000 #> 23: 3 2019-06-13 09:00:00 60.000000 #> 24: 3 2019-06-13 10:00:00 60.000000 #> 25: 3 2019-06-13 11:00:00 60.000000 #> 26: 3 2019-06-13 12:00:00 60.000000 #> 27: 3 2019-06-13 13:00:00 60.000000 #> 28: 3 2019-06-13 14:00:00 60.000000 #> 29: 3 2019-06-13 15:00:00 60.000000 #> 30: 3 2019-06-13 16:00:00 60.000000 #> 31: 3 2019-06-13 17:00:00 50.850000 #> 32: 4 2019-06-13 04:00:00 34.066667 #> 33: 4 2019-06-13 05:00:00 60.000000 #> 34: 4 2019-06-13 06:00:00 60.000000 #> 35: 4 2019-06-13 07:00:00 60.000000 #> 36: 4 2019-06-13 08:00:00 60.000000 #> 37: 4 2019-06-13 09:00:00 20.266667 #> 38: 4 2019-06-13 12:00:00 14.216667 #> 39: 4 2019-06-13 13:00:00 60.000000 #> 40: 4 2019-06-13 14:00:00 60.000000 #> 41: 4 2019-06-13 15:00:00 60.000000 #> 42: 4 2019-06-13 16:00:00 60.000000 #> 43: 4 2019-06-13 17:00:00 60.000000 #> 44: 4 2019-06-13 18:00:00 60.000000 #> 45: 4 2019-06-13 19:00:00 20.100000 #> charger_id start time_occupied 方法时,我使用.Find,因为如果您未明确定义此代码,则您的代码将使用在Excel中使用的最后一个已知方法。同样,显式定义保留了更少的错误余地。

3)尝试在编码时包含错误处理。这提供了“断点”,以便将来进行调试。

4)您可以使以下内容更具动态性。但是,我将由您自己决定如何做!

LookAt:=xlWhole

这只是解决问题的一种方法。希望这有助于您的理解!

答案 1 :(得分:2)

这里不需要循环-只需找到您的值并解析日期即可。假设要找到的值在Column A中存在,并且表从Column D开始,则列之间的明确关系为Day(date) + 3


Sub Test()

Dim ws As Worksheet: Set ws = ThisWorkbook.Sheets("Sheet1")
Dim lr As Long, Found As Range
Dim date_a As Long, date_b As Long

lr = ws.Range("A" & ws.Rows.Count).End(xlUp).Row

Set Found = ws.Range("A2:A" & lr).Find("Student", LookIn:=xlValues)

If Not Found Is Nothing Then
    date_a = Day(Range("A1")) + 3
    date_b = Day(Range("B1")) + 3

    With ws.Range(ws.Cells(Found.Row, date_a), ws.Cells(Found.Row, date_b))
        .Merge
        .Value = ws.Range("C1")
    End With

Else
    MsgBox "Value 'Student' Not Found"
End If

End Sub

答案 2 :(得分:1)

我已经尝试过了:

Dim ThisRow As Long

Dim FindWhat As String
FindWhat = "Student"

Dim MyStart As Byte
Dim MyEnd As Byte

MyStart = Day(Range("A1").Value) + 3 'we add +3 because starting 1 is in the fourth column
MyEnd = Day(Range("B1").Value) + 3 'we add +3 because starting 1 is in the fourth column

Dim SearchRange As Range
Set SearchRange = Range("A3:A10") 'range of values

With Application.WorksheetFunction
    'we first if the value exists with a count.
    If .CountIf(SearchRange, FindWhat) > 0 Then 'it means findwhat exists
        ThisRow = .Match(FindWhat, Range("A:A"), 0) 'we find row number of value

        Range(Cells(ThisRow, MyStart), Cells(ThisRow, MyEnd)).Value = Range("C1").Value
        Application.DisplayAlerts = False
        Range(Cells(ThisRow, MyStart), Cells(ThisRow, MyEnd)).Merge
        Application.DisplayAlerts = True
    Else
        MsgBox "Value 'Student' Not Found"
    End If
End With

请注意,我已经使用了工作表函数COUNTIF和MATCH。 MATCH将在范围内找到元素的位置,因此,如果您检查整列,它将告诉您行号。但是,如果找不到任何内容,则会出现错误。避免这种情况的简单方法是,首先使用COUNTIF计数该值是否在该范围内,如果存在,则可以安全地使用MATCH

另外,请注意,由于我们使用的是MATCH,此函数只会查找第一个重合,因此,如果您在A列中的值列表重复,则此方法将对您不起作用!

enter image description here