简体   繁体   English

如何在VBA excel宏中进行正则表达式搜索和替换?

[英]How can I do a regex search and replace in a VBA excel macro?

I'd like to create a VBA macro that replaces all cells in a worksheet with text strings in a time format (regular expression): 我想创建一个VBA宏,用时间格式(正则表达式)替换工作表中的所有单元格:

(1[0-2]|[1-9]):[0-5][0-9]:[0-5][0-9] [AP]M

with the cell address and worksheet name. 使用单元格地址和工作表名称。 I think the call will be akin too: 我认为这个电话也是如此:

 Cells.Replace What:="1:23:45 AM",    
    Replacement:="=cell(""filename"")&cell(""Address"")", _
    LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:= _
    False, ReplaceFormat:=False

But I'm hoping I can make the " What:= " argument a reg ex, or at least restricted to a time format. 但我希望我可以将“ What:= ”参数设为reg ex,或者至少限制为时间格式。

How would I go about this? 我该怎么做?


Test Data: Save the following in CSV format: 测试数据: 以CSV格式保存以下内容:

00:00,04:27,00:36,04:31,00:00
00:00,00:00,04:18,01:07,10:06
00:00,00:00,00:00,00:00,00:00

Eventually the macro will delete all the zero times, and replace the other times with static text that is the evaluated formula =cell("filename")&"!"&cell("address") 最终宏将删除所有零次,并用静态文本替换其他时间,静态文本是评估的公式=cell("filename")&"!"&cell("address")


Result of acting on the above input file (I would be saving the sheets as XLSX): 作用于上述输入文件的结果(我将表格保存为XLSX):

     [    A    ]   [     B     ]  [     C     ]  [     D     ]  [     E     ]
[1]                'Sheet1!$B$1   'Sheet1!$C$1   'Sheet1!$D$1
[2]                               'Sheet1!$C$2   'Sheet1!$D$2   'Sheet1!$E$2
[3]

For brevity, I stripped out the directory and file name that the =cell("filename") function returns, although the above is what I really would like. 为简洁起见,我删除了 =cell("filename") 函数返回 的目录和文件名 ,尽管上面是我真正想要的。

Since you are trying to replace a format, I'd do a replace based on a format. 由于您要替换格式,我会根据格式进行替换。 With a regex it seems like you'd be forced to deal with the underlying number. 使用正则表达式,似乎你被迫处理底层数字。

I tested this in XL 2003 and 2010: 我在XL 2​​003和2010中测试了这个:

Sub ReplaceByFormat()
With ActiveSheet.Cells
    .Replace What:="", Replacement:="=cell(""filename"")&cell(""Address"")", _
             SearchFormat:=True, _
             ReplaceFormat:=False, _
             LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False
    Application.FindFormat.NumberFormat = "h:mm AM/PM"
End With
End Sub

EDIT 编辑

First off I had a mistake above in placing the FindFormat at the end. 首先,我在最后放置FindFormat时遇到了错误。 It needs to be at the beginning (Doh). 它需要在开始(Doh)。

There is no OR argument to the replace function. 替换函数没有OR参数。 So below, I've just repeated the code for a second type of format. 下面,我刚刚重复了第二种格式的代码。

This code assumes that the dates are all constants. 此代码假定日期都是常量。 If they are formulas you could fix with a find and replace in the vba. 如果它们是公式,则可以使用vba中的查找和替换进行修复。 If they're a mix, you'll need to extend the code a bit: 如果它们是混合,你需要稍微扩展一下代码:

Sub ReplaceByFormat()

With ActiveSheet.Cells.SpecialCells(xlCellTypeConstants)
    Application.FindFormat.NumberFormat = "h:mm AM/PM"
    .Replace What:="", Replacement:="=cell(""filename"")&cell(""Address"")", _
             SearchFormat:=True, _
             ReplaceFormat:=False, _
             LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False
    Application.FindFormat.NumberFormat = "m/d/yyyy"
    .Replace What:="", Replacement:="=cell(""filename"")&cell(""Address"")", _
             SearchFormat:=True, _
             ReplaceFormat:=False, _
             LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False
End With
End Sub

I've updated my code formerly hosted here to 我已经更新了我以前的代码托管在这里 ,以

  1. Remove any text fields in a user selected range that are '00:00 删除用户选定范围内'00:00所有文本字段
  2. Replace any text "time fields" with the full path 用完整路径替换任何文本“时间字段”

(nb: In the end the Regex is overkill as a cell test for a value betwen 0.0 and 1.0 would suffice given the actual data format) (nb:最后,正则表达式过度,因为对于实际数据格式,0.0和1.0之间的值的单元测试就足够了)

