Excel 查找关键字并从同一字段中刮取文本以转换为日期

Excel 查找关键字并从同一字段中刮取文本以转换为日期,excel,parsing,excel-formula,vba,Excel,Parsing,Excel Formula,Vba,我正在尝试创建一个Excel函数,该函数将在定义的列中的任何行中查找关键字,然后将同一字段中的文本(以dd/mm/yy格式)刮取,并将其转换为新列中的日期 字段数据示例[“关键字”,13/10/17] 在Excel中可以这样做吗?是否需要VBA 感谢使用数据字段数组的示例: 始终使用代码模块声明头中的选项Explicit表达式声明变量。程序代码向您展示了一种使用数据字段数组而不是在范围内循环的快速方法。您可以通过以下示例代码轻松地将范围值设置为变量数组: Dim a ' vari

我正在尝试创建一个Excel函数,该函数将在定义的列中的任何行中查找关键字,然后将同一字段中的文本(以dd/mm/yy格式)刮取,并将其转换为新列中的日期

字段数据示例[“关键字”,13/10/17]

在Excel中可以这样做吗?是否需要VBA


感谢使用数据字段数组的示例:

始终使用代码模块声明头中的
选项Explicit
表达式声明变量。程序代码向您展示了一种使用数据字段数组而不是在范围内循环的快速方法。您可以通过以下示例代码轻松地将范围值设置为变量数组:

Dim a         ' variant
a = ThisWorkbook.Range("A2:A4711").value
这样你可以加快搜索速度。请记住,VBA然后会自动创建一个维度为2的基于一的数组

下面的步骤是什么?

  • 在col A中搜索“关键字”
  • 获取列B的字符串(“13/10/17”),转换为日期和
  • 将日期写入工作表测试中的C列
测试呼叫

Option Explicit           
'注意:将选项显式写入代码模块的声明头中

Sub TestCall()
' Example
  writeKeyDate "Keyword", "A", "B", "C", "Test"
End Sub
程序代码

Sub writeKeyDate(ByVal sKey As String, _
                 ByVal sCol As String, ByVal sCol2 As String, ByVal sCol3 As String, _
                 Optional ByVal wsName As String = "Test")
'     sKey .... search string
'     sCol .... character of column where to search
'     sCol2 ... character of column with datestring
'     sCol3 ... character of target column
'     wsName .. worksheet name as string, e.g. "MySheet", or "Test"
'               (if not set, then automatically "Test")
' declare vars
  Dim oSht    As Worksheet                       ' work sheet
  Dim a       As Variant                         ' one based 2-dim data field array
  Dim i       As Long                            ' rows
  Dim n       As Long                            ' last row
  Dim sDate   As String                          ' date string in sCol2
' set sheet
  Set oSht = ThisWorkbook.Worksheets(wsName)     ' fully qualified reference to worksheet
' get last row number of search column
  n = oSht.Range(sCol & oSht.Rows.Count).End(xlUp).Row
  If n < 2 Then Exit Sub                         ' only if data avaible (row 1 assumed as head line)

' get range values to one based 2dim data field array
  a = oSht.Range(sCol & "2:" & sCol & n).Value    '  array gets data from e.g. "A2:A100"
' loop through column sCol to find keyword sKey
  For i = LBound(a) To UBound(a)              '  array boundaries counting from 1 to n -1 (one off for title line)
      ' searchstring found
        If LCase(a(i, 1)) = LCase(sKey) Then  '  case insensitive
           sDate = oSht.Range(sCol2 & i + 1).Value2
           On Error Resume Next
           If Len(Trim(sDate)) > 0 Then
              oSht.Range(sCol3 & i + 1).Value = CDate(sDate)
           End If
        End If
  Next

