2016-06-28 54 views
1

我用1200行搜索数据库,我想在充满单元的单元格中搜索一小时,并将特定小时复制到另一个单元格。在单元格中搜索一小时,并将小时复制到VBA中的其他单元格

为为例,在单元格我有一个数据是这样的: “我迫不及待地想在公共汽车站为6:45平顺性和乘坐公共汽车did'nt停止”

我”什么得到的是这样的:

Sub Find() 
irow = 2 
    Do Until IsEmpty(Cells(irow, 34)) 
     If Cells(irow, 34).Value Like "*[0-23]:[0-59]*" = True Then 
      Cells(irow, 34).Value = Cells(irow, 37).Value 
      irow = irow + 1 
     Else 
      irow = irow + 1 
     End If 
    Loop 
End Sub 

谢谢!

回答

0

以下代码在文本中查找时间戳,并将其写入同一行的单独列中。但是,它假定只有一个“[数字] [数字]:[数字] [数字]”序列存在。如果您的输入可能有多个您可能需要一些额外的筛选条件。

但首先您需要确保在VBA项目中激活正则表达式。 (见here

Sub Find() 
    my_row = 1 
    my_column = 1 
    Dim regEx As New RegExp 
    regEx.Pattern = "\d*\d:\d\d" 
    regEx.IgnoreCase = True 'True to ignore case 
    regEx.Global = True 'True matches all occurances, False matches the first occura 
    Do Until IsEmpty(Cells(my_row, my_column)) 
     If regEx.Test(Cells(my_row, my_column)) Then 
      Debug.Print ("Found something") 
      Dim matches 
      Set matches = regEx.Execute(Cells(my_row, my_column)) 
      If matches.Count = 1 Then 
       Cells(my_row, my_column + 2).Value = matches(0).Value 
      Else 
       Debug.Print ("Warning more than one match found") 
      End If 
     Else 
      Debug.Print ("Nothing found") 
     End If 
     my_row = my_row + 1 
    Loop 
End Sub 

我用下面的行测试代码:

I wait to the bus in the bus a92ohr2902 stop for the ride of 6:58 and the ride did'nt stop 
I wait to the bus in the bus ;3;23576;80-934 stop for the ride of 6:59 and the ride did'nt stop 
I wait to the bus in the bus 2016-06-01 stop for the ride of 14:00 and the ride did'nt stop 
I wait to the bus in the bus 9023845 stop for the ride of 14:01 and the ride did'nt stop 
I wait to the bus in the bus ;3;23576;80-934 stop for the ride of 20:50 and the ride did'nt stop 
I wait to the bus in the bus 2016-06-01 stop for the ride of 20:59 and the ride did'nt stop 
I wait to the bus in the bus 9023845 stop for the ride of 21:00 and the ride did'nt stop 
I wait to the bus in the bus a92ohr2902 stop for the ride of 21:01 and the ride did'nt stop 
+0

它看起来不错,但如果我已经在特定的单元格其他号码?像这样:“我在公共汽车站等待6点57分乘坐公共汽车254,并且乘车没有停止” – user3306637

+0

@ user3306637没错,在这种情况下,代码由[Mrig]提供(http:// stackoverflow .com/a/38072190/637466)似乎更好地将时间戳与其他数字分开。 – aPhilRa

+0

Mrig的代码不起作用。你的代码工作很好,只需要检查这个问题。 – user3306637

1

而不是

If Cells(irow, 34).Value Like "*[0-23]:[0-59]*" = True Then 

尝试

If Cells(irow, 34).Value Like "*#:##*" Then 

您还可以使用以下代码:

Sub Find() 
    Dim i As Integer 
    Dim arr() As String 

    irow = 2 
    Do Until IsEmpty(Cells(irow, 34)) 
     arr = Split(Cells(irow, 34), " ") 
     For i = LBound(arr) To UBound(arr) 
      If IsTime(arr(i)) Then 
       'to get the hour 
       MsgBox Left(arr(i), Application.WorksheetFunction.Find(":", arr(i)) - 1) 
       Cells(irow, 34).Value = Cells(irow, 37).Value 
       Exit For 
      End If 
     Next 
     irow = irow + 1 
    Loop 
End Sub 

Function IsTime(strData As String) As Boolean 
    On Error Resume Next 
    IsTime = TimeValue(strData) 
End Function 
相关问题