Excel宏:根据列日期选择特定行

时间:2012-10-19 16:26:06

标签: vba excel-vba excel-2007 excel

我正在编写我的第一个宏,并且有一个关于如何根据特定列中的值选择特定行的问题。这是我目前的代码:

Sub Pipeline()

'Module 3
'Iterating through the Funding Date Column and looking for clients going live within 30 days
'Selecting the rows for each client in that target range
'TODO: Export information into an email template in Outlook
'TODO: Send email to distribution list


Dim fundingDate As range
Set fundingDate = range("M4:M500")

Dim todaysDate As Date
todaysDate = Date

For Each cell In fundingDate
  If cell < todaysDate + 30 Then
   'Need to select the entire row
  Else
  cell.Font.ColorIndex = 3
End If
Next

End Sub

1 个答案:

答案 0 :(得分:4)

替换'Need to select the entire row

cell.entirerow.select

<强>更新 这是一种更有效的方法,可以在没有所有循环的情况下获得所需的内容。

在您的代码中使用以下代码从For Each cell ...替换为Next

With fundingDate    
    .AutoFilter 1, "<" & todaysDate + 30        
    .SpecialCells(xlCellTypeVisible).Select 
    'here are your clients going live in next 30 days    
    .AutoFilterMode = False    
End With

您可能需要提供一些错误检查,以防您在30天内没有客户端上线(SpecialCells方法将失败)此外,如果M4不是您的列标题,您可能需要调整范围如何吸收可见细胞。