根据列中的日期隐藏行

时间:2018-08-06 19:48:13

标签: excel vba excel-vba

我已经搜索并搜索了互联网和所有论坛,并且一直在拼凑代码,但仍然无法弄清楚。我已经尝试过For循环和For Each循环,但仍然无法解决问题。在工作表中,所有日期都在 D列中。我想按月隐藏行。我希望能够单击一个宏按钮,并且只显示1月,2月等的日期。

这是我目前拥有的:

Sub January()
'
'
'
Dim cell As Range
For Each cell In Range("Date")
    If cell.Value = "" Then
        cell.EntireRow.Hidden = False
    End If
    If cell.Value < "1/1/2018" Or cell.Value > "1/31/2018" Then
        cell.EntireRow.Hidden = True
    End If
Next cell
End Sub

运行此命令时,它只会隐藏非空单元格的任何内容。我在将单元格定义为Range和定义为Variant之间进行了循环,这两种方法都是相同的。

ETA:

现在正在工作,并得到了所有人的帮助。对此,我真的非常感激!这就是我的结局。

Sub January()
'
'
'
Dim cell As Range
For Each cell In Range("Date")
    If cell.Value = "" Then
        cell.EntireRow.Hidden = False
    ElseIf cell.Value < CDate("1/1") Or cell.Value > CDate("1/31") Then
        cell.EntireRow.Hidden = True
    End If
Next cell
End Sub

我从代码中删除了年份,以便将来无需更改任何代码。

5 个答案:

答案 0 :(得分:1)

您当前的设置会将所有日期限定为<或>相应的日期比较。

如果您要在此代码中隐藏1月的行,则需要使用AND而不是OR

并确保您使用>=<=来包括那些开始和结束日期。

If cell >= "1/1/2018" AND cell <= "1/31/2018" Then

如果您要隐藏不是在一月份的行,那么您的<>会被转置:

If cell < "1/1/2018" OR cell > "1/31/2018" Then

答案 1 :(得分:1)

替代方法:如果您拥有Excel 2013或更高版本,只需添加一个Table Slicer并过滤由/** * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved. * For licensing, see LICENSE.md. */ // The editor creator to use. import ClassicEditorBase from '@ckeditor/ckeditor5-editor-classic/src/classiceditor'; import Essentials from '@ckeditor/ckeditor5-essentials/src/essentials'; import UploadAdapter from '@ckeditor/ckeditor5-adapter-ckfinder/src/uploadadapter'; import Autoformat from '@ckeditor/ckeditor5-autoformat/src/autoformat'; import Bold from '@ckeditor/ckeditor5-basic-styles/src/bold'; import Italic from '@ckeditor/ckeditor5-basic-styles/src/italic'; import BlockQuote from '@ckeditor/ckeditor5-block-quote/src/blockquote'; import Heading from '@ckeditor/ckeditor5-heading/src/heading'; import Link from '@ckeditor/ckeditor5-link/src/link'; import List from '@ckeditor/ckeditor5-list/src/list'; import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph'; import Alignment from '@ckeditor/ckeditor5-alignment/src/alignment'; import Table from '@ckeditor/ckeditor5-table/src/table'; import TableToolbar from '@ckeditor/ckeditor5-table/src/tabletoolbar'; export default class ClassicEditor extends ClassicEditorBase {} // Plugins to include in the build. ClassicEditor.builtinPlugins = [ Essentials, UploadAdapter, Autoformat, Bold, Italic, BlockQuote, Heading, Link, List, Paragraph, Alignment, Table, TableToolbar ]; // Editor configuration. ClassicEditor.defaultConfig = { toolbar: { items: [ 'heading', '|', 'alignment', '|', 'bold', 'italic', '|', 'link', '|', 'bulletedList', 'numberedList', '|', 'blockQuote', '|', 'insertTable', '|', 'undo', 'redo' ] }, heading: { options: [ { model: 'paragraph', title: 'parágrafo normal', class: 'ck-heading_paragraph' }, { model: 'heading1', view: 'h1', title: 'Título 1', class: 'ck-heading_heading1' }, { model: 'heading2', view: 'h2', title: 'Título 2', class: 'ck-heading_heading2' }, { model: 'heading3', view: 'h3', title: 'Título 3', class: 'ck-heading_heading3' }, { model: 'heading4', view: 'h4', title: 'Título 4', class: 'ck-heading_heading4' }, { model: 'heading5', view: 'h5', title: 'Título 5', class: 'ck-heading_heading5' }, { model: 'heading6', view: 'h6', title: 'Título 6', class: 'ck-heading_heading6' }, { model: 'heading7', view: 'h7', title: 'Título 7', class: 'ck-heading_heading7' } ] }, table: { toolbar: [ 'tableColumn', 'tableRow', 'mergeTableCells' ] }, language: 'pt-br' }; 生成的MONTH列,如下所示:

