搜索特定名称标题列

时间:2013-10-27 13:03:38

标签: excel-vba excel-2010 vba excel

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

1 个答案:

答案 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)