End Sub
  • c) 如果希望搜索区分大小写,则必须按如下方式更改代码:

    如果a(i,1)=sKey
    而不是
    如果LCase(a(i,1))=LCase(sKey)

  • 祝你好运

    ============================================

    编辑同一列中单元格内的搜索和数据示例(冒号分隔)

    在一列中搜索的编辑过程

    Sub writeKeyDate1(ByVal skey As String, _
                     ByVal sCol As String, ByVal sCol2 As String, _
                     Optional ByVal wsName As String = "Test")
    '     sKey .... search string
    '     sCol .... character of column where to search (includes key, date string)
    '     sCol2 ... character of target column
    '     wsName .. worksheet name as string, e.g. "MySheet", or "Test"
    '               (if not set, then automatically "Test")
    ' declare vars
      Dim oSht    As Worksheet                       ' work sheet
      Dim a       As Variant                         ' one based 2-dim data field array
      Dim i       As Long                            ' rows
      Dim n       As Long                            ' last row
      Dim s       As String
      Dim sDate   As String                          ' date string in sCol2
    ' set sheet
      Set oSht = ThisWorkbook.Worksheets(wsName)     ' fully qualified reference to worksheet
    ' get last row number of search column
      n = oSht.Range(sCol & oSht.Rows.Count).End(xlUp).Row
      If n < 2 Then Exit Sub                         ' only if data avaible (row 1 assumed as head line)
    
    ' get range values to one based 2dim data field array
      a = oSht.Range(sCol & "2:" & sCol & n).Value    '  array gets data from e.g. "A2:A100"
    ' loop through column sCol to find keyword sKey
      For i = LBound(a) To UBound(a)              '  array boundaries counting from 1 to n -1 (one off for title line)
            s = Split(LCase(a(i, 1)) & "", ",")(0)
          ' searchstring found
            If InStr(LCase(s), LCase(skey)) > 0 Then
               sDate = Trim(Split(LCase(a(i, 1)) & ",", ",")(1))
               On Error Resume Next
               If Len(sDate) > 0 Then
                  oSht.Range(sCol2 & i + 1).Value = CDate(sDate)
               End If
            End If
      Next
    
    End Sub
    
    Sub-writeKeyDate1(ByVal skey作为字符串_
    ByVal sCol作为字符串,ByVal sCol2作为字符串_
    可选的ByVal wsName As String=“Test”)
    “斯凯。。。。搜索字符串
    “斯科尔。。。。要搜索的列的字符(包括键、日期字符串)
    “sCol2。。。目标列的特征
    “wsName…”。。工作表名称为字符串,例如“MySheet”或“Test”
    '(如果未设置,则自动“测试”)
    “申报vars
    将oSht标注为工作表的工作表
    Dim a作为变型“基于1的2维数据场阵列”
    我喜欢长长的一排
    与最后一排一样长
    像线一样变暗
    在sCol2中,Dim sDate作为字符串的日期字符串
    "定位表"
    设置oSht=ThisWorkbook.Worksheets(wsName)对工作表的完全限定引用
    '获取搜索列的最后一行号
    n=oSht.Range(sCol&oSht.Rows.Count)。End(xlUp)。Row
    如果n<2,则仅当数据可用时才退出Sub(第1行假定为首行)
    '将范围值获取到基于1的2dim数据字段数组
    a=oSht.Range(sCol&“2:”&sCol&n)。值数组从例如“A2:A100”获取数据
    '循环通过列sCol查找关键字sKey
    对于i=LBound(a)到UBound(a)'的数组边界,从1到n-1计数(标题行为一个)
    s=拆分(LCase(a(i,1))和“,””(0)
    '找到搜索字符串
    如果InStr(LCase,LCase(skey))>0,则
    sDate=修剪(拆分(LCase(a(i,1))和“,”,“,”,”(1))
    出错时继续下一步
    如果Len(sDate)>0,则
    oSht.范围(sCol2&i+1)。值=CDate(sDate)
    如果结束
    如果结束
    下一个
    端接头
    
    是否会有多个匹配项?您好,不只是查找一个关键字。我当然理解如何做到这一点,但不确定在VBA上如何将文本转换为新行中的日期。最终的想法是将该日期用于透视表。添加了一个使用数据字段数组而不是纯(慢速)范围循环的答案,以帮助其他用户阅读此问题。欢迎这样做,但是请考虑下一次,当您在这里请求时,您希望自己做一些最小的编码工作。我已经注意到,您正在解析由2个部分组成的列文本,而不是从另一列获得日期字符串。等一下,我编辑的答案。谢谢。这太棒了。如果日期和关键字在同一个字段中呢?用逗号分隔,例如关键字,2017年10月13日添加了“writeKeyDate1”过程,允许在同一列单元格中解析关键字和日期。@SDROB,我截至11月的编辑是否解决了您在注释中的其他问题,以便您可以将您的操作标记为已接受?
    Sub TestCall1()
    ' Example
      writeKeyDate1 "Keyword", "A", "B", "Test"
    End Sub
    
    Sub writeKeyDate1(ByVal skey As String, _
                     ByVal sCol As String, ByVal sCol2 As String, _
                     Optional ByVal wsName As String = "Test")
    '     sKey .... search string
    '     sCol .... character of column where to search (includes key, date string)
    '     sCol2 ... character of target column
    '     wsName .. worksheet name as string, e.g. "MySheet", or "Test"
    '               (if not set, then automatically "Test")
    ' declare vars
      Dim oSht    As Worksheet                       ' work sheet
      Dim a       As Variant                         ' one based 2-dim data field array
      Dim i       As Long                            ' rows
      Dim n       As Long                            ' last row
      Dim s       As String
      Dim sDate   As String                          ' date string in sCol2
    ' set sheet
      Set oSht = ThisWorkbook.Worksheets(wsName)     ' fully qualified reference to worksheet
    ' get last row number of search column
      n = oSht.Range(sCol & oSht.Rows.Count).End(xlUp).Row
      If n < 2 Then Exit Sub                         ' only if data avaible (row 1 assumed as head line)
    
    ' get range values to one based 2dim data field array
      a = oSht.Range(sCol & "2:" & sCol & n).Value    '  array gets data from e.g. "A2:A100"
    ' loop through column sCol to find keyword sKey
      For i = LBound(a) To UBound(a)              '  array boundaries counting from 1 to n -1 (one off for title line)
            s = Split(LCase(a(i, 1)) & "", ",")(0)
          ' searchstring found
            If InStr(LCase(s), LCase(skey)) > 0 Then
               sDate = Trim(Split(LCase(a(i, 1)) & ",", ",")(1))
               On Error Resume Next
               If Len(sDate) > 0 Then
                  oSht.Range(sCol2 & i + 1).Value = CDate(sDate)
               End If
            End If
      Next
    
    End Sub