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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12978474/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 18:08:51  来源:igfitidea点击:

Excel Macro: Selecting a specific Row based on column date

vbaexcel-vbaexcel-2007excel

提问by BC00

I am writing my first macro and have a question on how I can select a specific Row based on a value in a specific column. here is my code so far:

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

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

回答by Scott Holtzman

replace 'Need to select the entire rowwith

替换'Need to select the entire row

cell.entirerow.select

cell.entirerow.select

UPDATEHere is a much more efficient way to get what you need without all the looping.

更新这是一种更有效的方法,无需所有循环即可获得所需内容。

In your code Replace from For Each cell ...to Nextwith this:

在您的代码中,将 from 替换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

You may need to provide some error checking in case you don't have clients going live within 30 days (SpecialCellsmethod will fail on this) and also, if M4 is not your column header, you may want to adjust how the range picks up the visible cells.

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