gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 09:31:52 25 4
gpt4 key购买 nike

我想创建一个 VBA 宏,用时间格式(正则表达式)的文本字符串替换工作表中的所有单元格:

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

带有单元格地址和工作表名称。我认为调用也会类似:

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

但我希望我可以将“What:=”参数设置为正则表达式,或者至少限制为时间格式。

我该如何解决这个问题?

<小时/>

测试数据:以 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

最终宏将删除所有零时间,并用计算公式的静态文本替换其他时间=cell("filename")&"!"&cell("address")

<小时/>

对上述输入文件执行操作的结果(我会将工作表保存为 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]

为了简洁起见,我去掉了 =cell("filename") 函数返回的目录和文件名,尽管上面是我真正的内容愿意。

最佳答案

我已经更新了之前托管的代码 here

  1. 删除用户所选范围内 '00:00 的所有文本字段
  2. 将任何文本“时间字段”替换为完整路径

(注意:最终,正则表达式是多余的,因为对于给定的实际数据格式,0.0 和 1.0 之间的值的单元格测试就足够了)

before after

    '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

关于regex - 如何在 VBA Excel 宏中进行正则表达式搜索和替换?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8638518/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com