之前后

    'Press Alt + F11 to open the Visual Basic Editor (VBE)
    'From the Menu, choose Insert-Module.
    'Paste the code into the right-hand code window.
    'Press Alt + F11 to close the VBE
    'In Xl2003 Goto Tools … Macro … Macros and double-click KillTime  


    Sub KillTime()
    Dim rng1 As Range
    Dim rngArea As Range
    Dim lngRow As Long
    Dim lngCol As Long
    Dim lngCalc As Long
    Dim objReg As Object
    Dim strSht As String
    Dim X()

    On Error Resume Next
    Set rng1 = Application.InputBox("Select range for the replacement of leading zeros", "User select", Selection.Address, , , , , 8)
    If rng1 Is Nothing Then Exit Sub
    On Error GoTo 0

    strSht = ActiveWorkbook.Path & "\[" & ActiveWorkbook.Name & "]" & rng1.Parent.Name
    'remove '00:00
    rng1.Replace "00:00", vbNullString, xlWhole

    'See Patrick Matthews excellent article on using Regular Expressions with VBA
    Set objReg = CreateObject("vbscript.regexp")
    objReg.Pattern = "^0\.\d+$"    
     'Speed up the code by turning off screenupdating and setting calculation to manual
      'Disable any code events that may occur when writing to cells
    With Application
        lngCalc = .Calculation
        .ScreenUpdating = False
        .Calculation = xlCalculationManual
        .EnableEvents = False
    End With

    'Test each area in the user selected range

    'Non contiguous range areas are common when using SpecialCells to define specific cell types to work on
    For Each rngArea In rng1.Areas
        'The most common outcome is used for the True outcome to optimise code speed
        If rngArea.Cells.Count > 1 Then
           'If there is more than once cell then set the variant array to the dimensions of the range area
           'Using Value2 provides a useful speed improvement over Value. On my testing it was 2% on blank cells, up to 10% on non-blanks
            X = rngArea.Value2
            For lngRow = 1 To rngArea.Rows.Count
                For lngCol = 1 To rngArea.Columns.Count
                   If objReg.test(X(lngRow, lngCol)) Then X(lngRow, lngCol) = strSht & rngArea.Cells(1).Offset(lngRow - 1, lngCol - 1).Address(0, 0)
                Next lngCol
            Next lngRow
            'Dump the updated array back over the initial range
            rngArea.Value2 = X
        Else
            'caters for a single cell range area. No variant array required
               If objReg.test(rngArea.Value) Then rngArea.Value = strSht & rngArea.Address(0, 0)            
        End If
    Next rngArea

    'cleanup the Application settings
    With Application
        .ScreenUpdating = True
        .Calculation = lngCalc
        .EnableEvents = True
    End With

    Set objReg = Nothing
    End Sub

Firstly, a better pattern would be: 首先,更好的模式是:

  /[0-2]?[0-9]:[0-5][0-9]:[0-5][0-9] [A|P]M/

Secondly, the actual code without anything specific to your problem would be as simple as (adapting to your scenario, off course): 其次,没有任何特定于您的问题的实际代码将如此简单(适应您的场景,偏离课程):

Set RegExp= CreateObject("VBScript.RegExp")
RegExp.Pattern = "[0-2]?[0-9]:[0-5][0-9]:[0-5][0-9] [A|P]M"
For i = ......
  Expr = Format(ActiveSheet.Cells(i, 1).Value, ActiveSheet.Cells(i, 1).NumberFormat)
  If RegExp.Test(Expr) Then Replace....
Next i

This way VBA will treat the values in the cells as they appear. 这样,VBA会在出现时处理单元格中的值。

EDIT 编辑

About the pattern also matching "29:00:00 |M" - I'm not sure why "|" 关于模式也匹配“29:00:00 | M” - 我不知道为什么“|” is considered a valid char since it means "OR". 被认为是有效的char,因为它意味着“OR”。 The same happens if you use ",". 如果你使用“,”也会发生同样的情况。 Anyway, a better one surely is: 无论如何,一个更好的肯定是:

/^(([0-1]?[0-9])|(2[0-4])):[0-5][0-9]:[0-5][0-9] [A|P]M$/
  • Allows 0-24 hours only 仅允许0-24小时
  • "^" and "$" makes sure the cell contains only time format value in it, denoting begining and end of the string “^”和“$”确保单元格中只包含时间格式值,表示字符串的开头和结尾

But in the end, it doesn't matter if the RegExp pattern matches EXACTLY only time values, because the formating of the input data will be done with Excel, wich will pre-validate the cell content (if you type "29:00:00" it will convert it to 5 AM of the next day). 但最后,如果RegExp模式仅与时间值完全匹配并不重要,因为输入数据的格式化将通过Excel完成,这将预先验证单元格内容(如果键入“29:00: 00“它将它转换为第二天的凌晨5点。” This being an Excel solution can lead to an Excel solution only, but not a global solution. 这是一个Excel解决方案,只能导致Excel解决方案,而不是全局解决方案。

In that sense, using regular expression is not even common in Excel - RegExp is a string tester, without semantic meaning evaluation, for which you have other means to validate inputs in this context. 从这个意义上说,使用正则表达式在Excel中甚至不常见 - RegExp是一个字符串测试器,没有语义含义评估,您可以使用其他方法在此上下文中验证输入。 For instance, you could do the same with pure VBA: 例如,您可以使用纯VBA执行相同操作:

Function IsTime(rng As Range) As Boolean
  Dim sValue As String
  sValue = rng.Cells(1).Text
  On Error Resume Next
  IsTime = IsDate(TimeValue(sValue))
  On Error GoTo 0
End Function
'Source: http://excel.tips.net/T003292_Checking_for_Time_Input.html

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

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