如何搜索特定名称标题栏“DATA / HORA”并适应下面的宏?
Sub Data()
Dim cell As Range
Dim lastRow As Long
lastRow = Range("A" & Rows.Count).End(xlUp).Row
For Each cell In Range("A1:A" & lastRow)
If InStr(cell.Value, "-") <> 0 Then
cell.Value = RegexReplace(cell.Value, _
"(\d{4})\-(\d{2})\-(\d{2})", "$3/$2/$1")
End If
cell.NumberFormat = "dd/mm/yyyy;@"
Next
End Sub
Function RegexReplace
------
End Function
答案 0 :(得分:1)
替换:
lastRow = Range("A" & Rows.Count).End(xlUp).Row
For Each cell In Range("A1:A" & lastRow)
使用:
Dim ColLetr As String
For i = 1 To Columns.Count
If Cells(1, i) = "DATA/HORA" Then
ColLetr = Split(Cells(1, i).Address, "$")(1)
End If
Next
lastRow = Range(ColLetr & Rows.Count).End(xlUp).Row
For Each cell In Range(ColLetr & "1:" & ColLetr & lastRow)
修改#1 强>:
解决评论:
Dim ColLetr As String
For i = 1 To Columns.Count
If Cells(1, i) = "DATA/HORA" Then
ColLetr = Split(Cells(1, i).Address, "$")(1)
Exit For
End If
Next
If ColLetr = "" Then
MsgBox "DATA/HORA not found"
Exit Sub
End If
lastRow = Range(ColLetr & Rows.Count).End(xlUp).Row
For Each cell In Range(ColLetr & "1:" & ColLetr & lastRow)