0

如何搜索特定名称标题列“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
4

1 回答 1

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)
于 2013-10-27T13:25:24.203 回答