我是VBA的新手,我正在尝试编写一个宏,该宏将在我的数据中找到包含日期的列,并选择旁边的列。
日期类似于:
Date output
01/02/2011 200
02/02/2011 200
03/02/2011 200
我试过了:
Sub Macro5()
With ActiveSheet.Cells
.Find(what:=vbDate).Select
End With
ActiveCell.Offset(0, 1).Select
ActiveCell.EntireColumn.Select
End Sub
但它没有按预期工作。 请问有人给我一个解决方案吗?
答案 0 :(得分:3)
这是一个搜索格式
的演示Sub Demo()
Dim rng As Range
Dim rngDate As Range
' Set reference to search range by whatever means
Set rng = ActiveSheet.UsedRange
' Setup search
Application.FindFormat.Clear
Application.FindFormat.NumberFormat = "m/d/yyyy"
' Do search
Set rngDate = rng.Find(What:="*", SearchFormat:=True)
' report result
If Not rngDate Is Nothing Then
MsgBox "Found a Date formated cell in column " & rngDate.Column
End If
End Sub
答案 1 :(得分:1)
以下代码搜索活动工作表中的日期。一找到它,就会选择旁边的列并停止。
Dim rng As Range
For Each rng In ActiveSheet.UsedRange
If IsDate(rng.Value) Then
rng.Offset(0, 1).EntireColumn.Select
Exit Sub
End If
Next
答案 2 :(得分:1)
这是一个例子,它在第2行搜索格式化为日期的单元格(假设任何日期至少包含日期,即字符串d
):
Dim i As Long
With Sheet1.Range("2:2") ' or wherever you want to search
For i = 1 To .Columns.Count
With .Cells(1, i)
If InStr(.NumberFormat, "d") <> 0 Then
.Offset(0, 1).EntireColumn.Select
Exit For
End If
End With
Next i
End With