enter image description here

或者以其他方式使用数据透视表和切片器: enter image description here

要了解设置数据透视表有多么容易,请参阅VBA to copy data if multiple criteria are met

答案 2 :(得分:1)

最终,我相信这是您要查找的代码:

Sub January()
Dim cell As Range

Application.ScreenUpdating = False

For Each cell In Range("Date")
 'If date falls on or after January 1, AND on or before January 31, don't hide the row
  If cell.Value >= CDate("1/1/2018") And cell.Value <= CDate("1/31/2018") Then
    cell.EntireRow.Hidden = False
  Else
   'If the cell doesn't contain anything or isn't in January, hide the row
    cell.EntireRow.Hidden = True
  End If
Next cell

Application.ScreenUpdating = True
End Sub

您需要使用And逻辑,而不是 Or逻辑。 Or逻辑总是返回TRUE ,除非这两个表达式均为假或涉及空值。因此,自从您拥有的每个日期(我假设是)在2018年1月1日之后掉下来之后,代码一旦评估为true便停止查看您的逻辑语句。这反过来导致行意外隐藏。

此外,我将使用CDate将您拥有的字符串转换为日期。它可以帮助Excel更好地了解正在发生的事情,并使外部人员更容易理解您的代码。另一个可以做的好的做法是在代码中添加注释。我认为我们所有人都已经通过在某些时候将注释留在代码之外而学会了困难的方法。

最后一件事:如果您打算每个月使用一个按钮,请考虑对所有按钮执行一个过程,并使用变量填充日期范围,可能使用输入框从用户那里获取值。如果您决定将来进行任何更改,它将为您省去很多麻烦。

答案 3 :(得分:0)

未经测试,写在手机上。我只是提供一种尝试使用MONTH和YEAR的替代方法。有些人可能会发现这种方法更容易理解。

Option Explicit

Sub January()

Dim cell As Range

For Each cell In Range("Date")
    If cell.Value = "" Then
        cell.EntireRow.Hidden = False
    Else
    cell.EntireRow.Hidden = (Month(cell.Value) = 1) and (year(cell.Value) = 2018)
End if

Next cell

End sub

答案 4 :(得分:0)

我实际上将使用SlicersTable
但是,如果您致电VBA您的简洁解决方案,那么我会说放弃循环。
别无所求,但如果Excel已经具有此功能,则可以使用它。
这就像我们需要利用的折扣或促销。
因此,为什么不循环而不是过滤?

Dim lr As Long, r As Range
With Sheet1 '/* sheet where data reside */
    .AutoFilterMode = False '/* reset any filtering already applied */
    lr = .Range("D" & .Rows.Count).End(xlUp).Row '/* get the target cells */
    Set r = .Range("D1:D" & lr) '/* explicitly set target object */
    '/* filter without showing the dropdown, see the last argument set to false */
    r.AutoFilter 1, ">=2/1/2018", xlAnd, "<=2/28/2018", False 
End With

以上是今年2月,您可以对其进行调整以使其具有动态性。
您可以每月只创建一个通用的子程序即可创建单独的 sub过程

相关问题